blob: 496202380af8d07a018c762f123f76458713d0c0 [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
Evan Cheng8688a582013-01-29 02:32:37 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
611 setOperationAction(ISD::FSIN , MVT::f32, Expand);
612 setOperationAction(ISD::FCOS , MVT::f32, Expand);
613 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614
Chris Lattnera54aa942006-01-29 06:26:08 +0000615 // Expand FP immediates into loads from the stack, except for the special
616 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 addLegalFPImmediate(APFloat(+0.0)); // xorpd
618 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000619 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 // Use SSE for f32, x87 for f64.
621 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000622 addRegisterClass(MVT::f32, &X86::FR32RegClass);
623 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000624
625 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000627
628 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632
633 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
635 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000636
637 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000638 setOperationAction(ISD::FSIN , MVT::f32, Expand);
639 setOperationAction(ISD::FCOS , MVT::f32, Expand);
640 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641
Nate Begemane1795842008-02-14 08:57:00 +0000642 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // xorps
644 addLegalFPImmediate(APFloat(+0.0)); // FLD0
645 addLegalFPImmediate(APFloat(+1.0)); // FLD1
646 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
647 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000650 setOperationAction(ISD::FSIN , MVT::f64, Expand);
651 setOperationAction(ISD::FCOS , MVT::f64, Expand);
652 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000653 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000654 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000655 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000656 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000657 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
658 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000659
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
661 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
662 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
663 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000664
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000665 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000666 setOperationAction(ISD::FSIN , MVT::f64, Expand);
667 setOperationAction(ISD::FSIN , MVT::f32, Expand);
668 setOperationAction(ISD::FCOS , MVT::f64, Expand);
669 setOperationAction(ISD::FCOS , MVT::f32, Expand);
670 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
671 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000672 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000673 addLegalFPImmediate(APFloat(+0.0)); // FLD0
674 addLegalFPImmediate(APFloat(+1.0)); // FLD1
675 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
676 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000677 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
678 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
679 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
680 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000681 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682
Cameron Zwarich33390842011-07-08 21:39:21 +0000683 // We don't support FMA.
684 setOperationAction(ISD::FMA, MVT::f64, Expand);
685 setOperationAction(ISD::FMA, MVT::f32, Expand);
686
Dale Johannesen59a58732007-08-05 18:49:15 +0000687 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000688 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000689 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
691 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000692 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000693 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000694 addLegalFPImmediate(TmpFlt); // FLD0
695 TmpFlt.changeSign();
696 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000697
698 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000699 APFloat TmpFlt2(+1.0);
700 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
701 &ignored);
702 addLegalFPImmediate(TmpFlt2); // FLD1
703 TmpFlt2.changeSign();
704 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
705 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000706
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000707 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000708 setOperationAction(ISD::FSIN , MVT::f80, Expand);
709 setOperationAction(ISD::FCOS , MVT::f80, Expand);
710 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000711 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000712
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000713 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
714 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
715 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
716 setOperationAction(ISD::FRINT, MVT::f80, Expand);
717 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000718 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000719 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000720
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000721 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
723 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
724 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, MVT::f80, Expand);
727 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
728 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
729 setOperationAction(ISD::FEXP, MVT::f80, Expand);
730 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000731
Mon P Wangf007a8b2008-11-06 05:31:54 +0000732 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000733 // (for widening) or expand (for scalarization). Then we will selectively
734 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000735 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
736 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000737 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000738 setOperationAction(ISD::ADD , VT, Expand);
739 setOperationAction(ISD::SUB , VT, Expand);
740 setOperationAction(ISD::FADD, VT, Expand);
741 setOperationAction(ISD::FNEG, VT, Expand);
742 setOperationAction(ISD::FSUB, VT, Expand);
743 setOperationAction(ISD::MUL , VT, Expand);
744 setOperationAction(ISD::FMUL, VT, Expand);
745 setOperationAction(ISD::SDIV, VT, Expand);
746 setOperationAction(ISD::UDIV, VT, Expand);
747 setOperationAction(ISD::FDIV, VT, Expand);
748 setOperationAction(ISD::SREM, VT, Expand);
749 setOperationAction(ISD::UREM, VT, Expand);
750 setOperationAction(ISD::LOAD, VT, Expand);
751 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
752 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
753 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
754 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
755 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
756 setOperationAction(ISD::FABS, VT, Expand);
757 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000758 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000759 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000760 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000761 setOperationAction(ISD::FREM, VT, Expand);
762 setOperationAction(ISD::FMA, VT, Expand);
763 setOperationAction(ISD::FPOWI, VT, Expand);
764 setOperationAction(ISD::FSQRT, VT, Expand);
765 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
766 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000767 setOperationAction(ISD::FCEIL, VT, Expand);
768 setOperationAction(ISD::FTRUNC, VT, Expand);
769 setOperationAction(ISD::FRINT, VT, Expand);
770 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000771 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
772 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
773 setOperationAction(ISD::SDIVREM, VT, Expand);
774 setOperationAction(ISD::UDIVREM, VT, Expand);
775 setOperationAction(ISD::FPOW, VT, Expand);
776 setOperationAction(ISD::CTPOP, VT, Expand);
777 setOperationAction(ISD::CTTZ, VT, Expand);
778 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
779 setOperationAction(ISD::CTLZ, VT, Expand);
780 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
781 setOperationAction(ISD::SHL, VT, Expand);
782 setOperationAction(ISD::SRA, VT, Expand);
783 setOperationAction(ISD::SRL, VT, Expand);
784 setOperationAction(ISD::ROTL, VT, Expand);
785 setOperationAction(ISD::ROTR, VT, Expand);
786 setOperationAction(ISD::BSWAP, VT, Expand);
787 setOperationAction(ISD::SETCC, VT, Expand);
788 setOperationAction(ISD::FLOG, VT, Expand);
789 setOperationAction(ISD::FLOG2, VT, Expand);
790 setOperationAction(ISD::FLOG10, VT, Expand);
791 setOperationAction(ISD::FEXP, VT, Expand);
792 setOperationAction(ISD::FEXP2, VT, Expand);
793 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
794 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
795 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
796 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
797 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
798 setOperationAction(ISD::TRUNCATE, VT, Expand);
799 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
800 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
801 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
802 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000803 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
804 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000805 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000806 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000807 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
808 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
809 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000810 }
811
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
813 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000814 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000815 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000816 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000817 }
818
Dale Johannesen0488fb62010-09-30 23:57:10 +0000819 // MMX-sized vectors (other than x86mmx) are expected to be expanded
820 // into smaller operations.
821 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
822 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
823 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
824 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
825 setOperationAction(ISD::AND, MVT::v8i8, Expand);
826 setOperationAction(ISD::AND, MVT::v4i16, Expand);
827 setOperationAction(ISD::AND, MVT::v2i32, Expand);
828 setOperationAction(ISD::AND, MVT::v1i64, Expand);
829 setOperationAction(ISD::OR, MVT::v8i8, Expand);
830 setOperationAction(ISD::OR, MVT::v4i16, Expand);
831 setOperationAction(ISD::OR, MVT::v2i32, Expand);
832 setOperationAction(ISD::OR, MVT::v1i64, Expand);
833 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
834 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
835 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
836 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
840 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
842 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
843 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
844 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
845 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000846 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
847 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
848 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
849 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000850
Craig Topper1accb7e2012-01-10 06:54:16 +0000851 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000852 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
855 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
856 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
857 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
858 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
859 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000860 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
862 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
863 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
864 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
865 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000866 }
867
Craig Topper1accb7e2012-01-10 06:54:16 +0000868 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000869 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000870
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000871 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
872 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000873 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
874 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
875 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
876 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
879 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
880 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
881 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000882 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
884 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
885 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
886 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
887 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
888 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
889 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
890 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
891 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
892 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
893 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
894 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000895 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000896
Nadav Rotem354efd82011-09-18 14:57:03 +0000897 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000898 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
899 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
900 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
903 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
905 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
906 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000907
Evan Cheng2c3ae372006-04-12 21:21:57 +0000908 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000909 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000910 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000911 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000913 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000914 // Do not attempt to custom lower non-128-bit vectors
915 if (!VT.is128BitVector())
916 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000917 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
918 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
919 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000920 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000921
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
923 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
924 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
925 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
926 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000928
Nate Begemancdd1eec2008-02-12 22:51:28 +0000929 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
931 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000932 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000933
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000934 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000935 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000936 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000937
938 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000939 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000940 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000941
Craig Topper0d1f1762012-08-12 00:34:56 +0000942 setOperationAction(ISD::AND, VT, Promote);
943 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
944 setOperationAction(ISD::OR, VT, Promote);
945 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
946 setOperationAction(ISD::XOR, VT, Promote);
947 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
948 setOperationAction(ISD::LOAD, VT, Promote);
949 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
950 setOperationAction(ISD::SELECT, VT, Promote);
951 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000952 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000953
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000955
Evan Cheng2c3ae372006-04-12 21:21:57 +0000956 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
958 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
959 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
960 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000961
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
963 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000964
Michael Liaoa7554632012-10-23 17:36:08 +0000965 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
966 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000967 // As there is no 64-bit GPR available, we need build a special custom
968 // sequence to convert from v2i32 to v2f32.
969 if (!Subtarget->is64Bit())
970 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000971
Michael Liao9d796db2012-10-10 16:32:15 +0000972 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000973 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000974
Michael Liaob8150d82012-09-10 18:33:51 +0000975 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000976 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000977
Craig Topperd0a31172012-01-10 06:37:29 +0000978 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000979 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
980 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
981 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
982 setOperationAction(ISD::FRINT, MVT::f32, Legal);
983 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
984 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
985 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
986 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
987 setOperationAction(ISD::FRINT, MVT::f64, Legal);
988 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
989
Craig Topper12fb5c62012-09-08 17:42:27 +0000990 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000991 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
992 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
993 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
994 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000995 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000996 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
997 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
998 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
999 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001002 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001003
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001004 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1005 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1006 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1007 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1008 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001009
Nate Begeman14d12ca2008-02-11 04:19:36 +00001010 // i8 and i16 vectors are custom , because the source register and source
1011 // source memory operand types are not the same width. f32 vectors are
1012 // custom since the immediate controlling the insert encodes additional
1013 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1015 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1016 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1017 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1020 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1021 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1022 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001023
Pete Coopera77214a2011-11-14 19:38:42 +00001024 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001025 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001026 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001027 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1028 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001029 }
1030 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001031
Craig Topper1accb7e2012-01-10 06:54:16 +00001032 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001033 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001035
Nadav Rotem43012222011-05-11 08:12:09 +00001036 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001037 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001038
Nadav Rotem43012222011-05-11 08:12:09 +00001039 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001040 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001041
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001042 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001043 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1044 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1045
1046 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1047 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1048
1049 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1050 } else {
1051 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1052 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1053
1054 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1055 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1056
1057 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1058 }
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001059 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1060 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001061 }
1062
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001063 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001064 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1065 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1066 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1067 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1068 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1069 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001070
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1073 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001074
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1076 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1077 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1078 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1079 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001080 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001081 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1082 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1083 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1084 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001086 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001087
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1089 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1090 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1091 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1092 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001093 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001094 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1095 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1096 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1097 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001099 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001100
Michael Liaobedcbd42012-10-16 18:14:11 +00001101 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001102 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001103
1104 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1105
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001106 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1107 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001108 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001109
Michael Liaoa7554632012-10-23 17:36:08 +00001110 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1111 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1112 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1113
Michael Liaob8150d82012-09-10 18:33:51 +00001114 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1115
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001116 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1117 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1118
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001119 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1120 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1121
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001122 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001123 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001124
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001125 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1126
Duncan Sands28b77e92011-09-06 19:07:46 +00001127 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1128 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1129 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1130 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001131
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001132 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1133 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1134 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1135
Craig Topperaaa643c2011-11-09 07:28:55 +00001136 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1137 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1138 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1139 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001140
Nadav Rotem0509db22012-12-28 05:45:24 +00001141 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1142 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1143 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1144 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1145 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1146 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001147
Craig Topperbf404372012-08-31 15:40:30 +00001148 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001149 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1150 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1151 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1152 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1153 setOperationAction(ISD::FMA, MVT::f32, Legal);
1154 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001155 }
Craig Topper880ef452012-08-11 22:34:26 +00001156
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001157 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001158 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1159 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1160 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1161 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001162
Craig Topperaaa643c2011-11-09 07:28:55 +00001163 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1164 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1165 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1166 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001167
Craig Topperaaa643c2011-11-09 07:28:55 +00001168 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1169 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1170 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001171 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001172
1173 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001174
1175 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1176 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1177
1178 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1179 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1180
1181 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001182
1183 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001184 } else {
1185 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1186 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1187 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1188 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1189
1190 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1191 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1192 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1193 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1194
1195 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1196 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1197 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1198 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001199
1200 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1201 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1202
1203 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1204 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1205
1206 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001207 }
Craig Topper13894fa2011-08-24 06:14:18 +00001208
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001209 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001210 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1211 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001212 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001213
1214 // Extract subvector is special because the value type
1215 // (result) is 128-bit but the source is 256-bit wide.
1216 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001217 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001218
1219 // Do not attempt to custom lower other non-256-bit vectors
1220 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001221 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001222
Craig Topper0d1f1762012-08-12 00:34:56 +00001223 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1224 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1225 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1226 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1227 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1228 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1229 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001230 }
1231
David Greene54d8eba2011-01-27 22:38:56 +00001232 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001233 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001234 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001235
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001236 // Do not attempt to promote non-256-bit vectors
1237 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001238 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001239
Craig Topper0d1f1762012-08-12 00:34:56 +00001240 setOperationAction(ISD::AND, VT, Promote);
1241 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1242 setOperationAction(ISD::OR, VT, Promote);
1243 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1244 setOperationAction(ISD::XOR, VT, Promote);
1245 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1246 setOperationAction(ISD::LOAD, VT, Promote);
1247 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1248 setOperationAction(ISD::SELECT, VT, Promote);
1249 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001250 }
David Greene9b9838d2009-06-29 16:47:10 +00001251 }
1252
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001253 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1254 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001255 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1256 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001257 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1258 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001259 }
1260
Evan Cheng6be2c582006-04-05 23:38:46 +00001261 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001263 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001264
Eli Friedman962f5492010-06-02 19:35:46 +00001265 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1266 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001267 //
Eli Friedman962f5492010-06-02 19:35:46 +00001268 // FIXME: We really should do custom legalization for addition and
1269 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1270 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001271 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1272 // Add/Sub/Mul with overflow operations are custom lowered.
1273 MVT VT = IntVTs[i];
1274 setOperationAction(ISD::SADDO, VT, Custom);
1275 setOperationAction(ISD::UADDO, VT, Custom);
1276 setOperationAction(ISD::SSUBO, VT, Custom);
1277 setOperationAction(ISD::USUBO, VT, Custom);
1278 setOperationAction(ISD::SMULO, VT, Custom);
1279 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001280 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001281
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001282 // There are no 8-bit 3-address imul/mul instructions
1283 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1284 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001285
Evan Chengd54f2d52009-03-31 19:38:51 +00001286 if (!Subtarget->is64Bit()) {
1287 // These libcalls are not available in 32-bit.
1288 setLibcallName(RTLIB::SHL_I128, 0);
1289 setLibcallName(RTLIB::SRL_I128, 0);
1290 setLibcallName(RTLIB::SRA_I128, 0);
1291 }
1292
Evan Cheng8688a582013-01-29 02:32:37 +00001293 // Combine sin / cos into one node or libcall if possible.
1294 if (Subtarget->hasSinCos()) {
1295 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1296 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001297 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001298 // For MacOSX, we don't want to the normal expansion of a libcall to
1299 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1300 // traffic.
1301 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1302 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1303 }
1304 }
1305
Evan Cheng206ee9d2006-07-07 08:33:52 +00001306 // We have target-specific dag combine patterns for the following nodes:
1307 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001308 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001309 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001310 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001311 setTargetDAGCombine(ISD::SHL);
1312 setTargetDAGCombine(ISD::SRA);
1313 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001314 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001315 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001316 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001317 setTargetDAGCombine(ISD::FADD);
1318 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001319 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001320 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001321 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001322 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001323 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001324 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001325 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001326 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001327 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001328 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001329 if (Subtarget->is64Bit())
1330 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001331 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001332
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001333 computeRegisterProperties();
1334
Evan Cheng05219282011-01-06 06:52:41 +00001335 // On Darwin, -Os means optimize for size without hurting performance,
1336 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001337 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001338 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001339 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001340 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1341 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1342 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001343 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001344 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001345
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001346 // Predictable cmov don't hurt on atom because it's in-order.
1347 predictableSelectIsExpensive = !Subtarget->isAtom();
1348
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001349 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001350}
1351
Duncan Sands28b77e92011-09-06 19:07:46 +00001352EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1353 if (!VT.isVector()) return MVT::i8;
1354 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001355}
1356
Evan Cheng29286502008-01-23 23:17:41 +00001357/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1358/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001359static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001360 if (MaxAlign == 16)
1361 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001362 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001363 if (VTy->getBitWidth() == 128)
1364 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001365 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001366 unsigned EltAlign = 0;
1367 getMaxByValAlign(ATy->getElementType(), EltAlign);
1368 if (EltAlign > MaxAlign)
1369 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001370 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001371 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1372 unsigned EltAlign = 0;
1373 getMaxByValAlign(STy->getElementType(i), EltAlign);
1374 if (EltAlign > MaxAlign)
1375 MaxAlign = EltAlign;
1376 if (MaxAlign == 16)
1377 break;
1378 }
1379 }
Evan Cheng29286502008-01-23 23:17:41 +00001380}
1381
1382/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1383/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001384/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1385/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001386unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001387 if (Subtarget->is64Bit()) {
1388 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001389 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001390 if (TyAlign > 8)
1391 return TyAlign;
1392 return 8;
1393 }
1394
Evan Cheng29286502008-01-23 23:17:41 +00001395 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001396 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001397 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001398 return Align;
1399}
Chris Lattner2b02a442007-02-25 08:29:00 +00001400
Evan Chengf0df0312008-05-15 08:39:06 +00001401/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001402/// and store operations as a result of memset, memcpy, and memmove
1403/// lowering. If DstAlign is zero that means it's safe to destination
1404/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1405/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001406/// probably because the source does not need to be loaded. If 'IsMemset' is
1407/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1408/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1409/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001410/// It returns EVT::Other if the type should be determined using generic
1411/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001412EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001413X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1414 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001415 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001416 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001417 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001418 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001419 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001420 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1421 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001422 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001423 (Subtarget->isUnalignedMemAccessFast() ||
1424 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001425 (SrcAlign == 0 || SrcAlign >= 16)))) {
1426 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001427 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001428 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001429 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001430 return MVT::v8f32;
1431 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001432 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001433 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001434 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001435 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001436 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001437 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001438 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001439 // Do not use f64 to lower memcpy if source is string constant. It's
1440 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001441 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001442 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001443 }
Evan Chengf0df0312008-05-15 08:39:06 +00001444 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 return MVT::i64;
1446 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001447}
1448
Evan Cheng7d342672012-12-12 01:32:07 +00001449bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001450 if (VT == MVT::f32)
1451 return X86ScalarSSEf32;
1452 else if (VT == MVT::f64)
1453 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001454 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001455}
1456
Evan Cheng376642e2012-12-10 23:21:26 +00001457bool
1458X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1459 if (Fast)
1460 *Fast = Subtarget->isUnalignedMemAccessFast();
1461 return true;
1462}
1463
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001464/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1465/// current function. The returned value is a member of the
1466/// MachineJumpTableInfo::JTEntryKind enum.
1467unsigned X86TargetLowering::getJumpTableEncoding() const {
1468 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1469 // symbol.
1470 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1471 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001472 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001473
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001474 // Otherwise, use the normal jump table encoding heuristics.
1475 return TargetLowering::getJumpTableEncoding();
1476}
1477
Chris Lattnerc64daab2010-01-26 05:02:42 +00001478const MCExpr *
1479X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1480 const MachineBasicBlock *MBB,
1481 unsigned uid,MCContext &Ctx) const{
1482 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1483 Subtarget->isPICStyleGOT());
1484 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1485 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001486 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1487 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001488}
1489
Evan Chengcc415862007-11-09 01:32:10 +00001490/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1491/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001492SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001493 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001494 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001495 // This doesn't have DebugLoc associated with it, but is not really the
1496 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001497 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001498 return Table;
1499}
1500
Chris Lattner589c6f62010-01-26 06:28:43 +00001501/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1502/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1503/// MCExpr.
1504const MCExpr *X86TargetLowering::
1505getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1506 MCContext &Ctx) const {
1507 // X86-64 uses RIP relative addressing based on the jump table label.
1508 if (Subtarget->isPICStyleRIPRel())
1509 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1510
1511 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001512 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001513}
1514
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001515// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001516std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001517X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001518 const TargetRegisterClass *RRC = 0;
1519 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001520 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001521 default:
1522 return TargetLowering::findRepresentativeClass(VT);
1523 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001524 RRC = Subtarget->is64Bit() ?
1525 (const TargetRegisterClass*)&X86::GR64RegClass :
1526 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001527 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001528 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001529 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001530 break;
1531 case MVT::f32: case MVT::f64:
1532 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1533 case MVT::v4f32: case MVT::v2f64:
1534 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1535 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001536 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001537 break;
1538 }
1539 return std::make_pair(RRC, Cost);
1540}
1541
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001542bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1543 unsigned &Offset) const {
1544 if (!Subtarget->isTargetLinux())
1545 return false;
1546
1547 if (Subtarget->is64Bit()) {
1548 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1549 Offset = 0x28;
1550 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1551 AddressSpace = 256;
1552 else
1553 AddressSpace = 257;
1554 } else {
1555 // %gs:0x14 on i386
1556 Offset = 0x14;
1557 AddressSpace = 256;
1558 }
1559 return true;
1560}
1561
Chris Lattner2b02a442007-02-25 08:29:00 +00001562//===----------------------------------------------------------------------===//
1563// Return Value Calling Convention Implementation
1564//===----------------------------------------------------------------------===//
1565
Chris Lattner59ed56b2007-02-28 04:55:35 +00001566#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001567
Michael J. Spencerec38de22010-10-10 22:04:20 +00001568bool
Eric Christopher471e4222011-06-08 23:55:35 +00001569X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001570 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001571 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001572 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001573 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001574 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001575 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001576 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001577}
1578
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579SDValue
1580X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001581 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001583 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001584 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001585 MachineFunction &MF = DAG.getMachineFunction();
1586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattner9774c912007-02-27 05:28:59 +00001588 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001589 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 RVLocs, *DAG.getContext());
1591 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Evan Chengdcea1632010-02-04 02:40:39 +00001593 // Add the regs to the liveout set for the function.
1594 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1595 for (unsigned i = 0; i != RVLocs.size(); ++i)
1596 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1597 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
Dan Gohman475871a2008-07-27 21:46:04 +00001599 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001600
Dan Gohman475871a2008-07-27 21:46:04 +00001601 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001602 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1603 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001604 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1605 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001607 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001608 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1609 CCValAssign &VA = RVLocs[i];
1610 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001611 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001612 EVT ValVT = ValToCopy.getValueType();
1613
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001614 // Promote values to the appropriate types
1615 if (VA.getLocInfo() == CCValAssign::SExt)
1616 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1617 else if (VA.getLocInfo() == CCValAssign::ZExt)
1618 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1619 else if (VA.getLocInfo() == CCValAssign::AExt)
1620 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1621 else if (VA.getLocInfo() == CCValAssign::BCvt)
1622 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1623
Dale Johannesenc4510512010-09-24 19:05:48 +00001624 // If this is x86-64, and we disabled SSE, we can't return FP values,
1625 // or SSE or MMX vectors.
1626 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1627 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001628 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001629 report_fatal_error("SSE register return with SSE disabled");
1630 }
1631 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1632 // llvm-gcc has never done it right and no one has noticed, so this
1633 // should be OK for now.
1634 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001635 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001636 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Chris Lattner447ff682008-03-11 03:23:40 +00001638 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1639 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001640 if (VA.getLocReg() == X86::ST0 ||
1641 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001642 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1643 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001644 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001646 RetOps.push_back(ValToCopy);
1647 // Don't emit a copytoreg.
1648 continue;
1649 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001650
Evan Cheng242b38b2009-02-23 09:03:22 +00001651 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1652 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001653 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001654 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001655 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001656 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001657 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1658 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001659 // If we don't have SSE2 available, convert to v4f32 so the generated
1660 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001661 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001662 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001663 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001664 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001665 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001666
Dale Johannesendd64c412009-02-04 00:33:20 +00001667 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001668 Flag = Chain.getValue(1);
1669 }
Dan Gohman61a92132008-04-21 23:59:07 +00001670
Eli Benderskya5597f02013-01-25 22:07:43 +00001671 // The x86-64 ABIs require that for returning structs by value we copy
1672 // the sret argument into %rax/%eax (depending on ABI) for the return.
1673 // We saved the argument into a virtual register in the entry block,
1674 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001675 if (Subtarget->is64Bit() &&
1676 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1677 MachineFunction &MF = DAG.getMachineFunction();
1678 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1679 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001680 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001681 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001682 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001683
Eli Benderskya5597f02013-01-25 22:07:43 +00001684 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1685 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001686 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001687
Eli Benderskya5597f02013-01-25 22:07:43 +00001688 // RAX/EAX now acts like a return value.
1689 MRI.addLiveOut(RetValReg);
Dan Gohman61a92132008-04-21 23:59:07 +00001690 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
Chris Lattner447ff682008-03-11 03:23:40 +00001692 RetOps[0] = Chain; // Update chain.
1693
1694 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001695 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001696 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001697
1698 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001700}
1701
Evan Chengbf010eb2012-04-10 01:51:00 +00001702bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001703 if (N->getNumValues() != 1)
1704 return false;
1705 if (!N->hasNUsesOfValue(1, 0))
1706 return false;
1707
Evan Chengbf010eb2012-04-10 01:51:00 +00001708 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001709 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001710 if (Copy->getOpcode() == ISD::CopyToReg) {
1711 // If the copy has a glue operand, we conservatively assume it isn't safe to
1712 // perform a tail call.
1713 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1714 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001715 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001716 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001717 return false;
1718
Evan Cheng1bf891a2010-12-01 22:59:46 +00001719 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001720 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001721 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001722 if (UI->getOpcode() != X86ISD::RET_FLAG)
1723 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001724 HasRet = true;
1725 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001726
Evan Chengbf010eb2012-04-10 01:51:00 +00001727 if (!HasRet)
1728 return false;
1729
1730 Chain = TCChain;
1731 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001732}
1733
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001734MVT
1735X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001736 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001737 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001738 // TODO: Is this also valid on 32-bit?
1739 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001740 ReturnMVT = MVT::i8;
1741 else
1742 ReturnMVT = MVT::i32;
1743
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001744 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001745 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001746}
1747
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748/// LowerCallResult - Lower the result values of a call into the
1749/// appropriate copies out of appropriate physical registers.
1750///
1751SDValue
1752X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001753 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 const SmallVectorImpl<ISD::InputArg> &Ins,
1755 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001756 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001757
Chris Lattnere32bbf62007-02-28 07:09:55 +00001758 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001759 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001760 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001761 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001762 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001764
Chris Lattner3085e152007-02-25 08:59:22 +00001765 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001766 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001767 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001768 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Torok Edwin3f142c32009-02-01 18:15:56 +00001770 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001772 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001773 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001774 }
1775
Evan Cheng79fb3b42009-02-20 20:43:02 +00001776 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001777
1778 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001779 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001780 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001781 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001782 // instead.
1783 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1784 // If we prefer to use the value in xmm registers, copy it out as f80 and
1785 // use a truncate to move it from fp stack reg to xmm reg.
1786 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001787 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001788 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1789 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001790 Val = Chain.getValue(0);
1791
1792 // Round the f80 to the right size, which also moves it to the appropriate
1793 // xmm register.
1794 if (CopyVT != VA.getValVT())
1795 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1796 // This truncation won't change the value.
1797 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001798 } else {
1799 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1800 CopyVT, InFlag).getValue(1);
1801 Val = Chain.getValue(0);
1802 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001803 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001805 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001806
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001808}
1809
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001810//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001811// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001812//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001813// StdCall calling convention seems to be standard for many Windows' API
1814// routines and around. It differs from C calling convention just a little:
1815// callee should clean up the stack, not caller. Symbols should be also
1816// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001817// For info on fast calling convention see Fast Calling Convention (tail call)
1818// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001819
Dan Gohman98ca4f22009-08-05 01:29:28 +00001820/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001821/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001822enum StructReturnType {
1823 NotStructReturn,
1824 RegStructReturn,
1825 StackStructReturn
1826};
1827static StructReturnType
1828callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001830 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001831
Rafael Espindola1cee7102012-07-25 13:41:10 +00001832 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1833 if (!Flags.isSRet())
1834 return NotStructReturn;
1835 if (Flags.isInReg())
1836 return RegStructReturn;
1837 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001838}
1839
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001840/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001841/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001842static StructReturnType
1843argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001845 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001846
Rafael Espindola1cee7102012-07-25 13:41:10 +00001847 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1848 if (!Flags.isSRet())
1849 return NotStructReturn;
1850 if (Flags.isInReg())
1851 return RegStructReturn;
1852 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001853}
1854
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001855/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1856/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001857/// the specific parameter attribute. The copy will be passed as a byval
1858/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001859static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001860CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001861 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1862 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001863 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001864
Dale Johannesendd64c412009-02-04 00:33:20 +00001865 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001866 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001867 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001868}
1869
Chris Lattner29689432010-03-11 00:22:57 +00001870/// IsTailCallConvention - Return true if the calling convention is one that
1871/// supports tail call optimization.
1872static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001873 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1874 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001875}
1876
Evan Cheng485fafc2011-03-21 01:19:09 +00001877bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001878 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001879 return false;
1880
1881 CallSite CS(CI);
1882 CallingConv::ID CalleeCC = CS.getCallingConv();
1883 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1884 return false;
1885
1886 return true;
1887}
1888
Evan Cheng0c439eb2010-01-27 00:07:07 +00001889/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1890/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001891static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1892 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001893 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001894}
1895
Dan Gohman98ca4f22009-08-05 01:29:28 +00001896SDValue
1897X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001898 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 const SmallVectorImpl<ISD::InputArg> &Ins,
1900 DebugLoc dl, SelectionDAG &DAG,
1901 const CCValAssign &VA,
1902 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001903 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001904 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001906 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1907 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001908 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001909 EVT ValVT;
1910
1911 // If value is passed by pointer we have address passed instead of the value
1912 // itself.
1913 if (VA.getLocInfo() == CCValAssign::Indirect)
1914 ValVT = VA.getLocVT();
1915 else
1916 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001917
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001918 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001919 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001920 // In case of tail call optimization mark all arguments mutable. Since they
1921 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001922 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001923 unsigned Bytes = Flags.getByValSize();
1924 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1925 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001926 return DAG.getFrameIndex(FI, getPointerTy());
1927 } else {
1928 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001929 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001930 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1931 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001932 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001933 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001934 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001935}
1936
Dan Gohman475871a2008-07-27 21:46:04 +00001937SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001939 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940 bool isVarArg,
1941 const SmallVectorImpl<ISD::InputArg> &Ins,
1942 DebugLoc dl,
1943 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001944 SmallVectorImpl<SDValue> &InVals)
1945 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001946 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001947 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001948
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 const Function* Fn = MF.getFunction();
1950 if (Fn->hasExternalLinkage() &&
1951 Subtarget->isTargetCygMing() &&
1952 Fn->getName() == "main")
1953 FuncInfo->setForceFramePointer(true);
1954
Evan Cheng1bc78042006-04-26 01:20:17 +00001955 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001957 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001958 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001959
Chris Lattner29689432010-03-11 00:22:57 +00001960 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001961 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001962
Chris Lattner638402b2007-02-28 07:00:42 +00001963 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001964 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001965 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001967
1968 // Allocate shadow area for Win64
1969 if (IsWin64) {
1970 CCInfo.AllocateStack(32, 8);
1971 }
1972
Duncan Sands45907662010-10-31 13:21:44 +00001973 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001974
Chris Lattnerf39f7712007-02-28 05:46:49 +00001975 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001976 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001977 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1978 CCValAssign &VA = ArgLocs[i];
1979 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1980 // places.
1981 assert(VA.getValNo() != LastVal &&
1982 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001983 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Chris Lattnerf39f7712007-02-28 05:46:49 +00001986 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001987 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001988 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001990 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001994 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001997 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001999 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002000 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002001 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002002 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002003 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002004 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002005
Devang Patel68e6bee2011-02-21 23:21:26 +00002006 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002008
Chris Lattnerf39f7712007-02-28 05:46:49 +00002009 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2010 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2011 // right size.
2012 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002013 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002014 DAG.getValueType(VA.getValVT()));
2015 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002016 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002017 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002018 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002019 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002021 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002022 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002023 if (RegVT.isVector())
2024 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2025 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002026 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002027 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002028 } else {
2029 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002030 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002031 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002032
2033 // If value is passed via pointer - do a load.
2034 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002035 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002036 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002037
Dan Gohman98ca4f22009-08-05 01:29:28 +00002038 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002039 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002040
Eli Benderskya5597f02013-01-25 22:07:43 +00002041 // The x86-64 ABIs require that for returning structs by value we copy
2042 // the sret argument into %rax/%eax (depending on ABI) for the return.
2043 // Save the argument into a virtual register so that we can access it
2044 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002045 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002046 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2047 unsigned Reg = FuncInfo->getSRetReturnReg();
2048 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002049 MVT PtrTy = getPointerTy();
2050 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002051 FuncInfo->setSRetReturnReg(Reg);
2052 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002055 }
2056
Chris Lattnerf39f7712007-02-28 05:46:49 +00002057 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002058 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002059 if (FuncIsMadeTailCallSafe(CallConv,
2060 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002061 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002062
Evan Cheng1bc78042006-04-26 01:20:17 +00002063 // If the function takes variable number of arguments, make a frame index for
2064 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002065 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002066 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2067 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002068 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002069 }
2070 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2072
2073 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002074 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002075 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002076 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002077 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002078 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2079 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002080 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2082 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2083 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002084 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002085 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086
2087 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002088 // The XMM registers which might contain var arg parameters are shadowed
2089 // in their paired GPR. So we only need to save the GPR to their home
2090 // slots.
2091 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002092 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093 } else {
2094 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2095 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096
Chad Rosier30450e82011-12-22 22:35:21 +00002097 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2098 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002099 }
2100 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2101 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002102
Bill Wendling831737d2012-12-30 10:32:01 +00002103 bool NoImplicitFloatOps = Fn->getAttributes().
2104 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002105 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002106 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002107 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2108 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002109 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002110 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002111 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002112 // Kernel mode asks for SSE to be disabled, so don't push them
2113 // on the stack.
2114 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002115
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002116 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002117 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002118 // Get to the caller-allocated home save location. Add 8 to account
2119 // for the return address.
2120 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002121 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002122 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002123 // Fixup to set vararg frame on shadow area (4 x i64).
2124 if (NumIntRegs < 4)
2125 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002126 } else {
2127 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002128 // registers, then we must store them to their spots on the stack so
2129 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002130 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2131 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2132 FuncInfo->setRegSaveFrameIndex(
2133 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002134 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002135 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002136
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002139 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2140 getPointerTy());
2141 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002142 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002143 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2144 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002145 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002146 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002149 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002150 MachinePointerInfo::getFixedStack(
2151 FuncInfo->getRegSaveFrameIndex(), Offset),
2152 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002154 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002156
Dan Gohmanface41a2009-08-16 21:24:25 +00002157 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2158 // Now store the XMM (fp + vector) parameter registers.
2159 SmallVector<SDValue, 11> SaveXMMOps;
2160 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002161
Craig Topperc9099502012-04-20 06:31:50 +00002162 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002163 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2164 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002165
Dan Gohman1e93df62010-04-17 14:41:14 +00002166 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2167 FuncInfo->getRegSaveFrameIndex()));
2168 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2169 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002170
Dan Gohmanface41a2009-08-16 21:24:25 +00002171 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002172 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002173 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002174 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2175 SaveXMMOps.push_back(Val);
2176 }
2177 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2178 MVT::Other,
2179 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002181
2182 if (!MemOps.empty())
2183 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2184 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002187
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002189 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2190 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002191 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002192 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002193 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002194 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002195 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002196 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002197 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002198 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002199
Gordon Henriksen86737662008-01-05 16:56:59 +00002200 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002201 // RegSaveFrameIndex is X86-64 only.
2202 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002203 if (CallConv == CallingConv::X86_FastCall ||
2204 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002205 // fastcc functions can't have varargs.
2206 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002207 }
Evan Cheng25caf632006-05-23 21:06:34 +00002208
Rafael Espindola76927d752011-08-30 19:39:58 +00002209 FuncInfo->setArgumentStackSize(StackSize);
2210
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002212}
2213
Dan Gohman475871a2008-07-27 21:46:04 +00002214SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2216 SDValue StackPtr, SDValue Arg,
2217 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002218 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002219 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002220 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002222 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002223 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002224 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002225
2226 return DAG.getStore(Chain, dl, Arg, PtrOff,
2227 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002228 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002229}
2230
Bill Wendling64e87322009-01-16 19:25:27 +00002231/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002233SDValue
2234X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002235 SDValue &OutRetAddr, SDValue Chain,
2236 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002237 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002239 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002241
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002243 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002244 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002245 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002246}
2247
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002248/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002249/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002250static SDValue
2251EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002252 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2253 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002254 // Store the return address to the appropriate stack slot.
2255 if (!FPDiff) return Chain;
2256 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002257 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002258 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002259 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002261 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002262 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263 return Chain;
2264}
2265
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002267X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002268 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002269 SelectionDAG &DAG = CLI.DAG;
2270 DebugLoc &dl = CLI.DL;
2271 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2272 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2273 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2274 SDValue Chain = CLI.Chain;
2275 SDValue Callee = CLI.Callee;
2276 CallingConv::ID CallConv = CLI.CallConv;
2277 bool &isTailCall = CLI.IsTailCall;
2278 bool isVarArg = CLI.IsVarArg;
2279
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280 MachineFunction &MF = DAG.getMachineFunction();
2281 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002282 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002283 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002284 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002285 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002286
Nick Lewycky22de16d2012-01-19 00:34:10 +00002287 if (MF.getTarget().Options.DisableTailCalls)
2288 isTailCall = false;
2289
Evan Cheng5f941932010-02-05 02:21:12 +00002290 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002291 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002292 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002293 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002294 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002295 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002296
2297 // Sibcalls are automatically detected tailcalls which do not require
2298 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002299 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002300 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002301
2302 if (isTailCall)
2303 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002304 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002305
Chris Lattner29689432010-03-11 00:22:57 +00002306 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002307 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002308
Chris Lattner638402b2007-02-28 07:00:42 +00002309 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002310 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002311 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002312 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002313
2314 // Allocate shadow area for Win64
2315 if (IsWin64) {
2316 CCInfo.AllocateStack(32, 8);
2317 }
2318
Duncan Sands45907662010-10-31 13:21:44 +00002319 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Chris Lattner423c5f42007-02-28 05:31:48 +00002321 // Get a count of how many bytes are to be pushed on the stack.
2322 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002323 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002324 // This is a sibcall. The memory operands are available in caller's
2325 // own caller's stack.
2326 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002327 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2328 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002329 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002330
Gordon Henriksen86737662008-01-05 16:56:59 +00002331 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002334 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2335 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2336
Gordon Henriksen86737662008-01-05 16:56:59 +00002337 FPDiff = NumBytesCallerPushed - NumBytes;
2338
2339 // Set the delta of movement of the returnaddr stackslot.
2340 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002341 if (FPDiff < X86Info->getTCReturnAddrDelta())
2342 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 }
2344
Evan Chengf22f9b32010-02-06 03:28:46 +00002345 if (!IsSibcall)
2346 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002347
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002349 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002350 if (isTailCall && FPDiff)
2351 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2352 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002353
Dan Gohman475871a2008-07-27 21:46:04 +00002354 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2355 SmallVector<SDValue, 8> MemOpChains;
2356 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002357
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002358 // Walk the register/memloc assignments, inserting copies/loads. In the case
2359 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002360 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2361 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002362 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002363 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002364 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002365 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Chris Lattner423c5f42007-02-28 05:31:48 +00002367 // Promote the value if needed.
2368 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002369 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002370 case CCValAssign::Full: break;
2371 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002372 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002373 break;
2374 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002375 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002376 break;
2377 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002378 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002379 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002380 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2382 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002383 } else
2384 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2385 break;
2386 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002387 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002388 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002389 case CCValAssign::Indirect: {
2390 // Store the argument.
2391 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002392 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002393 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002394 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002395 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002396 Arg = SpillSlot;
2397 break;
2398 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Chris Lattner423c5f42007-02-28 05:31:48 +00002401 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002402 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2403 if (isVarArg && IsWin64) {
2404 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2405 // shadow reg if callee is a varargs function.
2406 unsigned ShadowReg = 0;
2407 switch (VA.getLocReg()) {
2408 case X86::XMM0: ShadowReg = X86::RCX; break;
2409 case X86::XMM1: ShadowReg = X86::RDX; break;
2410 case X86::XMM2: ShadowReg = X86::R8; break;
2411 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002412 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002413 if (ShadowReg)
2414 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002415 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002416 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002417 assert(VA.isMemLoc());
2418 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002419 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2420 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002421 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2422 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002423 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Evan Cheng32fe1032006-05-25 00:59:30 +00002426 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002428 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002429
Chris Lattner88e1fd52009-07-09 04:24:46 +00002430 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002431 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2432 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002433 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002434 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2435 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002436 } else {
2437 // If we are tail calling and generating PIC/GOT style code load the
2438 // address of the callee into ECX. The value in ecx is used as target of
2439 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2440 // for tail calls on PIC/GOT architectures. Normally we would just put the
2441 // address of GOT into ebx and then call target@PLT. But for tail calls
2442 // ebx would be restored (since ebx is callee saved) before jumping to the
2443 // target@PLT.
2444
2445 // Note: The actual moving to ECX is done further down.
2446 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2447 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2448 !G->getGlobal()->hasProtectedVisibility())
2449 Callee = LowerGlobalAddress(Callee, DAG);
2450 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002451 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002452 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002453 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002454
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002455 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 // From AMD64 ABI document:
2457 // For calls that may call functions that use varargs or stdargs
2458 // (prototype-less calls or calls to functions containing ellipsis (...) in
2459 // the declaration) %al is used as hidden argument to specify the number
2460 // of SSE registers used. The contents of %al do not need to match exactly
2461 // the number of registers, but must be an ubound on the number of SSE
2462 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002463
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002465 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002466 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2467 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2468 };
2469 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002470 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002471 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002472
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002473 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2474 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002475 }
2476
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002477 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002478 if (isTailCall) {
2479 // Force all the incoming stack arguments to be loaded from the stack
2480 // before any new outgoing arguments are stored to the stack, because the
2481 // outgoing stack slots may alias the incoming argument stack slots, and
2482 // the alias isn't otherwise explicit. This is slightly more conservative
2483 // than necessary, because it means that each store effectively depends
2484 // on every argument instead of just those arguments it would clobber.
2485 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2486
Dan Gohman475871a2008-07-27 21:46:04 +00002487 SmallVector<SDValue, 8> MemOpChains2;
2488 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002489 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002490 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002491 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2492 CCValAssign &VA = ArgLocs[i];
2493 if (VA.isRegLoc())
2494 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002495 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002496 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002497 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002498 // Create frame index.
2499 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002500 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002501 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002502 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002503
Duncan Sands276dcbd2008-03-21 09:14:45 +00002504 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002505 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002506 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002507 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002508 StackPtr = DAG.getCopyFromReg(Chain, dl,
2509 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002510 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002511 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002512
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2514 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002515 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002516 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002517 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002518 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002520 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002521 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002522 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002523 }
2524 }
2525
2526 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002527 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002528 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002529
2530 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002531 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2532 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002533 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 }
2535
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002536 // Build a sequence of copy-to-reg nodes chained together with token chain
2537 // and flag operands which copy the outgoing args into registers.
2538 SDValue InFlag;
2539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2540 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2541 RegsToPass[i].second, InFlag);
2542 InFlag = Chain.getValue(1);
2543 }
2544
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002545 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2546 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2547 // In the 64-bit large code model, we have to make all calls
2548 // through a register, since the call instruction's 32-bit
2549 // pc-relative offset may not be large enough to hold the whole
2550 // address.
2551 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002552 // If the callee is a GlobalAddress node (quite common, every direct call
2553 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2554 // it.
2555
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002556 // We should use extra load for direct calls to dllimported functions in
2557 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002558 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002559 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002560 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002561 bool ExtraLoad = false;
2562 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002563
Chris Lattner48a7d022009-07-09 05:02:21 +00002564 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2565 // external symbols most go through the PLT in PIC mode. If the symbol
2566 // has hidden or protected visibility, or if it is static or local, then
2567 // we don't need to use the PLT - we can directly call it.
2568 if (Subtarget->isTargetELF() &&
2569 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002570 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002572 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002573 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002574 (!Subtarget->getTargetTriple().isMacOSX() ||
2575 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002576 // PC-relative references to external symbols should go through $stub,
2577 // unless we're building with the leopard linker or later, which
2578 // automatically synthesizes these stubs.
2579 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002580 } else if (Subtarget->isPICStyleRIPRel() &&
2581 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002582 cast<Function>(GV)->getAttributes().
2583 hasAttribute(AttributeSet::FunctionIndex,
2584 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002585 // If the function is marked as non-lazy, generate an indirect call
2586 // which loads from the GOT directly. This avoids runtime overhead
2587 // at the cost of eager binding (and one extra byte of encoding).
2588 OpFlags = X86II::MO_GOTPCREL;
2589 WrapperKind = X86ISD::WrapperRIP;
2590 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002591 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002592
Devang Patel0d881da2010-07-06 22:08:15 +00002593 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002594 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002595
2596 // Add a wrapper if needed.
2597 if (WrapperKind != ISD::DELETED_NODE)
2598 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2599 // Add extra indirection if needed.
2600 if (ExtraLoad)
2601 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2602 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002603 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002604 }
Bill Wendling056292f2008-09-16 21:48:12 +00002605 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002606 unsigned char OpFlags = 0;
2607
Evan Cheng1bf891a2010-12-01 22:59:46 +00002608 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2609 // external symbols should go through the PLT.
2610 if (Subtarget->isTargetELF() &&
2611 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2612 OpFlags = X86II::MO_PLT;
2613 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002614 (!Subtarget->getTargetTriple().isMacOSX() ||
2615 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002616 // PC-relative references to external symbols should go through $stub,
2617 // unless we're building with the leopard linker or later, which
2618 // automatically synthesizes these stubs.
2619 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002620 }
Eric Christopherfd179292009-08-27 18:07:15 +00002621
Chris Lattner48a7d022009-07-09 05:02:21 +00002622 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2623 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002624 }
2625
Chris Lattnerd96d0722007-02-25 06:40:16 +00002626 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002627 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002628 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002629
Evan Chengf22f9b32010-02-06 03:28:46 +00002630 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002631 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2632 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002633 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002635
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002636 Ops.push_back(Chain);
2637 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002638
Dan Gohman98ca4f22009-08-05 01:29:28 +00002639 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002641
Gordon Henriksen86737662008-01-05 16:56:59 +00002642 // Add argument registers to the end of the list so that they are known live
2643 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002644 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2645 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2646 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002647
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002648 // Add a register mask operand representing the call-preserved registers.
2649 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2650 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2651 assert(Mask && "Missing call preserved mask for calling convention");
2652 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002653
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002655 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002656
Dan Gohman98ca4f22009-08-05 01:29:28 +00002657 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002658 // We used to do:
2659 //// If this is the first return lowered for this function, add the regs
2660 //// to the liveout set for the function.
2661 // This isn't right, although it's probably harmless on x86; liveouts
2662 // should be computed from returns not tail calls. Consider a void
2663 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 return DAG.getNode(X86ISD::TC_RETURN, dl,
2665 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002666 }
2667
Dale Johannesenace16102009-02-03 19:33:06 +00002668 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002669 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002670
Chris Lattner2d297092006-05-23 18:50:38 +00002671 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002672 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002673 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2674 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002675 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002676 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002677 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002678 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002679 // pops the hidden struct pointer, so we have to push it back.
2680 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002681 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002682 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002683 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002684 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002685
Gordon Henriksenae636f82008-01-03 16:47:34 +00002686 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002687 if (!IsSibcall) {
2688 Chain = DAG.getCALLSEQ_END(Chain,
2689 DAG.getIntPtrConstant(NumBytes, true),
2690 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2691 true),
2692 InFlag);
2693 InFlag = Chain.getValue(1);
2694 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002695
Chris Lattner3085e152007-02-25 08:59:22 +00002696 // Handle result values, copying them out of physregs into vregs that we
2697 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002698 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2699 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002700}
2701
Evan Cheng25ab6902006-09-08 06:48:29 +00002702//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002703// Fast Calling Convention (tail call) implementation
2704//===----------------------------------------------------------------------===//
2705
2706// Like std call, callee cleans arguments, convention except that ECX is
2707// reserved for storing the tail called function address. Only 2 registers are
2708// free for argument passing (inreg). Tail call optimization is performed
2709// provided:
2710// * tailcallopt is enabled
2711// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002712// On X86_64 architecture with GOT-style position independent code only local
2713// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002714// To keep the stack aligned according to platform abi the function
2715// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2716// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002717// If a tail called function callee has more arguments than the caller the
2718// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002719// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002720// original REtADDR, but before the saved framepointer or the spilled registers
2721// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2722// stack layout:
2723// arg1
2724// arg2
2725// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002726// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002727// move area ]
2728// (possible EBP)
2729// ESI
2730// EDI
2731// local1 ..
2732
2733/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2734/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002735unsigned
2736X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2737 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002738 MachineFunction &MF = DAG.getMachineFunction();
2739 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002740 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002741 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002742 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002743 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002744 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002745 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2746 // Number smaller than 12 so just add the difference.
2747 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2748 } else {
2749 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002750 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002751 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002752 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002753 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002754}
2755
Evan Cheng5f941932010-02-05 02:21:12 +00002756/// MatchingStackOffset - Return true if the given stack call argument is
2757/// already available in the same position (relatively) of the caller's
2758/// incoming argument stack.
2759static
2760bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2761 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2762 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002763 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2764 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002765 if (Arg.getOpcode() == ISD::CopyFromReg) {
2766 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002767 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002768 return false;
2769 MachineInstr *Def = MRI->getVRegDef(VR);
2770 if (!Def)
2771 return false;
2772 if (!Flags.isByVal()) {
2773 if (!TII->isLoadFromStackSlot(Def, FI))
2774 return false;
2775 } else {
2776 unsigned Opcode = Def->getOpcode();
2777 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2778 Def->getOperand(1).isFI()) {
2779 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002780 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002781 } else
2782 return false;
2783 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002784 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2785 if (Flags.isByVal())
2786 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002787 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002788 // define @foo(%struct.X* %A) {
2789 // tail call @bar(%struct.X* byval %A)
2790 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002791 return false;
2792 SDValue Ptr = Ld->getBasePtr();
2793 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2794 if (!FINode)
2795 return false;
2796 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002797 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002798 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002799 FI = FINode->getIndex();
2800 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002801 } else
2802 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002803
Evan Cheng4cae1332010-03-05 08:38:04 +00002804 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002805 if (!MFI->isFixedObjectIndex(FI))
2806 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002807 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002808}
2809
Dan Gohman98ca4f22009-08-05 01:29:28 +00002810/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2811/// for tail call optimization. Targets which want to do tail call
2812/// optimization should implement this function.
2813bool
2814X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002815 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002816 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002817 bool isCalleeStructRet,
2818 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002819 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002820 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002821 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002822 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002823 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002824 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002825 CalleeCC != CallingConv::C)
2826 return false;
2827
Evan Cheng7096ae42010-01-29 06:45:59 +00002828 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002829 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002830 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002831
2832 // If the function return type is x86_fp80 and the callee return type is not,
2833 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2834 // perform a tailcall optimization here.
2835 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2836 return false;
2837
Evan Cheng13617962010-04-30 01:12:32 +00002838 CallingConv::ID CallerCC = CallerF->getCallingConv();
2839 bool CCMatch = CallerCC == CalleeCC;
2840
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002841 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002842 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002843 return true;
2844 return false;
2845 }
2846
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002847 // Look for obvious safe cases to perform tail call optimization that do not
2848 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002849
Evan Cheng2c12cb42010-03-26 16:26:03 +00002850 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2851 // emit a special epilogue.
2852 if (RegInfo->needsStackRealignment(MF))
2853 return false;
2854
Evan Chenga375d472010-03-15 18:54:48 +00002855 // Also avoid sibcall optimization if either caller or callee uses struct
2856 // return semantics.
2857 if (isCalleeStructRet || isCallerStructRet)
2858 return false;
2859
Chad Rosier2416da32011-06-24 21:15:36 +00002860 // An stdcall caller is expected to clean up its arguments; the callee
2861 // isn't going to do that.
2862 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2863 return false;
2864
Chad Rosier871f6642011-05-18 19:59:50 +00002865 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002866 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002867 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002868
2869 // Optimizing for varargs on Win64 is unlikely to be safe without
2870 // additional testing.
2871 if (Subtarget->isTargetWin64())
2872 return false;
2873
Chad Rosier871f6642011-05-18 19:59:50 +00002874 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002875 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002876 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002877
Chad Rosier871f6642011-05-18 19:59:50 +00002878 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2879 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2880 if (!ArgLocs[i].isRegLoc())
2881 return false;
2882 }
2883
Chad Rosier30450e82011-12-22 22:35:21 +00002884 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2885 // stack. Therefore, if it's not used by the call it is not safe to optimize
2886 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002887 bool Unused = false;
2888 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2889 if (!Ins[i].Used) {
2890 Unused = true;
2891 break;
2892 }
2893 }
2894 if (Unused) {
2895 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002896 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002897 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002898 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002899 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002900 CCValAssign &VA = RVLocs[i];
2901 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2902 return false;
2903 }
2904 }
2905
Evan Cheng13617962010-04-30 01:12:32 +00002906 // If the calling conventions do not match, then we'd better make sure the
2907 // results are returned in the same way as what the caller expects.
2908 if (!CCMatch) {
2909 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002910 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002911 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002912 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2913
2914 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002915 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002916 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002917 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2918
2919 if (RVLocs1.size() != RVLocs2.size())
2920 return false;
2921 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2922 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2923 return false;
2924 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2925 return false;
2926 if (RVLocs1[i].isRegLoc()) {
2927 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2928 return false;
2929 } else {
2930 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2931 return false;
2932 }
2933 }
2934 }
2935
Evan Chenga6bff982010-01-30 01:22:00 +00002936 // If the callee takes no arguments then go on to check the results of the
2937 // call.
2938 if (!Outs.empty()) {
2939 // Check if stack adjustment is needed. For now, do not do this if any
2940 // argument is passed on the stack.
2941 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002942 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002943 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002944
2945 // Allocate shadow area for Win64
2946 if (Subtarget->isTargetWin64()) {
2947 CCInfo.AllocateStack(32, 8);
2948 }
2949
Duncan Sands45907662010-10-31 13:21:44 +00002950 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002951 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002952 MachineFunction &MF = DAG.getMachineFunction();
2953 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2954 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002955
2956 // Check if the arguments are already laid out in the right way as
2957 // the caller's fixed stack objects.
2958 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002959 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2960 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002961 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002962 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2963 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002964 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002965 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002966 if (VA.getLocInfo() == CCValAssign::Indirect)
2967 return false;
2968 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002969 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2970 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002971 return false;
2972 }
2973 }
2974 }
Evan Cheng9c044672010-05-29 01:35:22 +00002975
2976 // If the tailcall address may be in a register, then make sure it's
2977 // possible to register allocate for it. In 32-bit, the call address can
2978 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002979 // callee-saved registers are restored. These happen to be the same
2980 // registers used to pass 'inreg' arguments so watch out for those.
2981 if (!Subtarget->is64Bit() &&
2982 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002983 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002984 unsigned NumInRegs = 0;
2985 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2986 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002987 if (!VA.isRegLoc())
2988 continue;
2989 unsigned Reg = VA.getLocReg();
2990 switch (Reg) {
2991 default: break;
2992 case X86::EAX: case X86::EDX: case X86::ECX:
2993 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002994 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002995 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002996 }
2997 }
2998 }
Evan Chenga6bff982010-01-30 01:22:00 +00002999 }
Evan Chengb1712452010-01-27 06:25:16 +00003000
Evan Cheng86809cc2010-02-03 03:28:02 +00003001 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003002}
3003
Dan Gohman3df24e62008-09-03 23:12:08 +00003004FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003005X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3006 const TargetLibraryInfo *libInfo) const {
3007 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003008}
3009
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003010//===----------------------------------------------------------------------===//
3011// Other Lowering Hooks
3012//===----------------------------------------------------------------------===//
3013
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003014static bool MayFoldLoad(SDValue Op) {
3015 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3016}
3017
3018static bool MayFoldIntoStore(SDValue Op) {
3019 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3020}
3021
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003022static bool isTargetShuffle(unsigned Opcode) {
3023 switch(Opcode) {
3024 default: return false;
3025 case X86ISD::PSHUFD:
3026 case X86ISD::PSHUFHW:
3027 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003028 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003029 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003030 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003031 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003032 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003033 case X86ISD::MOVLPS:
3034 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003035 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003036 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003037 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003038 case X86ISD::MOVSS:
3039 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003040 case X86ISD::UNPCKL:
3041 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003042 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003043 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003044 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003045 return true;
3046 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003047}
3048
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003049static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003050 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003051 switch(Opc) {
3052 default: llvm_unreachable("Unknown x86 shuffle node");
3053 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003054 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003055 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003056 return DAG.getNode(Opc, dl, VT, V1);
3057 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003058}
3059
3060static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003061 SDValue V1, unsigned TargetMask,
3062 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003063 switch(Opc) {
3064 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003065 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003066 case X86ISD::PSHUFHW:
3067 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003068 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003069 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003070 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3071 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003072}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003073
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003074static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003075 SDValue V1, SDValue V2, unsigned TargetMask,
3076 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003077 switch(Opc) {
3078 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003079 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003080 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003081 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003082 return DAG.getNode(Opc, dl, VT, V1, V2,
3083 DAG.getConstant(TargetMask, MVT::i8));
3084 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003085}
3086
3087static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3088 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3089 switch(Opc) {
3090 default: llvm_unreachable("Unknown x86 shuffle node");
3091 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003092 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003093 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003094 case X86ISD::MOVLPS:
3095 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003096 case X86ISD::MOVSS:
3097 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003098 case X86ISD::UNPCKL:
3099 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003100 return DAG.getNode(Opc, dl, VT, V1, V2);
3101 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003102}
3103
Dan Gohmand858e902010-04-17 15:26:15 +00003104SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003105 MachineFunction &MF = DAG.getMachineFunction();
3106 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3107 int ReturnAddrIndex = FuncInfo->getRAIndex();
3108
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003109 if (ReturnAddrIndex == 0) {
3110 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003111 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003112 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003113 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003114 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003115 }
3116
Evan Cheng25ab6902006-09-08 06:48:29 +00003117 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003118}
3119
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003120bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3121 bool hasSymbolicDisplacement) {
3122 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003123 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003124 return false;
3125
3126 // If we don't have a symbolic displacement - we don't have any extra
3127 // restrictions.
3128 if (!hasSymbolicDisplacement)
3129 return true;
3130
3131 // FIXME: Some tweaks might be needed for medium code model.
3132 if (M != CodeModel::Small && M != CodeModel::Kernel)
3133 return false;
3134
3135 // For small code model we assume that latest object is 16MB before end of 31
3136 // bits boundary. We may also accept pretty large negative constants knowing
3137 // that all objects are in the positive half of address space.
3138 if (M == CodeModel::Small && Offset < 16*1024*1024)
3139 return true;
3140
3141 // For kernel code model we know that all object resist in the negative half
3142 // of 32bits address space. We may not accept negative offsets, since they may
3143 // be just off and we may accept pretty large positive ones.
3144 if (M == CodeModel::Kernel && Offset > 0)
3145 return true;
3146
3147 return false;
3148}
3149
Evan Chengef41ff62011-06-23 17:54:54 +00003150/// isCalleePop - Determines whether the callee is required to pop its
3151/// own arguments. Callee pop is necessary to support tail calls.
3152bool X86::isCalleePop(CallingConv::ID CallingConv,
3153 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3154 if (IsVarArg)
3155 return false;
3156
3157 switch (CallingConv) {
3158 default:
3159 return false;
3160 case CallingConv::X86_StdCall:
3161 return !is64Bit;
3162 case CallingConv::X86_FastCall:
3163 return !is64Bit;
3164 case CallingConv::X86_ThisCall:
3165 return !is64Bit;
3166 case CallingConv::Fast:
3167 return TailCallOpt;
3168 case CallingConv::GHC:
3169 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003170 case CallingConv::HiPE:
3171 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003172 }
3173}
3174
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003175/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3176/// specific condition code, returning the condition code and the LHS/RHS of the
3177/// comparison to make.
3178static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3179 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003180 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003181 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3182 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3183 // X > -1 -> X == 0, jump !sign.
3184 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003185 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003186 }
3187 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003188 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003189 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003190 }
3191 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003192 // X < 1 -> X <= 0
3193 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003194 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003195 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003196 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003197
Evan Chengd9558e02006-01-06 00:43:03 +00003198 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003199 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003200 case ISD::SETEQ: return X86::COND_E;
3201 case ISD::SETGT: return X86::COND_G;
3202 case ISD::SETGE: return X86::COND_GE;
3203 case ISD::SETLT: return X86::COND_L;
3204 case ISD::SETLE: return X86::COND_LE;
3205 case ISD::SETNE: return X86::COND_NE;
3206 case ISD::SETULT: return X86::COND_B;
3207 case ISD::SETUGT: return X86::COND_A;
3208 case ISD::SETULE: return X86::COND_BE;
3209 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003210 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003211 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003212
Chris Lattner4c78e022008-12-23 23:42:27 +00003213 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003214
Chris Lattner4c78e022008-12-23 23:42:27 +00003215 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003216 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3217 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003218 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3219 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003220 }
3221
Chris Lattner4c78e022008-12-23 23:42:27 +00003222 switch (SetCCOpcode) {
3223 default: break;
3224 case ISD::SETOLT:
3225 case ISD::SETOLE:
3226 case ISD::SETUGT:
3227 case ISD::SETUGE:
3228 std::swap(LHS, RHS);
3229 break;
3230 }
3231
3232 // On a floating point condition, the flags are set as follows:
3233 // ZF PF CF op
3234 // 0 | 0 | 0 | X > Y
3235 // 0 | 0 | 1 | X < Y
3236 // 1 | 0 | 0 | X == Y
3237 // 1 | 1 | 1 | unordered
3238 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003239 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003240 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003241 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003242 case ISD::SETOLT: // flipped
3243 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003244 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003245 case ISD::SETOLE: // flipped
3246 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003247 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003248 case ISD::SETUGT: // flipped
3249 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003250 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003251 case ISD::SETUGE: // flipped
3252 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003253 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003254 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003255 case ISD::SETNE: return X86::COND_NE;
3256 case ISD::SETUO: return X86::COND_P;
3257 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003258 case ISD::SETOEQ:
3259 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003260 }
Evan Chengd9558e02006-01-06 00:43:03 +00003261}
3262
Evan Cheng4a460802006-01-11 00:33:36 +00003263/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3264/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003265/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003266static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003267 switch (X86CC) {
3268 default:
3269 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003270 case X86::COND_B:
3271 case X86::COND_BE:
3272 case X86::COND_E:
3273 case X86::COND_P:
3274 case X86::COND_A:
3275 case X86::COND_AE:
3276 case X86::COND_NE:
3277 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003278 return true;
3279 }
3280}
3281
Evan Chengeb2f9692009-10-27 19:56:55 +00003282/// isFPImmLegal - Returns true if the target can instruction select the
3283/// specified FP immediate natively. If false, the legalizer will
3284/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003285bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003286 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3287 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3288 return true;
3289 }
3290 return false;
3291}
3292
Nate Begeman9008ca62009-04-27 18:41:29 +00003293/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3294/// the specified range (L, H].
3295static bool isUndefOrInRange(int Val, int Low, int Hi) {
3296 return (Val < 0) || (Val >= Low && Val < Hi);
3297}
3298
3299/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3300/// specified value.
3301static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003302 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003303}
3304
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003305/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003306/// from position Pos and ending in Pos+Size, falls within the specified
3307/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003308static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003309 unsigned Pos, unsigned Size, int Low) {
3310 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003311 if (!isUndefOrEqual(Mask[i], Low))
3312 return false;
3313 return true;
3314}
3315
Nate Begeman9008ca62009-04-27 18:41:29 +00003316/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3317/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3318/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003319static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003320 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003322 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 return (Mask[0] < 2 && Mask[1] < 2);
3324 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003325}
3326
Nate Begeman9008ca62009-04-27 18:41:29 +00003327/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3328/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003329static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3330 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003331 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003332
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003334 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3335 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003336
Evan Cheng506d3df2006-03-29 23:07:14 +00003337 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003338 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003339 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003340 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003341
Craig Toppera9a568a2012-05-02 08:03:44 +00003342 if (VT == MVT::v16i16) {
3343 // Lower quadword copied in order or undef.
3344 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3345 return false;
3346
3347 // Upper quadword shuffled.
3348 for (unsigned i = 12; i != 16; ++i)
3349 if (!isUndefOrInRange(Mask[i], 12, 16))
3350 return false;
3351 }
3352
Evan Cheng506d3df2006-03-29 23:07:14 +00003353 return true;
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3357/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003358static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3359 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003360 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003361
Rafael Espindola15684b22009-04-24 12:40:33 +00003362 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003363 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3364 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Rafael Espindola15684b22009-04-24 12:40:33 +00003366 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003367 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003368 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003369 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003370
Craig Toppera9a568a2012-05-02 08:03:44 +00003371 if (VT == MVT::v16i16) {
3372 // Upper quadword copied in order.
3373 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3374 return false;
3375
3376 // Lower quadword shuffled.
3377 for (unsigned i = 8; i != 12; ++i)
3378 if (!isUndefOrInRange(Mask[i], 8, 12))
3379 return false;
3380 }
3381
Rafael Espindola15684b22009-04-24 12:40:33 +00003382 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003383}
3384
Nate Begemana09008b2009-10-19 02:17:23 +00003385/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3386/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003387static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3388 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003389 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3390 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003391 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003392
Craig Topper0e2037b2012-01-20 05:53:00 +00003393 unsigned NumElts = VT.getVectorNumElements();
3394 unsigned NumLanes = VT.getSizeInBits()/128;
3395 unsigned NumLaneElts = NumElts/NumLanes;
3396
3397 // Do not handle 64-bit element shuffles with palignr.
3398 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003399 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003400
Craig Topper0e2037b2012-01-20 05:53:00 +00003401 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3402 unsigned i;
3403 for (i = 0; i != NumLaneElts; ++i) {
3404 if (Mask[i+l] >= 0)
3405 break;
3406 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003407
Craig Topper0e2037b2012-01-20 05:53:00 +00003408 // Lane is all undef, go to next lane
3409 if (i == NumLaneElts)
3410 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003411
Craig Topper0e2037b2012-01-20 05:53:00 +00003412 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003413
Craig Topper0e2037b2012-01-20 05:53:00 +00003414 // Make sure its in this lane in one of the sources
3415 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3416 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003417 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003418
3419 // If not lane 0, then we must match lane 0
3420 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3421 return false;
3422
3423 // Correct second source to be contiguous with first source
3424 if (Start >= (int)NumElts)
3425 Start -= NumElts - NumLaneElts;
3426
3427 // Make sure we're shifting in the right direction.
3428 if (Start <= (int)(i+l))
3429 return false;
3430
3431 Start -= i;
3432
3433 // Check the rest of the elements to see if they are consecutive.
3434 for (++i; i != NumLaneElts; ++i) {
3435 int Idx = Mask[i+l];
3436
3437 // Make sure its in this lane
3438 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3439 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3440 return false;
3441
3442 // If not lane 0, then we must match lane 0
3443 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3444 return false;
3445
3446 if (Idx >= (int)NumElts)
3447 Idx -= NumElts - NumLaneElts;
3448
3449 if (!isUndefOrEqual(Idx, Start+i))
3450 return false;
3451
3452 }
Nate Begemana09008b2009-10-19 02:17:23 +00003453 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003454
Nate Begemana09008b2009-10-19 02:17:23 +00003455 return true;
3456}
3457
Craig Topper1a7700a2012-01-19 08:19:12 +00003458/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3459/// the two vector operands have swapped position.
3460static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3461 unsigned NumElems) {
3462 for (unsigned i = 0; i != NumElems; ++i) {
3463 int idx = Mask[i];
3464 if (idx < 0)
3465 continue;
3466 else if (idx < (int)NumElems)
3467 Mask[i] = idx + NumElems;
3468 else
3469 Mask[i] = idx - NumElems;
3470 }
3471}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003472
Craig Topper1a7700a2012-01-19 08:19:12 +00003473/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3474/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3475/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3476/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003477static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003478 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003479 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003480 return false;
3481
Craig Topper1a7700a2012-01-19 08:19:12 +00003482 unsigned NumElems = VT.getVectorNumElements();
3483 unsigned NumLanes = VT.getSizeInBits()/128;
3484 unsigned NumLaneElems = NumElems/NumLanes;
3485
3486 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003487 return false;
3488
3489 // VSHUFPSY divides the resulting vector into 4 chunks.
3490 // The sources are also splitted into 4 chunks, and each destination
3491 // chunk must come from a different source chunk.
3492 //
3493 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3494 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3495 //
3496 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3497 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3498 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003499 // VSHUFPDY divides the resulting vector into 4 chunks.
3500 // The sources are also splitted into 4 chunks, and each destination
3501 // chunk must come from a different source chunk.
3502 //
3503 // SRC1 => X3 X2 X1 X0
3504 // SRC2 => Y3 Y2 Y1 Y0
3505 //
3506 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3507 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003508 unsigned HalfLaneElems = NumLaneElems/2;
3509 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3510 for (unsigned i = 0; i != NumLaneElems; ++i) {
3511 int Idx = Mask[i+l];
3512 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3513 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3514 return false;
3515 // For VSHUFPSY, the mask of the second half must be the same as the
3516 // first but with the appropriate offsets. This works in the same way as
3517 // VPERMILPS works with masks.
3518 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3519 continue;
3520 if (!isUndefOrEqual(Idx, Mask[i]+l))
3521 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003522 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003523 }
3524
3525 return true;
3526}
3527
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003528/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3529/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003530static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003531 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003532 return false;
3533
Craig Topper7a9a28b2012-08-12 02:23:29 +00003534 unsigned NumElems = VT.getVectorNumElements();
3535
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003536 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003537 return false;
3538
Evan Cheng2064a2b2006-03-28 06:50:32 +00003539 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003540 return isUndefOrEqual(Mask[0], 6) &&
3541 isUndefOrEqual(Mask[1], 7) &&
3542 isUndefOrEqual(Mask[2], 2) &&
3543 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003544}
3545
Nate Begeman0b10b912009-11-07 23:17:15 +00003546/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3547/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3548/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003549static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003550 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003551 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003552
Craig Topper7a9a28b2012-08-12 02:23:29 +00003553 unsigned NumElems = VT.getVectorNumElements();
3554
Nate Begeman0b10b912009-11-07 23:17:15 +00003555 if (NumElems != 4)
3556 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003557
Craig Topperdd637ae2012-02-19 05:41:45 +00003558 return isUndefOrEqual(Mask[0], 2) &&
3559 isUndefOrEqual(Mask[1], 3) &&
3560 isUndefOrEqual(Mask[2], 2) &&
3561 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003562}
3563
Evan Cheng5ced1d82006-04-06 23:23:56 +00003564/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3565/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003566static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003567 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003568 return false;
3569
Craig Topperdd637ae2012-02-19 05:41:45 +00003570 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003571
Evan Cheng5ced1d82006-04-06 23:23:56 +00003572 if (NumElems != 2 && NumElems != 4)
3573 return false;
3574
Chad Rosier238ae312012-04-30 17:47:15 +00003575 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003576 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003577 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003578
Chad Rosier238ae312012-04-30 17:47:15 +00003579 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003580 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003581 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003582
3583 return true;
3584}
3585
Nate Begeman0b10b912009-11-07 23:17:15 +00003586/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3587/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003588static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003589 if (!VT.is128BitVector())
3590 return false;
3591
Craig Topperdd637ae2012-02-19 05:41:45 +00003592 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003593
Craig Topper7a9a28b2012-08-12 02:23:29 +00003594 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003595 return false;
3596
Chad Rosier238ae312012-04-30 17:47:15 +00003597 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003598 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003599 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003600
Chad Rosier238ae312012-04-30 17:47:15 +00003601 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3602 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003603 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003604
3605 return true;
3606}
3607
Elena Demikhovsky15963732012-06-26 08:04:10 +00003608//
3609// Some special combinations that can be optimized.
3610//
3611static
3612SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3613 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003614 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003615 DebugLoc dl = SVOp->getDebugLoc();
3616
3617 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3618 return SDValue();
3619
3620 ArrayRef<int> Mask = SVOp->getMask();
3621
3622 // These are the special masks that may be optimized.
3623 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3624 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3625 bool MatchEvenMask = true;
3626 bool MatchOddMask = true;
3627 for (int i=0; i<8; ++i) {
3628 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3629 MatchEvenMask = false;
3630 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3631 MatchOddMask = false;
3632 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003633
Elena Demikhovsky32510202012-09-04 12:49:02 +00003634 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003635 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003636
Elena Demikhovsky15963732012-06-26 08:04:10 +00003637 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3638
Elena Demikhovsky32510202012-09-04 12:49:02 +00003639 SDValue Op0 = SVOp->getOperand(0);
3640 SDValue Op1 = SVOp->getOperand(1);
3641
3642 if (MatchEvenMask) {
3643 // Shift the second operand right to 32 bits.
3644 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3645 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3646 } else {
3647 // Shift the first operand left to 32 bits.
3648 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3649 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3650 }
3651 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3652 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003653}
3654
Evan Cheng0038e592006-03-28 00:39:58 +00003655/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3656/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003657static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003658 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003659 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003660
3661 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3662 "Unsupported vector type for unpckh");
3663
Craig Topper5a529e42013-01-18 06:44:29 +00003664 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003665 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003666 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003668 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3669 // independently on 128-bit lanes.
3670 unsigned NumLanes = VT.getSizeInBits()/128;
3671 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003672
Craig Topper94438ba2011-12-16 08:06:31 +00003673 for (unsigned l = 0; l != NumLanes; ++l) {
3674 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3675 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003676 i += 2, ++j) {
3677 int BitI = Mask[i];
3678 int BitI1 = Mask[i+1];
3679 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003680 return false;
David Greenea20244d2011-03-02 17:23:43 +00003681 if (V2IsSplat) {
3682 if (!isUndefOrEqual(BitI1, NumElts))
3683 return false;
3684 } else {
3685 if (!isUndefOrEqual(BitI1, j + NumElts))
3686 return false;
3687 }
Evan Cheng39623da2006-04-20 08:58:49 +00003688 }
Evan Cheng0038e592006-03-28 00:39:58 +00003689 }
David Greenea20244d2011-03-02 17:23:43 +00003690
Evan Cheng0038e592006-03-28 00:39:58 +00003691 return true;
3692}
3693
Evan Cheng4fcb9222006-03-28 02:43:26 +00003694/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3695/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003696static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003697 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003698 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003699
3700 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3701 "Unsupported vector type for unpckh");
3702
Craig Topper5a529e42013-01-18 06:44:29 +00003703 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003704 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003705 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003706
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003707 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3708 // independently on 128-bit lanes.
3709 unsigned NumLanes = VT.getSizeInBits()/128;
3710 unsigned NumLaneElts = NumElts/NumLanes;
3711
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003712 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003713 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3714 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003715 int BitI = Mask[i];
3716 int BitI1 = Mask[i+1];
3717 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003718 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003719 if (V2IsSplat) {
3720 if (isUndefOrEqual(BitI1, NumElts))
3721 return false;
3722 } else {
3723 if (!isUndefOrEqual(BitI1, j+NumElts))
3724 return false;
3725 }
Evan Cheng39623da2006-04-20 08:58:49 +00003726 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003727 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003728 return true;
3729}
3730
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003731/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3732/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3733/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003734static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003735 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003736 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003737
3738 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3739 "Unsupported vector type for unpckh");
3740
Craig Topper5a529e42013-01-18 06:44:29 +00003741 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003742 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003743 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003744
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003745 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3746 // FIXME: Need a better way to get rid of this, there's no latency difference
3747 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3748 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003749 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003750 return false;
3751
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003752 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3753 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003754 unsigned NumLanes = VT.getSizeInBits()/128;
3755 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003756
Craig Topper94438ba2011-12-16 08:06:31 +00003757 for (unsigned l = 0; l != NumLanes; ++l) {
3758 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3759 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003760 i += 2, ++j) {
3761 int BitI = Mask[i];
3762 int BitI1 = Mask[i+1];
3763
3764 if (!isUndefOrEqual(BitI, j))
3765 return false;
3766 if (!isUndefOrEqual(BitI1, j))
3767 return false;
3768 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003769 }
David Greenea20244d2011-03-02 17:23:43 +00003770
Rafael Espindola15684b22009-04-24 12:40:33 +00003771 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003772}
3773
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003774/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3775/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3776/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003777static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003778 unsigned NumElts = VT.getVectorNumElements();
3779
3780 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3781 "Unsupported vector type for unpckh");
3782
Craig Topper5a529e42013-01-18 06:44:29 +00003783 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003784 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003785 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003786
Craig Topper94438ba2011-12-16 08:06:31 +00003787 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3788 // independently on 128-bit lanes.
3789 unsigned NumLanes = VT.getSizeInBits()/128;
3790 unsigned NumLaneElts = NumElts/NumLanes;
3791
3792 for (unsigned l = 0; l != NumLanes; ++l) {
3793 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3794 i != (l+1)*NumLaneElts; i += 2, ++j) {
3795 int BitI = Mask[i];
3796 int BitI1 = Mask[i+1];
3797 if (!isUndefOrEqual(BitI, j))
3798 return false;
3799 if (!isUndefOrEqual(BitI1, j))
3800 return false;
3801 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003802 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003803 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003804}
3805
Evan Cheng017dcc62006-04-21 01:05:10 +00003806/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3807/// specifies a shuffle of elements that is suitable for input to MOVSS,
3808/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003809static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003810 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003811 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003812 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003813 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003814
Craig Topperc612d792012-01-02 09:17:37 +00003815 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003816
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003818 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003819
Craig Topperc612d792012-01-02 09:17:37 +00003820 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003822 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003823
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003824 return true;
3825}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003826
Craig Topper70b883b2011-11-28 10:14:51 +00003827/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003828/// as permutations between 128-bit chunks or halves. As an example: this
3829/// shuffle bellow:
3830/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3831/// The first half comes from the second half of V1 and the second half from the
3832/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003833static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3834 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003835 return false;
3836
3837 // The shuffle result is divided into half A and half B. In total the two
3838 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3839 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003840 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003841 bool MatchA = false, MatchB = false;
3842
3843 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003844 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003845 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3846 MatchA = true;
3847 break;
3848 }
3849 }
3850
3851 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003852 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003853 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3854 MatchB = true;
3855 break;
3856 }
3857 }
3858
3859 return MatchA && MatchB;
3860}
3861
Craig Topper70b883b2011-11-28 10:14:51 +00003862/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3863/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003864static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003865 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003866
Craig Topperc612d792012-01-02 09:17:37 +00003867 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003868
Craig Topperc612d792012-01-02 09:17:37 +00003869 unsigned FstHalf = 0, SndHalf = 0;
3870 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003871 if (SVOp->getMaskElt(i) > 0) {
3872 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3873 break;
3874 }
3875 }
Craig Topperc612d792012-01-02 09:17:37 +00003876 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003877 if (SVOp->getMaskElt(i) > 0) {
3878 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3879 break;
3880 }
3881 }
3882
3883 return (FstHalf | (SndHalf << 4));
3884}
3885
Craig Topper70b883b2011-11-28 10:14:51 +00003886/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003887/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3888/// Note that VPERMIL mask matching is different depending whether theunderlying
3889/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3890/// to the same elements of the low, but to the higher half of the source.
3891/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003892/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003893static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3894 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003895 return false;
3896
Craig Topperc612d792012-01-02 09:17:37 +00003897 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003898 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003899 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003900 return false;
3901
Craig Topperc612d792012-01-02 09:17:37 +00003902 unsigned NumLanes = VT.getSizeInBits()/128;
3903 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003904 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003905 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003906 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003907 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003908 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003909 continue;
3910 // VPERMILPS handling
3911 if (Mask[i] < 0)
3912 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003913 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003914 return false;
3915 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003916 }
3917
3918 return true;
3919}
3920
Craig Topper5aaffa82012-02-19 02:53:47 +00003921/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003922/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003923/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003924static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003926 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003927 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003928
3929 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003930 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003931 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003932
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003934 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003935
Craig Topperc612d792012-01-02 09:17:37 +00003936 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3938 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3939 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003940 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003941
Evan Cheng39623da2006-04-20 08:58:49 +00003942 return true;
3943}
3944
Evan Chengd9539472006-04-14 21:59:03 +00003945/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3946/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003947/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003948static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003949 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003950 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003951 return false;
3952
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003953 unsigned NumElems = VT.getVectorNumElements();
3954
Craig Topper5a529e42013-01-18 06:44:29 +00003955 if ((VT.is128BitVector() && NumElems != 4) ||
3956 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003957 return false;
3958
3959 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003960 for (unsigned i = 0; i != NumElems; i += 2)
3961 if (!isUndefOrEqual(Mask[i], i+1) ||
3962 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003964
3965 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003966}
3967
3968/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3969/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003970/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003971static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003972 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003973 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003974 return false;
3975
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003976 unsigned NumElems = VT.getVectorNumElements();
3977
Craig Topper5a529e42013-01-18 06:44:29 +00003978 if ((VT.is128BitVector() && NumElems != 4) ||
3979 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003980 return false;
3981
3982 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003983 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003984 if (!isUndefOrEqual(Mask[i], i) ||
3985 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003987
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003988 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003989}
3990
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003991/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3992/// specifies a shuffle of elements that is suitable for input to 256-bit
3993/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003994static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3995 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003996 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003997
Craig Topper7a9a28b2012-08-12 02:23:29 +00003998 unsigned NumElts = VT.getVectorNumElements();
3999 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004000 return false;
4001
Craig Topperc612d792012-01-02 09:17:37 +00004002 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004003 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004004 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004005 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004006 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004007 return false;
4008 return true;
4009}
4010
Evan Cheng0b457f02008-09-25 20:50:48 +00004011/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004012/// specifies a shuffle of elements that is suitable for input to 128-bit
4013/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004014static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004015 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004016 return false;
4017
Craig Topperc612d792012-01-02 09:17:37 +00004018 unsigned e = VT.getVectorNumElements() / 2;
4019 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004020 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004021 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004022 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004023 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004024 return false;
4025 return true;
4026}
4027
David Greenec38a03e2011-02-03 15:50:00 +00004028/// isVEXTRACTF128Index - Return true if the specified
4029/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4030/// suitable for input to VEXTRACTF128.
4031bool X86::isVEXTRACTF128Index(SDNode *N) {
4032 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4033 return false;
4034
4035 // The index should be aligned on a 128-bit boundary.
4036 uint64_t Index =
4037 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4038
Craig Topper5141d972013-01-18 08:41:28 +00004039 MVT VT = N->getValueType(0).getSimpleVT();
4040 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004041 bool Result = (Index * ElSize) % 128 == 0;
4042
4043 return Result;
4044}
4045
David Greeneccacdc12011-02-04 16:08:29 +00004046/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4047/// operand specifies a subvector insert that is suitable for input to
4048/// VINSERTF128.
4049bool X86::isVINSERTF128Index(SDNode *N) {
4050 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4051 return false;
4052
4053 // The index should be aligned on a 128-bit boundary.
4054 uint64_t Index =
4055 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4056
Craig Topper5141d972013-01-18 08:41:28 +00004057 MVT VT = N->getValueType(0).getSimpleVT();
4058 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004059 bool Result = (Index * ElSize) % 128 == 0;
4060
4061 return Result;
4062}
4063
Evan Cheng63d33002006-03-22 08:01:21 +00004064/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004065/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004066/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004067static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004068 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004069
Craig Topper1a7700a2012-01-19 08:19:12 +00004070 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4071 "Unsupported vector type for PSHUF/SHUFP");
4072
4073 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4074 // independently on 128-bit lanes.
4075 unsigned NumElts = VT.getVectorNumElements();
4076 unsigned NumLanes = VT.getSizeInBits()/128;
4077 unsigned NumLaneElts = NumElts/NumLanes;
4078
4079 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4080 "Only supports 2 or 4 elements per lane");
4081
4082 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004083 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004084 for (unsigned i = 0; i != NumElts; ++i) {
4085 int Elt = N->getMaskElt(i);
4086 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004087 Elt &= NumLaneElts - 1;
4088 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004089 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004090 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004091
Evan Cheng63d33002006-03-22 08:01:21 +00004092 return Mask;
4093}
4094
Evan Cheng506d3df2006-03-29 23:07:14 +00004095/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004096/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004097static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004098 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004099
4100 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4101 "Unsupported vector type for PSHUFHW");
4102
4103 unsigned NumElts = VT.getVectorNumElements();
4104
Evan Cheng506d3df2006-03-29 23:07:14 +00004105 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004106 for (unsigned l = 0; l != NumElts; l += 8) {
4107 // 8 nodes per lane, but we only care about the last 4.
4108 for (unsigned i = 0; i < 4; ++i) {
4109 int Elt = N->getMaskElt(l+i+4);
4110 if (Elt < 0) continue;
4111 Elt &= 0x3; // only 2-bits.
4112 Mask |= Elt << (i * 2);
4113 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004114 }
Craig Topper6b28d352012-05-03 07:12:59 +00004115
Evan Cheng506d3df2006-03-29 23:07:14 +00004116 return Mask;
4117}
4118
4119/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004120/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004121static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004122 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004123
4124 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4125 "Unsupported vector type for PSHUFHW");
4126
4127 unsigned NumElts = VT.getVectorNumElements();
4128
Evan Cheng506d3df2006-03-29 23:07:14 +00004129 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004130 for (unsigned l = 0; l != NumElts; l += 8) {
4131 // 8 nodes per lane, but we only care about the first 4.
4132 for (unsigned i = 0; i < 4; ++i) {
4133 int Elt = N->getMaskElt(l+i);
4134 if (Elt < 0) continue;
4135 Elt &= 0x3; // only 2-bits
4136 Mask |= Elt << (i * 2);
4137 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004138 }
Craig Topper6b28d352012-05-03 07:12:59 +00004139
Evan Cheng506d3df2006-03-29 23:07:14 +00004140 return Mask;
4141}
4142
Nate Begemana09008b2009-10-19 02:17:23 +00004143/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4144/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004145static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004146 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004147 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004148
Craig Topper0e2037b2012-01-20 05:53:00 +00004149 unsigned NumElts = VT.getVectorNumElements();
4150 unsigned NumLanes = VT.getSizeInBits()/128;
4151 unsigned NumLaneElts = NumElts/NumLanes;
4152
4153 int Val = 0;
4154 unsigned i;
4155 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004156 Val = SVOp->getMaskElt(i);
4157 if (Val >= 0)
4158 break;
4159 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004160 if (Val >= (int)NumElts)
4161 Val -= NumElts - NumLaneElts;
4162
Eli Friedman63f8dde2011-07-25 21:36:45 +00004163 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004164 return (Val - i) * EltSize;
4165}
4166
David Greenec38a03e2011-02-03 15:50:00 +00004167/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4168/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4169/// instructions.
4170unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4171 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4172 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4173
4174 uint64_t Index =
4175 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4176
Craig Toppercfcab212013-01-19 08:27:45 +00004177 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4178 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004179
4180 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004181 return Index / NumElemsPerChunk;
4182}
4183
David Greeneccacdc12011-02-04 16:08:29 +00004184/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4185/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4186/// instructions.
4187unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4188 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4189 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4190
4191 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004192 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004193
Craig Toppercfcab212013-01-19 08:27:45 +00004194 MVT VecVT = N->getValueType(0).getSimpleVT();
4195 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004196
4197 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004198 return Index / NumElemsPerChunk;
4199}
4200
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004201/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4202/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4203/// Handles 256-bit.
4204static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004205 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004206
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004207 unsigned NumElts = VT.getVectorNumElements();
4208
Craig Topper095c5282012-04-15 23:48:57 +00004209 assert((VT.is256BitVector() && NumElts == 4) &&
4210 "Unsupported vector type for VPERMQ/VPERMPD");
4211
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004212 unsigned Mask = 0;
4213 for (unsigned i = 0; i != NumElts; ++i) {
4214 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004215 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004216 continue;
4217 Mask |= Elt << (i*2);
4218 }
4219
4220 return Mask;
4221}
Evan Cheng37b73872009-07-30 08:33:02 +00004222/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4223/// constant +0.0.
4224bool X86::isZeroNode(SDValue Elt) {
4225 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004226 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004227 (isa<ConstantFPSDNode>(Elt) &&
4228 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4229}
4230
Nate Begeman9008ca62009-04-27 18:41:29 +00004231/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4232/// their permute mask.
4233static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4234 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004235 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004236 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004240 int Idx = SVOp->getMaskElt(i);
4241 if (Idx >= 0) {
4242 if (Idx < (int)NumElems)
4243 Idx += NumElems;
4244 else
4245 Idx -= NumElems;
4246 }
4247 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004248 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4250 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251}
4252
Evan Cheng533a0aa2006-04-19 20:35:22 +00004253/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4254/// match movhlps. The lower half elements should come from upper half of
4255/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004256/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004257static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004258 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004259 return false;
4260 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004261 return false;
4262 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004263 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004264 return false;
4265 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004266 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004267 return false;
4268 return true;
4269}
4270
Evan Cheng5ced1d82006-04-06 23:23:56 +00004271/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004272/// is promoted to a vector. It also returns the LoadSDNode by reference if
4273/// required.
4274static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004275 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4276 return false;
4277 N = N->getOperand(0).getNode();
4278 if (!ISD::isNON_EXTLoad(N))
4279 return false;
4280 if (LD)
4281 *LD = cast<LoadSDNode>(N);
4282 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004283}
4284
Dan Gohman65fd6562011-11-03 21:49:52 +00004285// Test whether the given value is a vector value which will be legalized
4286// into a load.
4287static bool WillBeConstantPoolLoad(SDNode *N) {
4288 if (N->getOpcode() != ISD::BUILD_VECTOR)
4289 return false;
4290
4291 // Check for any non-constant elements.
4292 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4293 switch (N->getOperand(i).getNode()->getOpcode()) {
4294 case ISD::UNDEF:
4295 case ISD::ConstantFP:
4296 case ISD::Constant:
4297 break;
4298 default:
4299 return false;
4300 }
4301
4302 // Vectors of all-zeros and all-ones are materialized with special
4303 // instructions rather than being loaded.
4304 return !ISD::isBuildVectorAllZeros(N) &&
4305 !ISD::isBuildVectorAllOnes(N);
4306}
4307
Evan Cheng533a0aa2006-04-19 20:35:22 +00004308/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4309/// match movlp{s|d}. The lower half elements should come from lower half of
4310/// V1 (and in order), and the upper half elements should come from the upper
4311/// half of V2 (and in order). And since V1 will become the source of the
4312/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004313static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004314 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004315 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004316 return false;
4317
Evan Cheng466685d2006-10-09 20:57:25 +00004318 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004319 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004320 // Is V2 is a vector load, don't do this transformation. We will try to use
4321 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004322 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004323 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004324
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004325 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004326
Evan Cheng533a0aa2006-04-19 20:35:22 +00004327 if (NumElems != 2 && NumElems != 4)
4328 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004329 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004330 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004331 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004332 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004333 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004334 return false;
4335 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004336}
4337
Evan Cheng39623da2006-04-20 08:58:49 +00004338/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4339/// all the same.
4340static bool isSplatVector(SDNode *N) {
4341 if (N->getOpcode() != ISD::BUILD_VECTOR)
4342 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004343
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004345 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4346 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004347 return false;
4348 return true;
4349}
4350
Evan Cheng213d2cf2007-05-17 18:45:50 +00004351/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004352/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004353/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004354static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004355 SDValue V1 = N->getOperand(0);
4356 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004357 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4358 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004360 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004362 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4363 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004364 if (Opc != ISD::BUILD_VECTOR ||
4365 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 return false;
4367 } else if (Idx >= 0) {
4368 unsigned Opc = V1.getOpcode();
4369 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4370 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004371 if (Opc != ISD::BUILD_VECTOR ||
4372 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004373 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004374 }
4375 }
4376 return true;
4377}
4378
4379/// getZeroVector - Returns a vector of specified type with all zero elements.
4380///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004381static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004382 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004384
Dale Johannesen0488fb62010-09-30 23:57:10 +00004385 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004386 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004387 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004388 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004389 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004390 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4391 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4392 } else { // SSE1
4393 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4394 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4395 }
Craig Topper5a529e42013-01-18 06:44:29 +00004396 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004397 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004398 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4399 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4400 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4401 } else {
4402 // 256-bit logic and arithmetic instructions in AVX are all
4403 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4404 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4405 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4406 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4407 }
Craig Topper9d352402012-04-23 07:24:41 +00004408 } else
4409 llvm_unreachable("Unexpected vector type");
4410
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004411 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004412}
4413
Chris Lattner8a594482007-11-25 00:24:49 +00004414/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004415/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4416/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4417/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004418static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004419 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004420 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004421
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004423 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004424 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004425 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004426 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4427 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4428 } else { // AVX
4429 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004430 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004431 }
Craig Topper5a529e42013-01-18 06:44:29 +00004432 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004433 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004434 } else
4435 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004437 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004438}
4439
Evan Cheng39623da2006-04-20 08:58:49 +00004440/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4441/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004442static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004443 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004444 if (Mask[i] > (int)NumElems) {
4445 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004446 }
Evan Cheng39623da2006-04-20 08:58:49 +00004447 }
Evan Cheng39623da2006-04-20 08:58:49 +00004448}
4449
Evan Cheng017dcc62006-04-21 01:05:10 +00004450/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4451/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004452static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 SDValue V2) {
4454 unsigned NumElems = VT.getVectorNumElements();
4455 SmallVector<int, 8> Mask;
4456 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004457 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 Mask.push_back(i);
4459 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004460}
4461
Nate Begeman9008ca62009-04-27 18:41:29 +00004462/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004463static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 SDValue V2) {
4465 unsigned NumElems = VT.getVectorNumElements();
4466 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004467 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 Mask.push_back(i);
4469 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004470 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004472}
4473
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004475static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 SDValue V2) {
4477 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004479 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 Mask.push_back(i + Half);
4481 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004482 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004483 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004484}
4485
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004486// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004487// a generic shuffle instruction because the target has no such instructions.
4488// Generate shuffles which repeat i16 and i8 several times until they can be
4489// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004490static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004494
Nate Begeman9008ca62009-04-27 18:41:29 +00004495 while (NumElems > 4) {
4496 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 EltNo -= NumElems/2;
4501 }
4502 NumElems >>= 1;
4503 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004504 return V;
4505}
Eric Christopherfd179292009-08-27 18:07:15 +00004506
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4508static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4509 EVT VT = V.getValueType();
4510 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511
Craig Topper5a529e42013-01-18 06:44:29 +00004512 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004513 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004514 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004515 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4516 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004517 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004518 // To use VPERMILPS to splat scalars, the second half of indicies must
4519 // refer to the higher part, which is a duplication of the lower one,
4520 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004521 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4522 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004523
4524 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4525 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4526 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004527 } else
4528 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529
4530 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4531}
4532
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004533/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004534static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4535 EVT SrcVT = SV->getValueType(0);
4536 SDValue V1 = SV->getOperand(0);
4537 DebugLoc dl = SV->getDebugLoc();
4538
4539 int EltNo = SV->getSplatIndex();
4540 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004541 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004542
Craig Topper5a529e42013-01-18 06:44:29 +00004543 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4544 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004545
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004546 // Extract the 128-bit part containing the splat element and update
4547 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004548 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004549 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4550 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004551 EltNo -= NumElems/2;
4552 }
4553
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004554 // All i16 and i8 vector types can't be used directly by a generic shuffle
4555 // instruction because the target has no such instruction. Generate shuffles
4556 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004557 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004558 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004559 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004560 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004561
4562 // Recreate the 256-bit vector and place the same 128-bit vector
4563 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004564 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004565 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004566 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004567 }
4568
4569 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004570}
4571
Evan Chengba05f722006-04-21 23:03:30 +00004572/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004573/// vector of zero or undef vector. This produces a shuffle where the low
4574/// element of V2 is swizzled into the zero/undef vector, landing at element
4575/// 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 +00004576static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004577 bool IsZero,
4578 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004579 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004580 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004581 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004582 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 unsigned NumElems = VT.getVectorNumElements();
4584 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004585 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004586 // If this is the insertion idx, put the low elt of V2 here.
4587 MaskVec.push_back(i == Idx ? NumElems : i);
4588 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004589}
4590
Craig Toppera1ffc682012-03-20 06:42:26 +00004591/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4592/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004593/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004594static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004595 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004596 unsigned NumElems = VT.getVectorNumElements();
4597 SDValue ImmN;
4598
Craig Topper89f4e662012-03-20 07:17:59 +00004599 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004600 switch(N->getOpcode()) {
4601 case X86ISD::SHUFP:
4602 ImmN = N->getOperand(N->getNumOperands()-1);
4603 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4604 break;
4605 case X86ISD::UNPCKH:
4606 DecodeUNPCKHMask(VT, Mask);
4607 break;
4608 case X86ISD::UNPCKL:
4609 DecodeUNPCKLMask(VT, Mask);
4610 break;
4611 case X86ISD::MOVHLPS:
4612 DecodeMOVHLPSMask(NumElems, Mask);
4613 break;
4614 case X86ISD::MOVLHPS:
4615 DecodeMOVLHPSMask(NumElems, Mask);
4616 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004617 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004618 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004619 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004620 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004621 case X86ISD::PSHUFD:
4622 case X86ISD::VPERMILP:
4623 ImmN = N->getOperand(N->getNumOperands()-1);
4624 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004625 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004626 break;
4627 case X86ISD::PSHUFHW:
4628 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004629 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004630 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004631 break;
4632 case X86ISD::PSHUFLW:
4633 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004634 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004635 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004636 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004637 case X86ISD::VPERMI:
4638 ImmN = N->getOperand(N->getNumOperands()-1);
4639 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4640 IsUnary = true;
4641 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004642 case X86ISD::MOVSS:
4643 case X86ISD::MOVSD: {
4644 // The index 0 always comes from the first element of the second source,
4645 // this is why MOVSS and MOVSD are used in the first place. The other
4646 // elements come from the other positions of the first source vector
4647 Mask.push_back(NumElems);
4648 for (unsigned i = 1; i != NumElems; ++i) {
4649 Mask.push_back(i);
4650 }
4651 break;
4652 }
4653 case X86ISD::VPERM2X128:
4654 ImmN = N->getOperand(N->getNumOperands()-1);
4655 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004656 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004657 break;
4658 case X86ISD::MOVDDUP:
4659 case X86ISD::MOVLHPD:
4660 case X86ISD::MOVLPD:
4661 case X86ISD::MOVLPS:
4662 case X86ISD::MOVSHDUP:
4663 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004664 // Not yet implemented
4665 return false;
4666 default: llvm_unreachable("unknown target shuffle node");
4667 }
4668
4669 return true;
4670}
4671
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4673/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004674static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004675 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004676 if (Depth == 6)
4677 return SDValue(); // Limit search depth.
4678
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004679 SDValue V = SDValue(N, 0);
4680 EVT VT = V.getValueType();
4681 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682
4683 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4684 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004685 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686
Craig Topper3d092db2012-03-21 02:14:01 +00004687 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004688 return DAG.getUNDEF(VT.getVectorElementType());
4689
Craig Topperd156dc12012-02-06 07:17:51 +00004690 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004691 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4692 : SV->getOperand(1);
4693 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004694 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695
4696 // Recurse into target specific vector shuffles to find scalars.
4697 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004698 MVT ShufVT = V.getValueType().getSimpleVT();
4699 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004700 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004701 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004702
Craig Topperd978c542012-05-06 19:46:21 +00004703 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004704 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004705
Craig Topper3d092db2012-03-21 02:14:01 +00004706 int Elt = ShuffleMask[Index];
4707 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004708 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004709
Craig Topper3d092db2012-03-21 02:14:01 +00004710 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004711 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004712 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004713 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004714 }
4715
4716 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004717 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718 V = V.getOperand(0);
4719 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004720 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004721
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004722 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723 return SDValue();
4724 }
4725
4726 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4727 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004728 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004729
4730 if (V.getOpcode() == ISD::BUILD_VECTOR)
4731 return V.getOperand(Index);
4732
4733 return SDValue();
4734}
4735
4736/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4737/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004738/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004739static
Craig Topper3d092db2012-03-21 02:14:01 +00004740unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004741 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004742 unsigned i;
4743 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004744 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004745 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 if (!(Elt.getNode() &&
4747 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4748 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004749 }
4750
4751 return i;
4752}
4753
Craig Topper3d092db2012-03-21 02:14:01 +00004754/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4755/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4757static
Craig Topper3d092db2012-03-21 02:14:01 +00004758bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4759 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4760 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004761 bool SeenV1 = false;
4762 bool SeenV2 = false;
4763
Craig Topper3d092db2012-03-21 02:14:01 +00004764 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004765 int Idx = SVOp->getMaskElt(i);
4766 // Ignore undef indicies
4767 if (Idx < 0)
4768 continue;
4769
Craig Topper3d092db2012-03-21 02:14:01 +00004770 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004771 SeenV1 = true;
4772 else
4773 SeenV2 = true;
4774
4775 // Only accept consecutive elements from the same vector
4776 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4777 return false;
4778 }
4779
4780 OpNum = SeenV1 ? 0 : 1;
4781 return true;
4782}
4783
4784/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4785/// logical left shift of a vector.
4786static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4787 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4788 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4789 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4790 false /* check zeros from right */, DAG);
4791 unsigned OpSrc;
4792
4793 if (!NumZeros)
4794 return false;
4795
4796 // Considering the elements in the mask that are not consecutive zeros,
4797 // check if they consecutively come from only one of the source vectors.
4798 //
4799 // V1 = {X, A, B, C} 0
4800 // \ \ \ /
4801 // vector_shuffle V1, V2 <1, 2, 3, X>
4802 //
4803 if (!isShuffleMaskConsecutive(SVOp,
4804 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004805 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004806 NumZeros, // Where to start looking in the src vector
4807 NumElems, // Number of elements in vector
4808 OpSrc)) // Which source operand ?
4809 return false;
4810
4811 isLeft = false;
4812 ShAmt = NumZeros;
4813 ShVal = SVOp->getOperand(OpSrc);
4814 return true;
4815}
4816
4817/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4818/// logical left shift of a vector.
4819static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4820 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4821 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4822 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4823 true /* check zeros from left */, DAG);
4824 unsigned OpSrc;
4825
4826 if (!NumZeros)
4827 return false;
4828
4829 // Considering the elements in the mask that are not consecutive zeros,
4830 // check if they consecutively come from only one of the source vectors.
4831 //
4832 // 0 { A, B, X, X } = V2
4833 // / \ / /
4834 // vector_shuffle V1, V2 <X, X, 4, 5>
4835 //
4836 if (!isShuffleMaskConsecutive(SVOp,
4837 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004838 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004839 0, // Where to start looking in the src vector
4840 NumElems, // Number of elements in vector
4841 OpSrc)) // Which source operand ?
4842 return false;
4843
4844 isLeft = true;
4845 ShAmt = NumZeros;
4846 ShVal = SVOp->getOperand(OpSrc);
4847 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004848}
4849
4850/// isVectorShift - Returns true if the shuffle can be implemented as a
4851/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004852static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004853 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004854 // Although the logic below support any bitwidth size, there are no
4855 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004856 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004857 return false;
4858
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004859 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4860 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4861 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004862
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004863 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004864}
4865
Evan Chengc78d3b42006-04-24 18:01:45 +00004866/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4867///
Dan Gohman475871a2008-07-27 21:46:04 +00004868static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004870 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004871 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004872 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004873 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004874 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004875
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004876 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004877 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004878 bool First = true;
4879 for (unsigned i = 0; i < 16; ++i) {
4880 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4881 if (ThisIsNonZero && First) {
4882 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004883 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004884 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004886 First = false;
4887 }
4888
4889 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004890 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004891 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4892 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004893 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004895 }
4896 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4898 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4899 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004900 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 } else
4903 ThisElt = LastElt;
4904
Gabor Greifba36cb52008-08-28 21:40:38 +00004905 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004906 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004907 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004908 }
4909 }
4910
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004911 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004912}
4913
Bill Wendlinga348c562007-03-22 18:42:45 +00004914/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004915///
Dan Gohman475871a2008-07-27 21:46:04 +00004916static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004917 unsigned NumNonZero, unsigned NumZero,
4918 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004919 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004920 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004921 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004922 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004923
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004924 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004925 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004926 bool First = true;
4927 for (unsigned i = 0; i < 8; ++i) {
4928 bool isNonZero = (NonZeros & (1 << i)) != 0;
4929 if (isNonZero) {
4930 if (First) {
4931 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004932 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004933 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004934 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004935 First = false;
4936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004937 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004939 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004940 }
4941 }
4942
4943 return V;
4944}
4945
Evan Chengf26ffe92008-05-29 08:22:04 +00004946/// getVShift - Return a vector logical shift node.
4947///
Owen Andersone50ed302009-08-10 22:56:29 +00004948static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004949 unsigned NumBits, SelectionDAG &DAG,
4950 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004951 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004952 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004953 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004954 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4955 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004956 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004957 DAG.getConstant(NumBits,
4958 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004959}
4960
Dan Gohman475871a2008-07-27 21:46:04 +00004961SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004962X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004963 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004964
Evan Chengc3630942009-12-09 21:00:30 +00004965 // Check if the scalar load can be widened into a vector load. And if
4966 // the address is "base + cst" see if the cst can be "absorbed" into
4967 // the shuffle mask.
4968 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4969 SDValue Ptr = LD->getBasePtr();
4970 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4971 return SDValue();
4972 EVT PVT = LD->getValueType(0);
4973 if (PVT != MVT::i32 && PVT != MVT::f32)
4974 return SDValue();
4975
4976 int FI = -1;
4977 int64_t Offset = 0;
4978 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4979 FI = FINode->getIndex();
4980 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004981 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004982 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4983 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4984 Offset = Ptr.getConstantOperandVal(1);
4985 Ptr = Ptr.getOperand(0);
4986 } else {
4987 return SDValue();
4988 }
4989
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 // FIXME: 256-bit vector instructions don't require a strict alignment,
4991 // improve this code to support it better.
4992 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004993 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004994 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004995 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004997 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004998 // Can't change the alignment. FIXME: It's possible to compute
4999 // the exact stack offset and reference FI + adjust offset instead.
5000 // If someone *really* cares about this. That's the way to implement it.
5001 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005002 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005003 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005004 }
5005 }
5006
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005007 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005008 // Ptr + (Offset & ~15).
5009 if (Offset < 0)
5010 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005011 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005012 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005013 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005014 if (StartOffset)
5015 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5016 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5017
5018 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005019 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005020
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005021 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5022 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005023 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005024 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005025
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005026 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005027 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005028 Mask.push_back(EltNo);
5029
Craig Toppercc3000632012-01-30 07:50:31 +00005030 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005031 }
5032
5033 return SDValue();
5034}
5035
Michael J. Spencerec38de22010-10-10 22:04:20 +00005036/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5037/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005038/// load which has the same value as a build_vector whose operands are 'elts'.
5039///
5040/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005041///
Nate Begeman1449f292010-03-24 22:19:06 +00005042/// FIXME: we'd also like to handle the case where the last elements are zero
5043/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5044/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005045static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005046 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005047 EVT EltVT = VT.getVectorElementType();
5048 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005049
Nate Begemanfdea31a2010-03-24 20:49:50 +00005050 LoadSDNode *LDBase = NULL;
5051 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005052
Nate Begeman1449f292010-03-24 22:19:06 +00005053 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005054 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005055 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005056 for (unsigned i = 0; i < NumElems; ++i) {
5057 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005058
Nate Begemanfdea31a2010-03-24 20:49:50 +00005059 if (!Elt.getNode() ||
5060 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5061 return SDValue();
5062 if (!LDBase) {
5063 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5064 return SDValue();
5065 LDBase = cast<LoadSDNode>(Elt.getNode());
5066 LastLoadedElt = i;
5067 continue;
5068 }
5069 if (Elt.getOpcode() == ISD::UNDEF)
5070 continue;
5071
5072 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5073 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5074 return SDValue();
5075 LastLoadedElt = i;
5076 }
Nate Begeman1449f292010-03-24 22:19:06 +00005077
5078 // If we have found an entire vector of loads and undefs, then return a large
5079 // load of the entire vector width starting at the base pointer. If we found
5080 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005081 if (LastLoadedElt == NumElems - 1) {
5082 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005083 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005084 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005085 LDBase->isVolatile(), LDBase->isNonTemporal(),
5086 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005087 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005088 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005089 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005090 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005091 }
5092 if (NumElems == 4 && LastLoadedElt == 1 &&
5093 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005094 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5095 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005096 SDValue ResNode =
5097 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5098 LDBase->getPointerInfo(),
5099 LDBase->getAlignment(),
5100 false/*isVolatile*/, true/*ReadMem*/,
5101 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005102
5103 // Make sure the newly-created LOAD is in the same position as LDBase in
5104 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5105 // update uses of LDBase's output chain to use the TokenFactor.
5106 if (LDBase->hasAnyUseOfValue(1)) {
5107 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5108 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5109 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5110 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5111 SDValue(ResNode.getNode(), 1));
5112 }
5113
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005114 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005115 }
5116 return SDValue();
5117}
5118
Nadav Rotem9d68b062012-04-08 12:54:54 +00005119/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5120/// to generate a splat value for the following cases:
5121/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005123/// a scalar load, or a constant.
5124/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005125/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005126SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005127X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005128 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005129 return SDValue();
5130
Craig Topper45e1c752013-01-20 00:38:18 +00005131 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005132 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005133
Craig Topper5da8a802012-05-04 05:49:51 +00005134 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5135 "Unsupported vector type for broadcast.");
5136
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005137 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005138 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005139
Nadav Rotem9d68b062012-04-08 12:54:54 +00005140 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005141 default:
5142 // Unknown pattern found.
5143 return SDValue();
5144
5145 case ISD::BUILD_VECTOR: {
5146 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005147 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148 return SDValue();
5149
Nadav Rotem9d68b062012-04-08 12:54:54 +00005150 Ld = Op.getOperand(0);
5151 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5152 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005153
5154 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005155 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005156 // Constants may have multiple users.
5157 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005158 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005159 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005160 }
5161
5162 case ISD::VECTOR_SHUFFLE: {
5163 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5164
5165 // Shuffles must have a splat mask where the first element is
5166 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005167 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005168 return SDValue();
5169
5170 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005171 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005172 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5173
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005174 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005175 return SDValue();
5176
5177 // Use the register form of the broadcast instruction available on AVX2.
5178 if (VT.is256BitVector())
5179 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5180 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5181 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005182
5183 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005184 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005185 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186
5187 // The scalar_to_vector node and the suspected
5188 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005189 // Constants may have multiple users.
5190 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005191 return SDValue();
5192 break;
5193 }
5194 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005195
Craig Topper7a9a28b2012-08-12 02:23:29 +00005196 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005197
5198 // Handle the broadcasting a single constant scalar from the constant pool
5199 // into a vector. On Sandybridge it is still better to load a constant vector
5200 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005201 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005202 EVT CVT = Ld.getValueType();
5203 assert(!CVT.isVector() && "Must not broadcast a vector type");
5204 unsigned ScalarSize = CVT.getSizeInBits();
5205
Craig Topper5da8a802012-05-04 05:49:51 +00005206 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005207 const Constant *C = 0;
5208 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5209 C = CI->getConstantIntValue();
5210 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5211 C = CF->getConstantFPValue();
5212
5213 assert(C && "Invalid constant type");
5214
Nadav Rotem154819d2012-04-09 07:45:58 +00005215 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005216 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005217 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005218 MachinePointerInfo::getConstantPool(),
5219 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005220
Nadav Rotem9d68b062012-04-08 12:54:54 +00005221 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5222 }
5223 }
5224
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005225 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005226 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5227
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005228 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005229 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005230 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5231 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5232
5233 // The scalar source must be a normal load.
5234 if (!IsLoad)
5235 return SDValue();
5236
Craig Topper5da8a802012-05-04 05:49:51 +00005237 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005238 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005239
Craig Toppera9376332012-01-10 08:23:59 +00005240 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005241 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005242 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005243 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005244 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005245 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005246
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005247 // Unsupported broadcast.
5248 return SDValue();
5249}
5250
Evan Chengc3630942009-12-09 21:00:30 +00005251SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005252X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5253 EVT VT = Op.getValueType();
5254
5255 // Skip if insert_vec_elt is not supported.
5256 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5257 return SDValue();
5258
5259 DebugLoc DL = Op.getDebugLoc();
5260 unsigned NumElems = Op.getNumOperands();
5261
5262 SDValue VecIn1;
5263 SDValue VecIn2;
5264 SmallVector<unsigned, 4> InsertIndices;
5265 SmallVector<int, 8> Mask(NumElems, -1);
5266
5267 for (unsigned i = 0; i != NumElems; ++i) {
5268 unsigned Opc = Op.getOperand(i).getOpcode();
5269
5270 if (Opc == ISD::UNDEF)
5271 continue;
5272
5273 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5274 // Quit if more than 1 elements need inserting.
5275 if (InsertIndices.size() > 1)
5276 return SDValue();
5277
5278 InsertIndices.push_back(i);
5279 continue;
5280 }
5281
5282 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5283 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5284
5285 // Quit if extracted from vector of different type.
5286 if (ExtractedFromVec.getValueType() != VT)
5287 return SDValue();
5288
5289 // Quit if non-constant index.
5290 if (!isa<ConstantSDNode>(ExtIdx))
5291 return SDValue();
5292
5293 if (VecIn1.getNode() == 0)
5294 VecIn1 = ExtractedFromVec;
5295 else if (VecIn1 != ExtractedFromVec) {
5296 if (VecIn2.getNode() == 0)
5297 VecIn2 = ExtractedFromVec;
5298 else if (VecIn2 != ExtractedFromVec)
5299 // Quit if more than 2 vectors to shuffle
5300 return SDValue();
5301 }
5302
5303 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5304
5305 if (ExtractedFromVec == VecIn1)
5306 Mask[i] = Idx;
5307 else if (ExtractedFromVec == VecIn2)
5308 Mask[i] = Idx + NumElems;
5309 }
5310
5311 if (VecIn1.getNode() == 0)
5312 return SDValue();
5313
5314 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5315 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5316 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5317 unsigned Idx = InsertIndices[i];
5318 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5319 DAG.getIntPtrConstant(Idx));
5320 }
5321
5322 return NV;
5323}
5324
5325SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005326X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005327 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005328
Craig Topper45e1c752013-01-20 00:38:18 +00005329 MVT VT = Op.getValueType().getSimpleVT();
5330 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005331 unsigned NumElems = Op.getNumOperands();
5332
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005333 // Vectors containing all zeros can be matched by pxor and xorps later
5334 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5335 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5336 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005337 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005338 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005340 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005341 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005343 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005344 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5345 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005346 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005347 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005348 return Op;
5349
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005350 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005351 }
5352
Nadav Rotem154819d2012-04-09 07:45:58 +00005353 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005354 if (Broadcast.getNode())
5355 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005356
Owen Andersone50ed302009-08-10 22:56:29 +00005357 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358
Evan Cheng0db9fe62006-04-25 20:13:52 +00005359 unsigned NumZero = 0;
5360 unsigned NumNonZero = 0;
5361 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005362 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005363 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005364 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005365 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005366 if (Elt.getOpcode() == ISD::UNDEF)
5367 continue;
5368 Values.insert(Elt);
5369 if (Elt.getOpcode() != ISD::Constant &&
5370 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005371 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005372 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005373 NumZero++;
5374 else {
5375 NonZeros |= (1 << i);
5376 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005377 }
5378 }
5379
Chris Lattner97a2a562010-08-26 05:24:29 +00005380 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5381 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005382 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383
Chris Lattner67f453a2008-03-09 05:42:06 +00005384 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005385 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005387 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005388
Chris Lattner62098042008-03-09 01:05:04 +00005389 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5390 // the value are obviously zero, truncate the value to i32 and do the
5391 // insertion that way. Only do this if the value is non-constant or if the
5392 // value is a constant being inserted into element 0. It is cheaper to do
5393 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005395 (!IsAllConstants || Idx == 0)) {
5396 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005397 // Handle SSE only.
5398 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5399 EVT VecVT = MVT::v4i32;
5400 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005401
Chris Lattner62098042008-03-09 01:05:04 +00005402 // Truncate the value (which may itself be a constant) to i32, and
5403 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005404 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005405 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005406 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Chris Lattner62098042008-03-09 01:05:04 +00005408 // Now we have our 32-bit value zero extended in the low element of
5409 // a vector. If Idx != 0, swizzle it into place.
5410 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 SmallVector<int, 4> Mask;
5412 Mask.push_back(Idx);
5413 for (unsigned i = 1; i != VecElts; ++i)
5414 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005415 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005416 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005417 }
Craig Topper07a27622012-01-22 03:07:48 +00005418 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005419 }
5420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005421
Chris Lattner19f79692008-03-08 22:59:52 +00005422 // If we have a constant or non-constant insertion into the low element of
5423 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5424 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005425 // depending on what the source datatype is.
5426 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005427 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005428 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005429
5430 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005431 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005432 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005433 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005434 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5435 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005436 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005437 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005438 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5439 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005440 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005441 }
5442
5443 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005445 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005446 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005447 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005448 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005449 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005450 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005451 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005452 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005453 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005454 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005455 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005456
5457 // Is it a vector logical left shift?
5458 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005459 X86::isZeroNode(Op.getOperand(0)) &&
5460 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005461 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005462 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005463 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005464 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005465 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005467
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005468 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005469 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470
Chris Lattner19f79692008-03-08 22:59:52 +00005471 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5472 // is a non-constant being inserted into an element other than the low one,
5473 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5474 // movd/movss) to move this into the low element, then shuffle it into
5475 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005477 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005478
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005480 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005482 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005483 MaskVec.push_back(i == Idx ? 0 : 1);
5484 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 }
5486 }
5487
Chris Lattner67f453a2008-03-09 05:42:06 +00005488 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005489 if (Values.size() == 1) {
5490 if (EVTBits == 32) {
5491 // Instead of a shuffle like this:
5492 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5493 // Check if it's possible to issue this instead.
5494 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5495 unsigned Idx = CountTrailingZeros_32(NonZeros);
5496 SDValue Item = Op.getOperand(Idx);
5497 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5498 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5499 }
Dan Gohman475871a2008-07-27 21:46:04 +00005500 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005501 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005502
Dan Gohmana3941172007-07-24 22:55:08 +00005503 // A vector full of immediates; various special cases are already
5504 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005505 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005506 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005507
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005508 // For AVX-length vectors, build the individual 128-bit pieces and use
5509 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005510 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005511 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005512 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005513 V.push_back(Op.getOperand(i));
5514
5515 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5516
5517 // Build both the lower and upper subvector.
5518 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5519 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5520 NumElems/2);
5521
5522 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005523 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005524 }
5525
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005526 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005527 if (EVTBits == 64) {
5528 if (NumNonZero == 1) {
5529 // One half is zero or undef.
5530 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005531 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005532 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005533 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005534 }
Dan Gohman475871a2008-07-27 21:46:04 +00005535 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005536 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005537
5538 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005539 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005540 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005541 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005542 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543 }
5544
Bill Wendling826f36f2007-03-28 00:57:11 +00005545 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005546 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005547 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005548 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005549 }
5550
5551 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005552 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005553 if (NumElems == 4 && NumZero > 0) {
5554 for (unsigned i = 0; i < 4; ++i) {
5555 bool isZero = !(NonZeros & (1 << i));
5556 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005557 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005558 else
Dale Johannesenace16102009-02-03 19:33:06 +00005559 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560 }
5561
5562 for (unsigned i = 0; i < 2; ++i) {
5563 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5564 default: break;
5565 case 0:
5566 V[i] = V[i*2]; // Must be a zero vector.
5567 break;
5568 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005569 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005570 break;
5571 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005572 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005573 break;
5574 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005576 break;
5577 }
5578 }
5579
Benjamin Kramer9c683542012-01-30 15:16:21 +00005580 bool Reverse1 = (NonZeros & 0x3) == 2;
5581 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5582 int MaskVec[] = {
5583 Reverse1 ? 1 : 0,
5584 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005585 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5586 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005587 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589 }
5590
Craig Topper7a9a28b2012-08-12 02:23:29 +00005591 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005592 // Check for a build vector of consecutive loads.
5593 for (unsigned i = 0; i < NumElems; ++i)
5594 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005595
Nate Begemanfdea31a2010-03-24 20:49:50 +00005596 // Check for elements which are consecutive loads.
5597 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5598 if (LD.getNode())
5599 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005600
Michael Liaofacace82012-10-19 17:15:18 +00005601 // Check for a build vector from mostly shuffle plus few inserting.
5602 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5603 if (Sh.getNode())
5604 return Sh;
5605
Michael J. Spencerec38de22010-10-10 22:04:20 +00005606 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005607 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005608 SDValue Result;
5609 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5610 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5611 else
5612 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005613
Chris Lattner24faf612010-08-28 17:59:08 +00005614 for (unsigned i = 1; i < NumElems; ++i) {
5615 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5616 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005618 }
5619 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005620 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005621
Chris Lattner6e80e442010-08-28 17:15:43 +00005622 // Otherwise, expand into a number of unpckl*, start by extending each of
5623 // our (non-undef) elements to the full vector width with the element in the
5624 // bottom slot of the vector (which generates no code for SSE).
5625 for (unsigned i = 0; i < NumElems; ++i) {
5626 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5627 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5628 else
5629 V[i] = DAG.getUNDEF(VT);
5630 }
5631
5632 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5634 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5635 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005636 unsigned EltStride = NumElems >> 1;
5637 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005638 for (unsigned i = 0; i < EltStride; ++i) {
5639 // If V[i+EltStride] is undef and this is the first round of mixing,
5640 // then it is safe to just drop this shuffle: V[i] is already in the
5641 // right place, the one element (since it's the first round) being
5642 // inserted as undef can be dropped. This isn't safe for successive
5643 // rounds because they will permute elements within both vectors.
5644 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5645 EltStride == NumElems/2)
5646 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005647
Chris Lattner6e80e442010-08-28 17:15:43 +00005648 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005649 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005650 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005651 }
5652 return V[0];
5653 }
Dan Gohman475871a2008-07-27 21:46:04 +00005654 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005655}
5656
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005657// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5658// to create 256-bit vectors from two other 128-bit ones.
5659static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5660 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005661 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005662
Craig Topper7a9a28b2012-08-12 02:23:29 +00005663 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005664
5665 SDValue V1 = Op.getOperand(0);
5666 SDValue V2 = Op.getOperand(1);
5667 unsigned NumElems = ResVT.getVectorNumElements();
5668
Craig Topper4c7972d2012-04-22 18:15:59 +00005669 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005670}
5671
Craig Topper55b24052012-09-11 06:15:32 +00005672static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005673 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005674
5675 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5676 // from two other 128-bit ones.
5677 return LowerAVXCONCAT_VECTORS(Op, DAG);
5678}
5679
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005680// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005681static SDValue
5682LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5683 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005684 SDValue V1 = SVOp->getOperand(0);
5685 SDValue V2 = SVOp->getOperand(1);
5686 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005687 MVT VT = SVOp->getValueType(0).getSimpleVT();
5688 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005689 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005690
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005691 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5692 return SDValue();
5693 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005694 return SDValue();
5695
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005696 // Check the mask for BLEND and build the value.
5697 unsigned MaskValue = 0;
5698 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005699 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005700 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005701
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005702 // Blend for v16i16 should be symetric for the both lanes.
5703 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005704
Craig Topper9b33ef72013-01-21 06:57:59 +00005705 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005706 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005707 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005708
Craig Topper04f74a12013-01-21 07:25:16 +00005709 if ((EltIdx < 0 || EltIdx == (int)i) &&
5710 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005711 continue;
5712
Craig Topper9b33ef72013-01-21 06:57:59 +00005713 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005714 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005715 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5716 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005717 else
Craig Topper1842ba02012-04-23 06:38:28 +00005718 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005719 }
5720
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005721 // Convert i32 vectors to floating point if it is not AVX2.
5722 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005723 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005724 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005725 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5726 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005727 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5728 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5729 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005730
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005731 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5732 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005733 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005734}
5735
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736// v8i16 shuffles - Prefer shuffles in the following order:
5737// 1. [all] pshuflw, pshufhw, optional move
5738// 2. [ssse3] 1 x pshufb
5739// 3. [ssse3] 2 x pshufb + 1 x por
5740// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005741static SDValue
5742LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5743 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005744 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005745 SDValue V1 = SVOp->getOperand(0);
5746 SDValue V2 = SVOp->getOperand(1);
5747 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005749
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 // Determine if more than 1 of the words in each of the low and high quadwords
5751 // of the result come from the same quadword of one of the two inputs. Undef
5752 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005753 unsigned LoQuad[] = { 0, 0, 0, 0 };
5754 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005755 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005757 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005758 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 MaskVals.push_back(EltIdx);
5760 if (EltIdx < 0) {
5761 ++Quad[0];
5762 ++Quad[1];
5763 ++Quad[2];
5764 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005765 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 }
5767 ++Quad[EltIdx / 4];
5768 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005769 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005770
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005772 unsigned MaxQuad = 1;
5773 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 if (LoQuad[i] > MaxQuad) {
5775 BestLoQuad = i;
5776 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005777 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005778 }
5779
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005781 MaxQuad = 1;
5782 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 if (HiQuad[i] > MaxQuad) {
5784 BestHiQuad = i;
5785 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005786 }
5787 }
5788
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005790 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 // single pshufb instruction is necessary. If There are more than 2 input
5792 // quads, disable the next transformation since it does not help SSSE3.
5793 bool V1Used = InputQuads[0] || InputQuads[1];
5794 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005795 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005797 BestLoQuad = InputQuads[0] ? 0 : 1;
5798 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 }
5800 if (InputQuads.count() > 2) {
5801 BestLoQuad = -1;
5802 BestHiQuad = -1;
5803 }
5804 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005805
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5807 // the shuffle mask. If a quad is scored as -1, that means that it contains
5808 // words from all 4 input quadwords.
5809 SDValue NewV;
5810 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005811 int MaskV[] = {
5812 BestLoQuad < 0 ? 0 : BestLoQuad,
5813 BestHiQuad < 0 ? 1 : BestHiQuad
5814 };
Eric Christopherfd179292009-08-27 18:07:15 +00005815 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005816 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5817 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5818 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005819
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5821 // source words for the shuffle, to aid later transformations.
5822 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005823 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005824 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005826 if (idx != (int)i)
5827 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005828 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005829 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 AllWordsInNewV = false;
5831 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005832 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005833
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5835 if (AllWordsInNewV) {
5836 for (int i = 0; i != 8; ++i) {
5837 int idx = MaskVals[i];
5838 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005840 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 if ((idx != i) && idx < 4)
5842 pshufhw = false;
5843 if ((idx != i) && idx > 3)
5844 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005845 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 V1 = NewV;
5847 V2Used = false;
5848 BestLoQuad = 0;
5849 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005850 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005851
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5853 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005854 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005855 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5856 unsigned TargetMask = 0;
5857 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005859 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5860 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5861 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005862 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005863 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005864 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005865 }
Eric Christopherfd179292009-08-27 18:07:15 +00005866
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005867 // Promote splats to a larger type which usually leads to more efficient code.
5868 // FIXME: Is this true if pshufb is available?
5869 if (SVOp->isSplat())
5870 return PromoteSplat(SVOp, DAG);
5871
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 // If we have SSSE3, and all words of the result are from 1 input vector,
5873 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5874 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005875 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005876 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005877
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005879 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 // mask, and elements that come from V1 in the V2 mask, so that the two
5881 // results can be OR'd together.
5882 bool TwoInputs = V1Used && V2Used;
5883 for (unsigned i = 0; i != 8; ++i) {
5884 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005885 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5886 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005887 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005888 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005889 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005890 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005891 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005892 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005893 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005895 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005896
Nate Begemanb9a47b82009-02-23 08:49:38 +00005897 // Calculate the shuffle mask for the second input, shuffle it, and
5898 // OR it with the first shuffled input.
5899 pshufbMask.clear();
5900 for (unsigned i = 0; i != 8; ++i) {
5901 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005902 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5903 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5904 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5905 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005907 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005908 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005909 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005910 MVT::v16i8, &pshufbMask[0], 16));
5911 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005912 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005913 }
5914
5915 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5916 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005917 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005919 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 for (int i = 0; i != 4; ++i) {
5921 int idx = MaskVals[i];
5922 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 InOrder.set(i);
5924 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005925 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005927 }
5928 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005930 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005931
Craig Topperdd637ae2012-02-19 05:41:45 +00005932 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5933 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005934 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005935 NewV.getOperand(0),
5936 getShufflePSHUFLWImmediate(SVOp), DAG);
5937 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 }
Eric Christopherfd179292009-08-27 18:07:15 +00005939
Nate Begemanb9a47b82009-02-23 08:49:38 +00005940 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5941 // and update MaskVals with the new element order.
5942 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005943 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 for (unsigned i = 4; i != 8; ++i) {
5945 int idx = MaskVals[i];
5946 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005947 InOrder.set(i);
5948 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005949 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 }
5952 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005954 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005955
Craig Topperdd637ae2012-02-19 05:41:45 +00005956 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5957 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005958 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005959 NewV.getOperand(0),
5960 getShufflePSHUFHWImmediate(SVOp), DAG);
5961 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005962 }
Eric Christopherfd179292009-08-27 18:07:15 +00005963
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 // In case BestHi & BestLo were both -1, which means each quadword has a word
5965 // from each of the four input quadwords, calculate the InOrder bitvector now
5966 // before falling through to the insert/extract cleanup.
5967 if (BestLoQuad == -1 && BestHiQuad == -1) {
5968 NewV = V1;
5969 for (int i = 0; i != 8; ++i)
5970 if (MaskVals[i] < 0 || MaskVals[i] == i)
5971 InOrder.set(i);
5972 }
Eric Christopherfd179292009-08-27 18:07:15 +00005973
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 // The other elements are put in the right place using pextrw and pinsrw.
5975 for (unsigned i = 0; i != 8; ++i) {
5976 if (InOrder[i])
5977 continue;
5978 int EltIdx = MaskVals[i];
5979 if (EltIdx < 0)
5980 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005981 SDValue ExtOp = (EltIdx < 8) ?
5982 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5983 DAG.getIntPtrConstant(EltIdx)) :
5984 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 DAG.getIntPtrConstant(i));
5988 }
5989 return NewV;
5990}
5991
5992// v16i8 shuffles - Prefer shuffles in the following order:
5993// 1. [ssse3] 1 x pshufb
5994// 2. [ssse3] 2 x pshufb + 1 x por
5995// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5996static
Nate Begeman9008ca62009-04-27 18:41:29 +00005997SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005998 SelectionDAG &DAG,
5999 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006000 SDValue V1 = SVOp->getOperand(0);
6001 SDValue V2 = SVOp->getOperand(1);
6002 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006003 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006004
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006005 // Promote splats to a larger type which usually leads to more efficient code.
6006 // FIXME: Is this true if pshufb is available?
6007 if (SVOp->isSplat())
6008 return PromoteSplat(SVOp, DAG);
6009
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006011 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006013
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006015 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006017
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006019 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 //
6021 // Otherwise, we have elements from both input vectors, and must zero out
6022 // elements that come from V2 in the first mask, and V1 in the second mask
6023 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 for (unsigned i = 0; i != 16; ++i) {
6025 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006026 if (EltIdx < 0 || EltIdx >= 16)
6027 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006028 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006031 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006033
6034 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6035 // the 2nd operand if it's undefined or zero.
6036 if (V2.getOpcode() == ISD::UNDEF ||
6037 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006038 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006039
Nate Begemanb9a47b82009-02-23 08:49:38 +00006040 // Calculate the shuffle mask for the second input, shuffle it, and
6041 // OR it with the first shuffled input.
6042 pshufbMask.clear();
6043 for (unsigned i = 0; i != 16; ++i) {
6044 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006045 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006046 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006048 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006049 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 MVT::v16i8, &pshufbMask[0], 16));
6051 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006052 }
Eric Christopherfd179292009-08-27 18:07:15 +00006053
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 // No SSSE3 - Calculate in place words and then fix all out of place words
6055 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6056 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006057 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6058 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006059 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006060 for (int i = 0; i != 8; ++i) {
6061 int Elt0 = MaskVals[i*2];
6062 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006063
Nate Begemanb9a47b82009-02-23 08:49:38 +00006064 // This word of the result is all undef, skip it.
6065 if (Elt0 < 0 && Elt1 < 0)
6066 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006067
Nate Begemanb9a47b82009-02-23 08:49:38 +00006068 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006069 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006070 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006071
Nate Begemanb9a47b82009-02-23 08:49:38 +00006072 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6073 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6074 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006075
6076 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6077 // using a single extract together, load it and store it.
6078 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006079 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006080 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006082 DAG.getIntPtrConstant(i));
6083 continue;
6084 }
6085
Nate Begemanb9a47b82009-02-23 08:49:38 +00006086 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006087 // source byte is not also odd, shift the extracted word left 8 bits
6088 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006089 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006090 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006091 DAG.getIntPtrConstant(Elt1 / 2));
6092 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006093 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006094 DAG.getConstant(8,
6095 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006096 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6098 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006099 }
6100 // If Elt0 is defined, extract it from the appropriate source. If the
6101 // source byte is not also even, shift the extracted word right 8 bits. If
6102 // Elt1 was also defined, OR the extracted values together before
6103 // inserting them in the result.
6104 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006105 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006106 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6107 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006109 DAG.getConstant(8,
6110 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006111 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006112 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6113 DAG.getConstant(0x00FF, MVT::i16));
6114 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006115 : InsElt0;
6116 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006117 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006118 DAG.getIntPtrConstant(i));
6119 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006120 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006121}
6122
Elena Demikhovsky41789462012-09-06 12:42:01 +00006123// v32i8 shuffles - Translate to VPSHUFB if possible.
6124static
6125SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006126 const X86Subtarget *Subtarget,
6127 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006128 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006129 SDValue V1 = SVOp->getOperand(0);
6130 SDValue V2 = SVOp->getOperand(1);
6131 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006132 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006133
6134 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006135 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6136 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006137
Michael Liao471b9172012-10-03 23:43:52 +00006138 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006139 // (1) one of input vector is undefined or zeroinitializer.
6140 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6141 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006142 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006143 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006144 return SDValue();
6145
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006146 if (V1IsAllZero && !V2IsAllZero) {
6147 CommuteVectorShuffleMask(MaskVals, 32);
6148 V1 = V2;
6149 }
6150 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006151 for (unsigned i = 0; i != 32; i++) {
6152 int EltIdx = MaskVals[i];
6153 if (EltIdx < 0 || EltIdx >= 32)
6154 EltIdx = 0x80;
6155 else {
6156 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6157 // Cross lane is not allowed.
6158 return SDValue();
6159 EltIdx &= 0xf;
6160 }
6161 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6162 }
6163 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6164 DAG.getNode(ISD::BUILD_VECTOR, dl,
6165 MVT::v32i8, &pshufbMask[0], 32));
6166}
6167
Evan Cheng7a831ce2007-12-15 03:00:47 +00006168/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006169/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006170/// done when every pair / quad of shuffle mask elements point to elements in
6171/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006172/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006173static
Nate Begeman9008ca62009-04-27 18:41:29 +00006174SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006175 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006176 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006177 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006178 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006179 MVT NewVT;
6180 unsigned Scale;
6181 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006182 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006183 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6184 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6185 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6186 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6187 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6188 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006189 }
6190
Nate Begeman9008ca62009-04-27 18:41:29 +00006191 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006192 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006193 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006194 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006195 int EltIdx = SVOp->getMaskElt(i+j);
6196 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006197 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006198 if (StartIdx < 0)
6199 StartIdx = (EltIdx / Scale);
6200 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006201 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006202 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006203 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006204 }
6205
Craig Topper11ac1f82012-05-04 04:08:44 +00006206 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6207 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006208 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006209}
6210
Evan Chengd880b972008-05-09 21:53:03 +00006211/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006212///
Craig Topperf84b7502013-01-20 00:50:58 +00006213static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006214 SDValue SrcOp, SelectionDAG &DAG,
6215 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006216 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006217 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006218 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006219 LD = dyn_cast<LoadSDNode>(SrcOp);
6220 if (!LD) {
6221 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6222 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006223 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006224 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006225 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006226 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006227 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006228 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006229 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006230 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006231 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6232 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6233 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006234 SrcOp.getOperand(0)
6235 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006236 }
6237 }
6238 }
6239
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006240 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006241 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006242 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006243 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006244}
6245
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006246/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6247/// which could not be matched by any known target speficic shuffle
6248static SDValue
6249LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006250
6251 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6252 if (NewOp.getNode())
6253 return NewOp;
6254
Craig Topper657a99c2013-01-19 23:36:09 +00006255 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006256
Craig Topper8f35c132012-01-20 09:29:03 +00006257 unsigned NumElems = VT.getVectorNumElements();
6258 unsigned NumLaneElems = NumElems / 2;
6259
Craig Topper8f35c132012-01-20 09:29:03 +00006260 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006261 MVT EltVT = VT.getVectorElementType();
6262 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006263 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006264
Craig Topper9a2b6e12012-04-06 07:45:23 +00006265 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006266 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006267 // Build a shuffle mask for the output, discovering on the fly which
6268 // input vectors to use as shuffle operands (recorded in InputUsed).
6269 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006270 // out with UseBuildVector set.
6271 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006272 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006273 unsigned LaneStart = l * NumLaneElems;
6274 for (unsigned i = 0; i != NumLaneElems; ++i) {
6275 // The mask element. This indexes into the input.
6276 int Idx = SVOp->getMaskElt(i+LaneStart);
6277 if (Idx < 0) {
6278 // the mask element does not index into any input vector.
6279 Mask.push_back(-1);
6280 continue;
6281 }
Craig Topper8f35c132012-01-20 09:29:03 +00006282
Craig Topper9a2b6e12012-04-06 07:45:23 +00006283 // The input vector this mask element indexes into.
6284 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006285
Craig Topper9a2b6e12012-04-06 07:45:23 +00006286 // Turn the index into an offset from the start of the input vector.
6287 Idx -= Input * NumLaneElems;
6288
6289 // Find or create a shuffle vector operand to hold this input.
6290 unsigned OpNo;
6291 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6292 if (InputUsed[OpNo] == Input)
6293 // This input vector is already an operand.
6294 break;
6295 if (InputUsed[OpNo] < 0) {
6296 // Create a new operand for this input vector.
6297 InputUsed[OpNo] = Input;
6298 break;
6299 }
6300 }
6301
6302 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006303 // More than two input vectors used! Give up on trying to create a
6304 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6305 UseBuildVector = true;
6306 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006307 }
6308
6309 // Add the mask index for the new shuffle vector.
6310 Mask.push_back(Idx + OpNo * NumLaneElems);
6311 }
6312
Craig Topper8ae97ba2012-05-21 06:40:16 +00006313 if (UseBuildVector) {
6314 SmallVector<SDValue, 16> SVOps;
6315 for (unsigned i = 0; i != NumLaneElems; ++i) {
6316 // The mask element. This indexes into the input.
6317 int Idx = SVOp->getMaskElt(i+LaneStart);
6318 if (Idx < 0) {
6319 SVOps.push_back(DAG.getUNDEF(EltVT));
6320 continue;
6321 }
6322
6323 // The input vector this mask element indexes into.
6324 int Input = Idx / NumElems;
6325
6326 // Turn the index into an offset from the start of the input vector.
6327 Idx -= Input * NumElems;
6328
6329 // Extract the vector element by hand.
6330 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6331 SVOp->getOperand(Input),
6332 DAG.getIntPtrConstant(Idx)));
6333 }
6334
6335 // Construct the output using a BUILD_VECTOR.
6336 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6337 SVOps.size());
6338 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006339 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006340 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006341 } else {
6342 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006343 (InputUsed[0] % 2) * NumLaneElems,
6344 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006345 // If only one input was used, use an undefined vector for the other.
6346 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6347 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006348 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006349 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006350 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006351 }
6352
6353 Mask.clear();
6354 }
Craig Topper8f35c132012-01-20 09:29:03 +00006355
6356 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006357 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006358}
6359
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006360/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6361/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006362static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006363LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006364 SDValue V1 = SVOp->getOperand(0);
6365 SDValue V2 = SVOp->getOperand(1);
6366 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006367 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006368
Craig Topper7a9a28b2012-08-12 02:23:29 +00006369 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006370
Benjamin Kramer9c683542012-01-30 15:16:21 +00006371 std::pair<int, int> Locs[4];
6372 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006373 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006374
Evan Chengace3c172008-07-22 21:13:36 +00006375 unsigned NumHi = 0;
6376 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006377 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006378 int Idx = PermMask[i];
6379 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006380 Locs[i] = std::make_pair(-1, -1);
6381 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006382 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6383 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006384 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006385 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006386 NumLo++;
6387 } else {
6388 Locs[i] = std::make_pair(1, NumHi);
6389 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006390 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006391 NumHi++;
6392 }
6393 }
6394 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006395
Evan Chengace3c172008-07-22 21:13:36 +00006396 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006397 // If no more than two elements come from either vector. This can be
6398 // implemented with two shuffles. First shuffle gather the elements.
6399 // The second shuffle, which takes the first shuffle as both of its
6400 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006401 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006402
Benjamin Kramer9c683542012-01-30 15:16:21 +00006403 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006404
Benjamin Kramer9c683542012-01-30 15:16:21 +00006405 for (unsigned i = 0; i != 4; ++i)
6406 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006407 unsigned Idx = (i < 2) ? 0 : 4;
6408 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006409 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006410 }
Evan Chengace3c172008-07-22 21:13:36 +00006411
Nate Begeman9008ca62009-04-27 18:41:29 +00006412 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006413 }
6414
6415 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006416 // Otherwise, we must have three elements from one vector, call it X, and
6417 // one element from the other, call it Y. First, use a shufps to build an
6418 // intermediate vector with the one element from Y and the element from X
6419 // that will be in the same half in the final destination (the indexes don't
6420 // matter). Then, use a shufps to build the final vector, taking the half
6421 // containing the element from Y from the intermediate, and the other half
6422 // from X.
6423 if (NumHi == 3) {
6424 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006425 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006426 std::swap(V1, V2);
6427 }
6428
6429 // Find the element from V2.
6430 unsigned HiIndex;
6431 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 int Val = PermMask[HiIndex];
6433 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006434 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006435 if (Val >= 4)
6436 break;
6437 }
6438
Nate Begeman9008ca62009-04-27 18:41:29 +00006439 Mask1[0] = PermMask[HiIndex];
6440 Mask1[1] = -1;
6441 Mask1[2] = PermMask[HiIndex^1];
6442 Mask1[3] = -1;
6443 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006444
6445 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006446 Mask1[0] = PermMask[0];
6447 Mask1[1] = PermMask[1];
6448 Mask1[2] = HiIndex & 1 ? 6 : 4;
6449 Mask1[3] = HiIndex & 1 ? 4 : 6;
6450 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006451 }
Craig Topper69947b92012-04-23 06:57:04 +00006452
6453 Mask1[0] = HiIndex & 1 ? 2 : 0;
6454 Mask1[1] = HiIndex & 1 ? 0 : 2;
6455 Mask1[2] = PermMask[2];
6456 Mask1[3] = PermMask[3];
6457 if (Mask1[2] >= 0)
6458 Mask1[2] += 4;
6459 if (Mask1[3] >= 0)
6460 Mask1[3] += 4;
6461 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006462 }
6463
6464 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006465 int LoMask[] = { -1, -1, -1, -1 };
6466 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006467
Benjamin Kramer9c683542012-01-30 15:16:21 +00006468 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006469 unsigned MaskIdx = 0;
6470 unsigned LoIdx = 0;
6471 unsigned HiIdx = 2;
6472 for (unsigned i = 0; i != 4; ++i) {
6473 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006474 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006475 MaskIdx = 1;
6476 LoIdx = 0;
6477 HiIdx = 2;
6478 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006479 int Idx = PermMask[i];
6480 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006481 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006482 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006483 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006484 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006485 LoIdx++;
6486 } else {
6487 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006488 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006489 HiIdx++;
6490 }
6491 }
6492
Nate Begeman9008ca62009-04-27 18:41:29 +00006493 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6494 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006495 int MaskOps[] = { -1, -1, -1, -1 };
6496 for (unsigned i = 0; i != 4; ++i)
6497 if (Locs[i].first != -1)
6498 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006499 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006500}
6501
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006502static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006503 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006504 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006505
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006506 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6507 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006508 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6509 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6510 // BUILD_VECTOR (load), undef
6511 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006512
6513 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006514}
6515
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006516static
Evan Cheng835580f2010-10-07 20:50:20 +00006517SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6518 EVT VT = Op.getValueType();
6519
6520 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006521 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6522 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006523 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6524 V1, DAG));
6525}
6526
6527static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006528SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006529 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006530 SDValue V1 = Op.getOperand(0);
6531 SDValue V2 = Op.getOperand(1);
6532 EVT VT = Op.getValueType();
6533
6534 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6535
Craig Topper1accb7e2012-01-10 06:54:16 +00006536 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006537 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6538
Evan Cheng0899f5c2011-08-31 02:05:24 +00006539 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6540 return DAG.getNode(ISD::BITCAST, dl, VT,
6541 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6542 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6543 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006544}
6545
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006546static
6547SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6548 SDValue V1 = Op.getOperand(0);
6549 SDValue V2 = Op.getOperand(1);
6550 EVT VT = Op.getValueType();
6551
6552 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6553 "unsupported shuffle type");
6554
6555 if (V2.getOpcode() == ISD::UNDEF)
6556 V2 = V1;
6557
6558 // v4i32 or v4f32
6559 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6560}
6561
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006562static
Craig Topper1accb7e2012-01-10 06:54:16 +00006563SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564 SDValue V1 = Op.getOperand(0);
6565 SDValue V2 = Op.getOperand(1);
6566 EVT VT = Op.getValueType();
6567 unsigned NumElems = VT.getVectorNumElements();
6568
6569 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6570 // operand of these instructions is only memory, so check if there's a
6571 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6572 // same masks.
6573 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006574
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006575 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006576 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006577 CanFoldLoad = true;
6578
6579 // When V1 is a load, it can be folded later into a store in isel, example:
6580 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6581 // turns into:
6582 // (MOVLPSmr addr:$src1, VR128:$src2)
6583 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006584 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006585 CanFoldLoad = true;
6586
Dan Gohman65fd6562011-11-03 21:49:52 +00006587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006588 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006589 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006590 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6591
6592 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006593 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006594 if (SVOp->getMaskElt(1) != -1)
6595 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006596 }
6597
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006598 // movl and movlp will both match v2i64, but v2i64 is never matched by
6599 // movl earlier because we make it strict to avoid messing with the movlp load
6600 // folding logic (see the code above getMOVLP call). Match it here then,
6601 // this is horrible, but will stay like this until we move all shuffle
6602 // matching to x86 specific nodes. Note that for the 1st condition all
6603 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006604 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006605 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6606 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006607 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006608 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006609 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006610 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006611
6612 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6613
6614 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006615 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006616 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006617}
6618
Michael Liaod9d09602012-10-23 17:34:00 +00006619// Reduce a vector shuffle to zext.
6620SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006621X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006622 // PMOVZX is only available from SSE41.
6623 if (!Subtarget->hasSSE41())
6624 return SDValue();
6625
6626 EVT VT = Op.getValueType();
6627
6628 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006629 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006630 return SDValue();
6631
6632 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6633 DebugLoc DL = Op.getDebugLoc();
6634 SDValue V1 = Op.getOperand(0);
6635 SDValue V2 = Op.getOperand(1);
6636 unsigned NumElems = VT.getVectorNumElements();
6637
6638 // Extending is an unary operation and the element type of the source vector
6639 // won't be equal to or larger than i64.
6640 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6641 VT.getVectorElementType() == MVT::i64)
6642 return SDValue();
6643
6644 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6645 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006646 while ((1U << Shift) < NumElems) {
6647 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006648 break;
6649 Shift += 1;
6650 // The maximal ratio is 8, i.e. from i8 to i64.
6651 if (Shift > 3)
6652 return SDValue();
6653 }
6654
6655 // Check the shuffle mask.
6656 unsigned Mask = (1U << Shift) - 1;
6657 for (unsigned i = 0; i != NumElems; ++i) {
6658 int EltIdx = SVOp->getMaskElt(i);
6659 if ((i & Mask) != 0 && EltIdx != -1)
6660 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006661 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006662 return SDValue();
6663 }
6664
6665 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6666 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6667 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6668
6669 if (!isTypeLegal(NVT))
6670 return SDValue();
6671
6672 // Simplify the operand as it's prepared to be fed into shuffle.
6673 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6674 if (V1.getOpcode() == ISD::BITCAST &&
6675 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6676 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6677 V1.getOperand(0)
6678 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6679 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6680 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006681 ConstantSDNode *CIdx =
6682 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006683 // If it's foldable, i.e. normal load with single use, we will let code
6684 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006685 if (CIdx && CIdx->getZExtValue() == 0 &&
6686 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006687 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6688 }
6689
6690 return DAG.getNode(ISD::BITCAST, DL, VT,
6691 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6692}
6693
Nadav Rotem154819d2012-04-09 07:45:58 +00006694SDValue
6695X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006696 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006697 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006698 DebugLoc dl = Op.getDebugLoc();
6699 SDValue V1 = Op.getOperand(0);
6700 SDValue V2 = Op.getOperand(1);
6701
6702 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006703 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006704
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006705 // Handle splat operations
6706 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006707 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006708 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006709 if (Broadcast.getNode())
6710 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006711 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006712
Michael Liaod9d09602012-10-23 17:34:00 +00006713 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006714 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006715 if (NewOp.getNode())
6716 return NewOp;
6717
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006718 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6719 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006720 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6721 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006722 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006723 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006724 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006725 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006726 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006727 // FIXME: Figure out a cleaner way to do this.
6728 // Try to make use of movq to zero out the top part.
6729 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006730 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006731 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006732 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006733 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6734 NewVT, true, false))
6735 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006736 DAG, Subtarget, dl);
6737 }
6738 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006739 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006740 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006741 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006742 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6743 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6744 DAG, Subtarget, dl);
6745 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006746 }
6747 }
6748 return SDValue();
6749}
6750
Dan Gohman475871a2008-07-27 21:46:04 +00006751SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006752X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006753 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006754 SDValue V1 = Op.getOperand(0);
6755 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006756 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006757 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006758 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006759 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006760 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006761 bool V1IsSplat = false;
6762 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006763 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006764 bool HasFp256 = Subtarget->hasFp256();
6765 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006766 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006767 bool OptForSize = MF.getFunction()->getAttributes().
6768 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769
Craig Topper3426a3e2011-11-14 06:46:21 +00006770 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006771
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006772 if (V1IsUndef && V2IsUndef)
6773 return DAG.getUNDEF(VT);
6774
6775 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006776
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006777 // Vector shuffle lowering takes 3 steps:
6778 //
6779 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6780 // narrowing and commutation of operands should be handled.
6781 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6782 // shuffle nodes.
6783 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6784 // so the shuffle can be broken into other shuffles and the legalizer can
6785 // try the lowering again.
6786 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006787 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006788 // be matched during isel, all of them must be converted to a target specific
6789 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006790
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006791 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6792 // narrowing and commutation of operands should be handled. The actual code
6793 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006794 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006795 if (NewOp.getNode())
6796 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006797
Craig Topper5aaffa82012-02-19 02:53:47 +00006798 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6799
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006800 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6801 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006802 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006803 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006804 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006805 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006806
Craig Topperdd637ae2012-02-19 05:41:45 +00006807 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006808 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006809 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006810
Craig Topperdd637ae2012-02-19 05:41:45 +00006811 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006812 return getMOVHighToLow(Op, dl, DAG);
6813
6814 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006815 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006816 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006817 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006818
Craig Topper5aaffa82012-02-19 02:53:47 +00006819 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006820 // The actual implementation will match the mask in the if above and then
6821 // during isel it can match several different instructions, not only pshufd
6822 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006823 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6824 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006825
Craig Topper5aaffa82012-02-19 02:53:47 +00006826 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006827
Craig Topper1accb7e2012-01-10 06:54:16 +00006828 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006829 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6830
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006831 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6832 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6833 DAG);
6834
Craig Topperb3982da2011-12-31 23:50:21 +00006835 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006836 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006837 }
Eric Christopherfd179292009-08-27 18:07:15 +00006838
Evan Chengf26ffe92008-05-29 08:22:04 +00006839 // Check if this can be converted into a logical shift.
6840 bool isLeft = false;
6841 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006842 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006843 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006844 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006845 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006846 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006847 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006848 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006849 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006850 }
Eric Christopherfd179292009-08-27 18:07:15 +00006851
Craig Topper5aaffa82012-02-19 02:53:47 +00006852 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006853 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006854 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006855 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006856 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006857 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6858
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006859 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006860 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6861 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006862 }
Eric Christopherfd179292009-08-27 18:07:15 +00006863
Nate Begeman9008ca62009-04-27 18:41:29 +00006864 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006865 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006866 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006867
Craig Topperdd637ae2012-02-19 05:41:45 +00006868 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006869 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006870
Craig Topperdd637ae2012-02-19 05:41:45 +00006871 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006872 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006873
Craig Topperdd637ae2012-02-19 05:41:45 +00006874 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006875 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006876
Craig Topperdd637ae2012-02-19 05:41:45 +00006877 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006878 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879
Craig Topperdd637ae2012-02-19 05:41:45 +00006880 if (ShouldXformToMOVHLPS(M, VT) ||
6881 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006882 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006883
Evan Chengf26ffe92008-05-29 08:22:04 +00006884 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006885 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006886 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006887 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006888 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006889 }
Eric Christopherfd179292009-08-27 18:07:15 +00006890
Evan Cheng9eca5e82006-10-25 21:49:50 +00006891 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006892 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6893 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006894 V1IsSplat = isSplatVector(V1.getNode());
6895 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006896
Chris Lattner8a594482007-11-25 00:24:49 +00006897 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006898 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6899 CommuteVectorShuffleMask(M, NumElems);
6900 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006901 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006902 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006903 }
6904
Craig Topperbeabc6c2011-12-05 06:56:46 +00006905 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006906 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006907 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006908 return V1;
6909 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6910 // the instruction selector will not match, so get a canonical MOVL with
6911 // swapped operands to undo the commute.
6912 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006913 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006914
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006915 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006916 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006917
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006918 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006919 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006920
Evan Cheng9bbbb982006-10-25 20:48:19 +00006921 if (V2IsSplat) {
6922 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006923 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006924 // new vector_shuffle with the corrected mask.p
6925 SmallVector<int, 8> NewMask(M.begin(), M.end());
6926 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006927 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006928 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006929 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006930 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006931 }
6932
Evan Cheng9eca5e82006-10-25 21:49:50 +00006933 if (Commuted) {
6934 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006935 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006936 CommuteVectorShuffleMask(M, NumElems);
6937 std::swap(V1, V2);
6938 std::swap(V1IsSplat, V2IsSplat);
6939 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006940
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006941 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006942 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006943
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006944 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006945 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006946 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947
Nate Begeman9008ca62009-04-27 18:41:29 +00006948 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006949 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006950 return CommuteVectorShuffle(SVOp, DAG);
6951
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006952 // The checks below are all present in isShuffleMaskLegal, but they are
6953 // inlined here right now to enable us to directly emit target specific
6954 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006955
Craig Topper0e2037b2012-01-20 05:53:00 +00006956 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006957 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006958 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006959 DAG);
6960
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006961 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6962 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006963 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006964 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006965 }
6966
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006967 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006968 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006969 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006970 DAG);
6971
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006972 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006973 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006974 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006975 DAG);
6976
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006978 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006979 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006980
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006981 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006982 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006983 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006984 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006985
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006986 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006987 // Generate target specific nodes for 128 or 256-bit shuffles only
6988 // supported in the AVX instruction set.
6989 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006990
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006991 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006992 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006993 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6994
Craig Topper70b883b2011-11-28 10:14:51 +00006995 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006996 if (isVPERMILPMask(M, VT, HasFp256)) {
6997 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006998 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006999 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007000 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007001 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007002 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007003
Craig Topper70b883b2011-11-28 10:14:51 +00007004 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007005 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007006 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007007 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007008
Craig Topper1842ba02012-04-23 06:38:28 +00007009 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007010 if (BlendOp.getNode())
7011 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007012
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007013 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007014 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007015 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007016 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007017 }
Craig Topper92040742012-04-16 06:43:40 +00007018 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7019 &permclMask[0], 8);
7020 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007021 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007022 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007023 }
Craig Topper095c5282012-04-15 23:48:57 +00007024
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007025 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007026 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007027 getShuffleCLImmediate(SVOp), DAG);
7028
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007029 //===--------------------------------------------------------------------===//
7030 // Since no target specific shuffle was selected for this generic one,
7031 // lower it into other known shuffles. FIXME: this isn't true yet, but
7032 // this is the plan.
7033 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007034
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007035 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7036 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007037 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007038 if (NewOp.getNode())
7039 return NewOp;
7040 }
7041
7042 if (VT == MVT::v16i8) {
7043 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7044 if (NewOp.getNode())
7045 return NewOp;
7046 }
7047
Elena Demikhovsky41789462012-09-06 12:42:01 +00007048 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007049 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007050 if (NewOp.getNode())
7051 return NewOp;
7052 }
7053
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007054 // Handle all 128-bit wide vectors with 4 elements, and match them with
7055 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007056 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007057 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7058
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007059 // Handle general 256-bit shuffles
7060 if (VT.is256BitVector())
7061 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7062
Dan Gohman475871a2008-07-27 21:46:04 +00007063 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007064}
7065
Craig Topperf84b7502013-01-20 00:50:58 +00007066static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007067 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007068 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007069
Craig Topper45e1c752013-01-20 00:38:18 +00007070 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007071 return SDValue();
7072
Duncan Sands83ec4b62008-06-06 12:08:01 +00007073 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007075 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007077 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007078 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007079 }
7080
7081 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007082 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7083 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7084 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7086 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007087 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007089 Op.getOperand(0)),
7090 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007092 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007094 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007095 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007096 }
7097
7098 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007099 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7100 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007101 // result has a single use which is a store or a bitcast to i32. And in
7102 // the case of a store, it's not worth it if the index is a constant 0,
7103 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007104 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007105 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007106 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007107 if ((User->getOpcode() != ISD::STORE ||
7108 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7109 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007110 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007112 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007114 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007115 Op.getOperand(0)),
7116 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007117 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007118 }
7119
7120 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007121 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007122 if (isa<ConstantSDNode>(Op.getOperand(1)))
7123 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007124 }
Dan Gohman475871a2008-07-27 21:46:04 +00007125 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007126}
7127
Dan Gohman475871a2008-07-27 21:46:04 +00007128SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007129X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7130 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007131 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007132 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133
David Greene74a579d2011-02-10 16:57:36 +00007134 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007135 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007136
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007137 // If this is a 256-bit vector result, first extract the 128-bit vector and
7138 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007139 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007140 DebugLoc dl = Op.getNode()->getDebugLoc();
7141 unsigned NumElems = VecVT.getVectorNumElements();
7142 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007143 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7144
7145 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007146 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007147
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007148 if (IdxVal >= NumElems/2)
7149 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007150 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007151 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007152 }
7153
Craig Topper7a9a28b2012-08-12 02:23:29 +00007154 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007155
Craig Topperd0a31172012-01-10 06:37:29 +00007156 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007157 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007158 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007159 return Res;
7160 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007161
Craig Topper45e1c752013-01-20 00:38:18 +00007162 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007163 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007164 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007165 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007166 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007167 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007168 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007169 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7170 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007171 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007172 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007173 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007175 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007176 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007177 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007178 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007179 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007180 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007181 }
7182
7183 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007184 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007185 if (Idx == 0)
7186 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007187
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007189 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007190 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007191 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007192 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007193 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007194 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007195 }
7196
7197 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007198 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7199 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7200 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007201 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007202 if (Idx == 0)
7203 return Op;
7204
7205 // UNPCKHPD the element to the lowest double word, then movsd.
7206 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7207 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007208 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007209 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007210 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007211 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007212 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007213 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007214 }
7215
Dan Gohman475871a2008-07-27 21:46:04 +00007216 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007217}
7218
Craig Topperf84b7502013-01-20 00:50:58 +00007219static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007220 MVT VT = Op.getValueType().getSimpleVT();
7221 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007222 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007223
Dan Gohman475871a2008-07-27 21:46:04 +00007224 SDValue N0 = Op.getOperand(0);
7225 SDValue N1 = Op.getOperand(1);
7226 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007227
Craig Topper7a9a28b2012-08-12 02:23:29 +00007228 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007229 return SDValue();
7230
Dan Gohman8a55ce42009-09-23 21:02:20 +00007231 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007232 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007233 unsigned Opc;
7234 if (VT == MVT::v8i16)
7235 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007236 else if (VT == MVT::v16i8)
7237 Opc = X86ISD::PINSRB;
7238 else
7239 Opc = X86ISD::PINSRB;
7240
Nate Begeman14d12ca2008-02-11 04:19:36 +00007241 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7242 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007243 if (N1.getValueType() != MVT::i32)
7244 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7245 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007246 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007247 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007248 }
7249
7250 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007251 // Bits [7:6] of the constant are the source select. This will always be
7252 // zero here. The DAG Combiner may combine an extract_elt index into these
7253 // bits. For example (insert (extract, 3), 2) could be matched by putting
7254 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007255 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007256 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007257 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007258 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007259 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007260 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007262 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007263 }
7264
7265 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007266 // PINSR* works with constant index.
7267 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007268 }
Dan Gohman475871a2008-07-27 21:46:04 +00007269 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007270}
7271
Dan Gohman475871a2008-07-27 21:46:04 +00007272SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007273X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007274 MVT VT = Op.getValueType().getSimpleVT();
7275 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007276
David Greene6b381262011-02-09 15:32:06 +00007277 DebugLoc dl = Op.getDebugLoc();
7278 SDValue N0 = Op.getOperand(0);
7279 SDValue N1 = Op.getOperand(1);
7280 SDValue N2 = Op.getOperand(2);
7281
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007282 // If this is a 256-bit vector result, first extract the 128-bit vector,
7283 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007284 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007285 if (!isa<ConstantSDNode>(N2))
7286 return SDValue();
7287
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007288 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007289 unsigned NumElems = VT.getVectorNumElements();
7290 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007291 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007292
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007293 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007294 bool Upper = IdxVal >= NumElems/2;
7295 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7296 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007297
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007298 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007299 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007300 }
7301
Craig Topperd0a31172012-01-10 06:37:29 +00007302 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007303 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7304
Dan Gohman8a55ce42009-09-23 21:02:20 +00007305 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007306 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007307
Dan Gohman8a55ce42009-09-23 21:02:20 +00007308 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007309 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7310 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007311 if (N1.getValueType() != MVT::i32)
7312 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7313 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007314 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007315 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007316 }
Dan Gohman475871a2008-07-27 21:46:04 +00007317 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007318}
7319
Craig Topper55b24052012-09-11 06:15:32 +00007320static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007321 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007322 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007323 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007324
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007325 // If this is a 256-bit vector result, first insert into a 128-bit
7326 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007327 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007328 // Insert into a 128-bit vector.
7329 EVT VT128 = EVT::getVectorVT(*Context,
7330 OpVT.getVectorElementType(),
7331 OpVT.getVectorNumElements() / 2);
7332
7333 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7334
7335 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007336 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007337 }
7338
Craig Topperd77d2fe2012-04-29 20:22:05 +00007339 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007340 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007341 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007342
Owen Anderson825b72b2009-08-11 20:47:22 +00007343 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007344 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007345 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007346 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007347}
7348
David Greene91585092011-01-26 15:38:49 +00007349// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7350// a simple subregister reference or explicit instructions to grab
7351// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007352static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7353 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007354 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007355 DebugLoc dl = Op.getNode()->getDebugLoc();
7356 SDValue Vec = Op.getNode()->getOperand(0);
7357 SDValue Idx = Op.getNode()->getOperand(1);
7358
Craig Topper7a9a28b2012-08-12 02:23:29 +00007359 if (Op.getNode()->getValueType(0).is128BitVector() &&
7360 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007361 isa<ConstantSDNode>(Idx)) {
7362 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7363 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007364 }
David Greene91585092011-01-26 15:38:49 +00007365 }
7366 return SDValue();
7367}
7368
David Greenecfe33c42011-01-26 19:13:22 +00007369// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7370// simple superregister reference or explicit instructions to insert
7371// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007372static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7373 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007374 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007375 DebugLoc dl = Op.getNode()->getDebugLoc();
7376 SDValue Vec = Op.getNode()->getOperand(0);
7377 SDValue SubVec = Op.getNode()->getOperand(1);
7378 SDValue Idx = Op.getNode()->getOperand(2);
7379
Craig Topper7a9a28b2012-08-12 02:23:29 +00007380 if (Op.getNode()->getValueType(0).is256BitVector() &&
7381 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007382 isa<ConstantSDNode>(Idx)) {
7383 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7384 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007385 }
7386 }
7387 return SDValue();
7388}
7389
Bill Wendling056292f2008-09-16 21:48:12 +00007390// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7391// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7392// one of the above mentioned nodes. It has to be wrapped because otherwise
7393// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7394// be used to form addressing mode. These wrapped nodes will be selected
7395// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007396SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007397X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007398 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007399
Chris Lattner41621a22009-06-26 19:22:52 +00007400 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7401 // global base reg.
7402 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007403 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007404 CodeModel::Model M = getTargetMachine().getCodeModel();
7405
Chris Lattner4f066492009-07-11 20:29:19 +00007406 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007407 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007408 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007409 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007410 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007411 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007412 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007413
Evan Cheng1606e8e2009-03-13 07:51:59 +00007414 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007415 CP->getAlignment(),
7416 CP->getOffset(), OpFlag);
7417 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007418 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007419 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007420 if (OpFlag) {
7421 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007422 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007423 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007424 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007425 }
7426
7427 return Result;
7428}
7429
Dan Gohmand858e902010-04-17 15:26:15 +00007430SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007431 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007432
Chris Lattner18c59872009-06-27 04:16:01 +00007433 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7434 // global base reg.
7435 unsigned char OpFlag = 0;
7436 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007437 CodeModel::Model M = getTargetMachine().getCodeModel();
7438
Chris Lattner4f066492009-07-11 20:29:19 +00007439 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007440 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007441 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007442 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007443 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007444 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007445 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007446
Chris Lattner18c59872009-06-27 04:16:01 +00007447 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7448 OpFlag);
7449 DebugLoc DL = JT->getDebugLoc();
7450 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007451
Chris Lattner18c59872009-06-27 04:16:01 +00007452 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007453 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007454 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7455 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007456 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007457 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007458
Chris Lattner18c59872009-06-27 04:16:01 +00007459 return Result;
7460}
7461
7462SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007463X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007464 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007465
Chris Lattner18c59872009-06-27 04:16:01 +00007466 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7467 // global base reg.
7468 unsigned char OpFlag = 0;
7469 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007470 CodeModel::Model M = getTargetMachine().getCodeModel();
7471
Chris Lattner4f066492009-07-11 20:29:19 +00007472 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007473 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7474 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7475 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007476 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007477 } else if (Subtarget->isPICStyleGOT()) {
7478 OpFlag = X86II::MO_GOT;
7479 } else if (Subtarget->isPICStyleStubPIC()) {
7480 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7481 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7482 OpFlag = X86II::MO_DARWIN_NONLAZY;
7483 }
Eric Christopherfd179292009-08-27 18:07:15 +00007484
Chris Lattner18c59872009-06-27 04:16:01 +00007485 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007486
Chris Lattner18c59872009-06-27 04:16:01 +00007487 DebugLoc DL = Op.getDebugLoc();
7488 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007489
Chris Lattner18c59872009-06-27 04:16:01 +00007490 // With PIC, the address is actually $g + Offset.
7491 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007492 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007493 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7494 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007495 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007496 Result);
7497 }
Eric Christopherfd179292009-08-27 18:07:15 +00007498
Eli Friedman586272d2011-08-11 01:48:05 +00007499 // For symbols that require a load from a stub to get the address, emit the
7500 // load.
7501 if (isGlobalStubReference(OpFlag))
7502 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007503 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007504
Chris Lattner18c59872009-06-27 04:16:01 +00007505 return Result;
7506}
7507
Dan Gohman475871a2008-07-27 21:46:04 +00007508SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007509X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007510 // Create the TargetBlockAddressAddress node.
7511 unsigned char OpFlags =
7512 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007513 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007514 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007515 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007516 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007517 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7518 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007519
Dan Gohmanf705adb2009-10-30 01:28:02 +00007520 if (Subtarget->isPICStyleRIPRel() &&
7521 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007522 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7523 else
7524 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007525
Dan Gohman29cbade2009-11-20 23:18:13 +00007526 // With PIC, the address is actually $g + Offset.
7527 if (isGlobalRelativeToPICBase(OpFlags)) {
7528 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7529 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7530 Result);
7531 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007532
7533 return Result;
7534}
7535
7536SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007537X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007538 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007539 // Create the TargetGlobalAddress node, folding in the constant
7540 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007541 unsigned char OpFlags =
7542 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007543 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007544 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007545 if (OpFlags == X86II::MO_NO_FLAG &&
7546 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007547 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007548 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007549 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007550 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007551 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007552 }
Eric Christopherfd179292009-08-27 18:07:15 +00007553
Chris Lattner4f066492009-07-11 20:29:19 +00007554 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007555 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007556 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7557 else
7558 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007559
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007560 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007561 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007562 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7563 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007564 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007566
Chris Lattner36c25012009-07-10 07:34:39 +00007567 // For globals that require a load from a stub to get the address, emit the
7568 // load.
7569 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007570 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007571 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007572
Dan Gohman6520e202008-10-18 02:06:02 +00007573 // If there was a non-zero offset that we didn't fold, create an explicit
7574 // addition for it.
7575 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007576 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007577 DAG.getConstant(Offset, getPointerTy()));
7578
Evan Cheng0db9fe62006-04-25 20:13:52 +00007579 return Result;
7580}
7581
Evan Chengda43bcf2008-09-24 00:05:32 +00007582SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007583X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007584 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007585 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007586 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007587}
7588
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007589static SDValue
7590GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007591 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007592 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007593 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007594 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007595 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007596 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007597 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007598 GA->getOffset(),
7599 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007600
7601 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7602 : X86ISD::TLSADDR;
7603
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007604 if (InFlag) {
7605 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007606 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007607 } else {
7608 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007609 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007610 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007611
7612 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007613 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007614
Rafael Espindola15f1b662009-04-24 12:59:40 +00007615 SDValue Flag = Chain.getValue(1);
7616 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007617}
7618
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007619// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007620static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007621LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007622 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007623 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007624 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7625 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007626 DAG.getNode(X86ISD::GlobalBaseReg,
7627 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007628 InFlag = Chain.getValue(1);
7629
Chris Lattnerb903bed2009-06-26 21:20:29 +00007630 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007631}
7632
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007633// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007634static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007635LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007636 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007637 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7638 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007639}
7640
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007641static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7642 SelectionDAG &DAG,
7643 const EVT PtrVT,
7644 bool is64Bit) {
7645 DebugLoc dl = GA->getDebugLoc();
7646
7647 // Get the start address of the TLS block for this module.
7648 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7649 .getInfo<X86MachineFunctionInfo>();
7650 MFI->incNumLocalDynamicTLSAccesses();
7651
7652 SDValue Base;
7653 if (is64Bit) {
7654 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7655 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7656 } else {
7657 SDValue InFlag;
7658 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7659 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7660 InFlag = Chain.getValue(1);
7661 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7662 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7663 }
7664
7665 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7666 // of Base.
7667
7668 // Build x@dtpoff.
7669 unsigned char OperandFlags = X86II::MO_DTPOFF;
7670 unsigned WrapperKind = X86ISD::Wrapper;
7671 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7672 GA->getValueType(0),
7673 GA->getOffset(), OperandFlags);
7674 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7675
7676 // Add x@dtpoff with the base.
7677 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7678}
7679
Hans Wennborg228756c2012-05-11 10:11:01 +00007680// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007681static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007682 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007683 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007684 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007685
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007686 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7687 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7688 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007689
Michael J. Spencerec38de22010-10-10 22:04:20 +00007690 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007691 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007692 MachinePointerInfo(Ptr),
7693 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007694
Chris Lattnerb903bed2009-06-26 21:20:29 +00007695 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007696 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7697 // initialexec.
7698 unsigned WrapperKind = X86ISD::Wrapper;
7699 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007700 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007701 } else if (model == TLSModel::InitialExec) {
7702 if (is64Bit) {
7703 OperandFlags = X86II::MO_GOTTPOFF;
7704 WrapperKind = X86ISD::WrapperRIP;
7705 } else {
7706 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7707 }
Chris Lattner18c59872009-06-27 04:16:01 +00007708 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007709 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007710 }
Eric Christopherfd179292009-08-27 18:07:15 +00007711
Hans Wennborg228756c2012-05-11 10:11:01 +00007712 // emit "addl x@ntpoff,%eax" (local exec)
7713 // or "addl x@indntpoff,%eax" (initial exec)
7714 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007715 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007716 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007717 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007718 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007719
Hans Wennborg228756c2012-05-11 10:11:01 +00007720 if (model == TLSModel::InitialExec) {
7721 if (isPIC && !is64Bit) {
7722 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7723 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7724 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007725 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007726
7727 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7728 MachinePointerInfo::getGOT(), false, false, false,
7729 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007730 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007731
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007732 // The address of the thread local variable is the add of the thread
7733 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007734 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007735}
7736
Dan Gohman475871a2008-07-27 21:46:04 +00007737SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007738X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007739
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007740 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007741 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007742
Eric Christopher30ef0e52010-06-03 04:07:48 +00007743 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007744 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007745
Eric Christopher30ef0e52010-06-03 04:07:48 +00007746 switch (model) {
7747 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007748 if (Subtarget->is64Bit())
7749 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7750 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007751 case TLSModel::LocalDynamic:
7752 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7753 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007754 case TLSModel::InitialExec:
7755 case TLSModel::LocalExec:
7756 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007757 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007758 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007759 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007760 llvm_unreachable("Unknown TLS model.");
7761 }
7762
7763 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 // Darwin only has one model of TLS. Lower to that.
7765 unsigned char OpFlag = 0;
7766 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7767 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768
Eric Christopher30ef0e52010-06-03 04:07:48 +00007769 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7770 // global base reg.
7771 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7772 !Subtarget->is64Bit();
7773 if (PIC32)
7774 OpFlag = X86II::MO_TLVP_PIC_BASE;
7775 else
7776 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007777 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007778 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007779 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007780 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007781 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007782
Eric Christopher30ef0e52010-06-03 04:07:48 +00007783 // With PIC32, the address is actually $g + Offset.
7784 if (PIC32)
7785 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7786 DAG.getNode(X86ISD::GlobalBaseReg,
7787 DebugLoc(), getPointerTy()),
7788 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007789
Eric Christopher30ef0e52010-06-03 04:07:48 +00007790 // Lowering the machine isd will make sure everything is in the right
7791 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007792 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007793 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007794 SDValue Args[] = { Chain, Offset };
7795 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007796
Eric Christopher30ef0e52010-06-03 04:07:48 +00007797 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7798 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7799 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007800
Eric Christopher30ef0e52010-06-03 04:07:48 +00007801 // And our return value (tls address) is in the standard call return value
7802 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007803 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007804 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7805 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007806 }
7807
7808 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007809 // Just use the implicit TLS architecture
7810 // Need to generate someting similar to:
7811 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7812 // ; from TEB
7813 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7814 // mov rcx, qword [rdx+rcx*8]
7815 // mov eax, .tls$:tlsvar
7816 // [rax+rcx] contains the address
7817 // Windows 64bit: gs:0x58
7818 // Windows 32bit: fs:__tls_array
7819
7820 // If GV is an alias then use the aliasee for determining
7821 // thread-localness.
7822 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7823 GV = GA->resolveAliasedGlobal(false);
7824 DebugLoc dl = GA->getDebugLoc();
7825 SDValue Chain = DAG.getEntryNode();
7826
7827 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7828 // %gs:0x58 (64-bit).
7829 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7830 ? Type::getInt8PtrTy(*DAG.getContext(),
7831 256)
7832 : Type::getInt32PtrTy(*DAG.getContext(),
7833 257));
7834
7835 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7836 Subtarget->is64Bit()
7837 ? DAG.getIntPtrConstant(0x58)
7838 : DAG.getExternalSymbol("_tls_array",
7839 getPointerTy()),
7840 MachinePointerInfo(Ptr),
7841 false, false, false, 0);
7842
7843 // Load the _tls_index variable
7844 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7845 if (Subtarget->is64Bit())
7846 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7847 IDX, MachinePointerInfo(), MVT::i32,
7848 false, false, 0);
7849 else
7850 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7851 false, false, false, 0);
7852
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007853 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007854 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007855 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7856
7857 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7858 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7859 false, false, false, 0);
7860
7861 // Get the offset of start of .tls section
7862 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7863 GA->getValueType(0),
7864 GA->getOffset(), X86II::MO_SECREL);
7865 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7866
7867 // The address of the thread local variable is the add of the thread
7868 // pointer with the offset of the variable.
7869 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007870 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007871
David Blaikie4d6ccb52012-01-20 21:51:11 +00007872 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007873}
7874
Chad Rosierb90d2a92012-01-03 23:19:12 +00007875/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7876/// and take a 2 x i32 value to shift plus a shift amount.
7877SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007878 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007879 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007880 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007881 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007882 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007883 SDValue ShOpLo = Op.getOperand(0);
7884 SDValue ShOpHi = Op.getOperand(1);
7885 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007886 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007887 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007888 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007889
Dan Gohman475871a2008-07-27 21:46:04 +00007890 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007891 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007892 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7893 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007894 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007895 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7896 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007897 }
Evan Chenge3413162006-01-09 18:33:28 +00007898
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7900 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007901 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007903
Dan Gohman475871a2008-07-27 21:46:04 +00007904 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007906 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7907 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007908
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007909 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007910 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7911 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007912 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007913 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7914 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007915 }
7916
Dan Gohman475871a2008-07-27 21:46:04 +00007917 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007918 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007919}
Evan Chenga3195e82006-01-12 22:54:21 +00007920
Dan Gohmand858e902010-04-17 15:26:15 +00007921SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7922 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007923 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007924
Dale Johannesen0488fb62010-09-30 23:57:10 +00007925 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007926 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007927
Owen Anderson825b72b2009-08-11 20:47:22 +00007928 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007929 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007930
Eli Friedman36df4992009-05-27 00:47:34 +00007931 // These are really Legal; return the operand so the caller accepts it as
7932 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007934 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007936 Subtarget->is64Bit()) {
7937 return Op;
7938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007939
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007940 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007941 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007942 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007943 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007944 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007945 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007946 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007947 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007948 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007949 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7950}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007951
Owen Andersone50ed302009-08-10 22:56:29 +00007952SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007953 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007954 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007955 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007956 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007957 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007958 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007959 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007960 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007961 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007962 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007963
Chris Lattner492a43e2010-09-22 01:28:21 +00007964 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007965
Stuart Hastings84be9582011-06-02 15:57:11 +00007966 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7967 MachineMemOperand *MMO;
7968 if (FI) {
7969 int SSFI = FI->getIndex();
7970 MMO =
7971 DAG.getMachineFunction()
7972 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7973 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7974 } else {
7975 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7976 StackSlot = StackSlot.getOperand(1);
7977 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007978 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007979 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7980 X86ISD::FILD, DL,
7981 Tys, Ops, array_lengthof(Ops),
7982 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007983
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007984 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007985 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007986 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987
7988 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7989 // shouldn't be necessary except that RFP cannot be live across
7990 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007991 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007992 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7993 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007994 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007995 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007996 SDValue Ops[] = {
7997 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7998 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007999 MachineMemOperand *MMO =
8000 DAG.getMachineFunction()
8001 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008002 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008003
Chris Lattner492a43e2010-09-22 01:28:21 +00008004 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8005 Ops, array_lengthof(Ops),
8006 Op.getValueType(), MMO);
8007 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008008 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008009 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008010 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008011
Evan Cheng0db9fe62006-04-25 20:13:52 +00008012 return Result;
8013}
8014
Bill Wendling8b8a6362009-01-17 03:56:04 +00008015// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008016SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8017 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008018 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008019 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008020 movq %rax, %xmm0
8021 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8022 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8023 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008024 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008025 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008026 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008027 addpd %xmm1, %xmm0
8028 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008029 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008030
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008031 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008032 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008033
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008034 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008035 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8036 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008037 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008038
Chris Lattner97484792012-01-25 09:56:22 +00008039 SmallVector<Constant*,2> CV1;
8040 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008041 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8042 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008043 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008044 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8045 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008046 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008047 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008048
Bill Wendling397ae212012-01-05 02:13:20 +00008049 // Load the 64-bit value into an XMM register.
8050 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8051 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008052 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008053 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008054 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008055 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8056 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8057 CLod0);
8058
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008060 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008061 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008062 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008064 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065
Craig Topperd0a31172012-01-10 06:37:29 +00008066 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008067 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8068 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8069 } else {
8070 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8071 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8072 S2F, 0x4E, DAG);
8073 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8074 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8075 Sub);
8076 }
8077
8078 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008079 DAG.getIntPtrConstant(0));
8080}
8081
Bill Wendling8b8a6362009-01-17 03:56:04 +00008082// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008083SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8084 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008085 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008086 // FP constant to bias correct the final result.
8087 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008089
8090 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008092 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008093
Eli Friedmanf3704762011-08-29 21:15:46 +00008094 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008095 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008096
Owen Anderson825b72b2009-08-11 20:47:22 +00008097 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008098 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008099 DAG.getIntPtrConstant(0));
8100
8101 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008102 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008103 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008104 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008105 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008106 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008107 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 MVT::v2f64, Bias)));
8109 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008110 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008111 DAG.getIntPtrConstant(0));
8112
8113 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008114 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008115
8116 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008117 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008118
Craig Topper69947b92012-04-23 06:57:04 +00008119 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008120 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008121 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008122 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008123 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008124
8125 // Handle final rounding.
8126 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008127}
8128
Michael Liaoa7554632012-10-23 17:36:08 +00008129SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8130 SelectionDAG &DAG) const {
8131 SDValue N0 = Op.getOperand(0);
8132 EVT SVT = N0.getValueType();
8133 DebugLoc dl = Op.getDebugLoc();
8134
8135 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8136 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8137 "Custom UINT_TO_FP is not supported!");
8138
Craig Topperb99bafe2013-01-21 06:21:54 +00008139 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8140 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008141 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8142 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8143}
8144
Dan Gohmand858e902010-04-17 15:26:15 +00008145SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8146 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008147 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008148 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008149
Michael Liaoa7554632012-10-23 17:36:08 +00008150 if (Op.getValueType().isVector())
8151 return lowerUINT_TO_FP_vec(Op, DAG);
8152
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008153 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008154 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8155 // the optimization here.
8156 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008157 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008158
Owen Andersone50ed302009-08-10 22:56:29 +00008159 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008160 EVT DstVT = Op.getValueType();
8161 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008162 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008163 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008164 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008165 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008166 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008167
8168 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008169 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008170 if (SrcVT == MVT::i32) {
8171 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8172 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8173 getPointerTy(), StackSlot, WordOff);
8174 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008175 StackSlot, MachinePointerInfo(),
8176 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008177 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008178 OffsetSlot, MachinePointerInfo(),
8179 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008180 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8181 return Fild;
8182 }
8183
8184 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8185 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008186 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008187 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008188 // For i64 source, we need to add the appropriate power of 2 if the input
8189 // was negative. This is the same as the optimization in
8190 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8191 // we must be careful to do the computation in x87 extended precision, not
8192 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008193 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8194 MachineMemOperand *MMO =
8195 DAG.getMachineFunction()
8196 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8197 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008198
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008199 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8200 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008201 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8202 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008203
8204 APInt FF(32, 0x5F800000ULL);
8205
8206 // Check whether the sign bit is set.
8207 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8208 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8209 ISD::SETLT);
8210
8211 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8212 SDValue FudgePtr = DAG.getConstantPool(
8213 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8214 getPointerTy());
8215
8216 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8217 SDValue Zero = DAG.getIntPtrConstant(0);
8218 SDValue Four = DAG.getIntPtrConstant(4);
8219 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8220 Zero, Four);
8221 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8222
8223 // Load the value out, extending it from f32 to f80.
8224 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008225 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008226 FudgePtr, MachinePointerInfo::getConstantPool(),
8227 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008228 // Extend everything to 80 bits to force it to be done on x87.
8229 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8230 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008231}
8232
Craig Topperb99bafe2013-01-21 06:21:54 +00008233std::pair<SDValue,SDValue>
8234X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8235 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008236 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008237
Owen Andersone50ed302009-08-10 22:56:29 +00008238 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008239
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008240 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008241 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8242 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008243 }
8244
Owen Anderson825b72b2009-08-11 20:47:22 +00008245 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8246 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008247 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008248
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008249 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008251 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008252 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008253 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008254 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008255 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008256 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008257
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008258 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8259 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008260 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008261 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008262 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008263 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008264
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008266 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8267 Opc = X86ISD::WIN_FTOL;
8268 else
8269 switch (DstTy.getSimpleVT().SimpleTy) {
8270 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8271 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8272 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8273 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8274 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008275
Dan Gohman475871a2008-07-27 21:46:04 +00008276 SDValue Chain = DAG.getEntryNode();
8277 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008278 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008279 // FIXME This causes a redundant load/store if the SSE-class value is already
8280 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008281 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008282 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008283 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008284 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008285 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008287 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008288 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008289 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008290
Chris Lattner492a43e2010-09-22 01:28:21 +00008291 MachineMemOperand *MMO =
8292 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8293 MachineMemOperand::MOLoad, MemSize, MemSize);
8294 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8295 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008296 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008297 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008298 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8299 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008300
Chris Lattner07290932010-09-22 01:05:16 +00008301 MachineMemOperand *MMO =
8302 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8303 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008304
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008305 if (Opc != X86ISD::WIN_FTOL) {
8306 // Build the FP_TO_INT*_IN_MEM
8307 SDValue Ops[] = { Chain, Value, StackSlot };
8308 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8309 Ops, 3, DstTy, MMO);
8310 return std::make_pair(FIST, StackSlot);
8311 } else {
8312 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8313 DAG.getVTList(MVT::Other, MVT::Glue),
8314 Chain, Value);
8315 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8316 MVT::i32, ftol.getValue(1));
8317 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8318 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008319 SDValue Ops[] = { eax, edx };
8320 SDValue pair = IsReplace
8321 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8322 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008323 return std::make_pair(pair, SDValue());
8324 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008325}
8326
Nadav Rotem0509db22012-12-28 05:45:24 +00008327static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8328 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008329 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008330 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008331 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008332 DebugLoc dl = Op->getDebugLoc();
8333
8334 // Optimize vectors in AVX mode:
8335 //
8336 // v8i16 -> v8i32
8337 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8338 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8339 // Concat upper and lower parts.
8340 //
8341 // v4i32 -> v4i64
8342 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8343 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8344 // Concat upper and lower parts.
8345 //
8346
8347 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8348 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8349 return SDValue();
8350
8351 if (Subtarget->hasInt256())
8352 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8353
8354 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8355 SDValue Undef = DAG.getUNDEF(InVT);
8356 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8357 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8358 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8359
Craig Toppera080daf2013-01-20 21:50:27 +00008360 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008361 VT.getVectorNumElements()/2);
8362
8363 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8364 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8365
8366 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8367}
8368
8369SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8370 SelectionDAG &DAG) const {
8371 if (Subtarget->hasFp256()) {
8372 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8373 if (Res.getNode())
8374 return Res;
8375 }
8376
8377 return SDValue();
8378}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008379SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8380 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008381 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008382 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008383 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008384 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008385
Nadav Rotem0509db22012-12-28 05:45:24 +00008386 if (Subtarget->hasFp256()) {
8387 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8388 if (Res.getNode())
8389 return Res;
8390 }
8391
Michael Liaoa7554632012-10-23 17:36:08 +00008392 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8393 VT.getVectorNumElements() != SVT.getVectorNumElements())
8394 return SDValue();
8395
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008396 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008397
8398 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008399 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008400 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8401
8402 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8403 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8404 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008405 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8406 DAG.getUNDEF(MVT::v8i16),
8407 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008408
8409 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8410}
8411
Craig Topperd713c0f2013-01-20 21:34:37 +00008412SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008413 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008414 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008415 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008416 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008417
Nadav Rotem3c22a442012-12-27 07:45:10 +00008418 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8419 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8420 if (Subtarget->hasInt256()) {
8421 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8422 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8423 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8424 ShufMask);
8425 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8426 DAG.getIntPtrConstant(0));
8427 }
8428
8429 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8430 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8431 DAG.getIntPtrConstant(0));
8432 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8433 DAG.getIntPtrConstant(2));
8434
8435 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8436 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8437
8438 // The PSHUFD mask:
8439 static const int ShufMask1[] = {0, 2, 0, 0};
8440 SDValue Undef = DAG.getUNDEF(VT);
8441 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8442 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8443
8444 // The MOVLHPS mask:
8445 static const int ShufMask2[] = {0, 1, 4, 5};
8446 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8447 }
8448
8449 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8450 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8451 if (Subtarget->hasInt256()) {
8452 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8453
8454 SmallVector<SDValue,32> pshufbMask;
8455 for (unsigned i = 0; i < 2; ++i) {
8456 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8457 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8458 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8459 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8460 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8461 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8462 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8463 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8464 for (unsigned j = 0; j < 8; ++j)
8465 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8466 }
8467 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8468 &pshufbMask[0], 32);
8469 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8470 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8471
8472 static const int ShufMask[] = {0, 2, -1, -1};
8473 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8474 &ShufMask[0]);
8475 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8476 DAG.getIntPtrConstant(0));
8477 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8478 }
8479
8480 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8481 DAG.getIntPtrConstant(0));
8482
8483 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8484 DAG.getIntPtrConstant(4));
8485
8486 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8487 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8488
8489 // The PSHUFB mask:
8490 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8491 -1, -1, -1, -1, -1, -1, -1, -1};
8492
8493 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8494 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8495 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8496
8497 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8498 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8499
8500 // The MOVLHPS Mask:
8501 static const int ShufMask2[] = {0, 1, 4, 5};
8502 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8503 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8504 }
8505
8506 // Handle truncation of V256 to V128 using shuffles.
8507 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008508 return SDValue();
8509
Nadav Rotem3c22a442012-12-27 07:45:10 +00008510 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8511 "Invalid op");
8512 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008513
8514 unsigned NumElems = VT.getVectorNumElements();
8515 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8516 NumElems * 2);
8517
Michael Liaobedcbd42012-10-16 18:14:11 +00008518 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8519 // Prepare truncation shuffle mask
8520 for (unsigned i = 0; i != NumElems; ++i)
8521 MaskVec[i] = i * 2;
8522 SDValue V = DAG.getVectorShuffle(NVT, DL,
8523 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8524 DAG.getUNDEF(NVT), &MaskVec[0]);
8525 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8526 DAG.getIntPtrConstant(0));
8527}
8528
Dan Gohmand858e902010-04-17 15:26:15 +00008529SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8530 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008531 MVT VT = Op.getValueType().getSimpleVT();
8532 if (VT.isVector()) {
8533 if (VT == MVT::v8i16)
8534 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008535 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8536 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008537 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008538 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008539
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008540 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8541 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008542 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008543 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8544 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008545
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008546 if (StackSlot.getNode())
8547 // Load the result.
8548 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8549 FIST, StackSlot, MachinePointerInfo(),
8550 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008551
8552 // The node is the result.
8553 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008554}
8555
Dan Gohmand858e902010-04-17 15:26:15 +00008556SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8557 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008558 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8559 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008560 SDValue FIST = Vals.first, StackSlot = Vals.second;
8561 assert(FIST.getNode() && "Unexpected failure");
8562
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008563 if (StackSlot.getNode())
8564 // Load the result.
8565 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8566 FIST, StackSlot, MachinePointerInfo(),
8567 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008568
8569 // The node is the result.
8570 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008571}
8572
Craig Topperb84b4232013-01-21 06:13:28 +00008573static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008574 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008575 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008576 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008577 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008578
8579 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8580
8581 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8582 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8583 In, DAG.getUNDEF(SVT)));
8584}
8585
Craig Topper43620672012-09-08 07:31:51 +00008586SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008587 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008588 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008589 MVT VT = Op.getValueType().getSimpleVT();
8590 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008591 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8592 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008593 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008594 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008595 }
Craig Topper43620672012-09-08 07:31:51 +00008596 Constant *C;
8597 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008598 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8599 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008600 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008601 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8602 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008603 C = ConstantVector::getSplat(NumElts, C);
8604 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 Topper43620672012-09-08 07:31:51 +00008608 false, false, false, Alignment);
8609 if (VT.isVector()) {
8610 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8611 return DAG.getNode(ISD::BITCAST, dl, VT,
8612 DAG.getNode(ISD::AND, dl, ANDVT,
8613 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8614 Op.getOperand(0)),
8615 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8616 }
Dale Johannesenace16102009-02-03 19:33:06 +00008617 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008618}
8619
Dan Gohmand858e902010-04-17 15:26:15 +00008620SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008621 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008622 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008623 MVT VT = Op.getValueType().getSimpleVT();
8624 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008625 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8626 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008627 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008628 NumElts = VT.getVectorNumElements();
8629 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008630 Constant *C;
8631 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008632 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8633 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008634 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008635 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8636 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008637 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008638 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8639 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008640 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008641 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008642 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008643 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008644 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008645 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008646 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008647 DAG.getNode(ISD::BITCAST, dl, XORVT,
8648 Op.getOperand(0)),
8649 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008650 }
Craig Topper69947b92012-04-23 06:57:04 +00008651
8652 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008653}
8654
Dan Gohmand858e902010-04-17 15:26:15 +00008655SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008656 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008657 SDValue Op0 = Op.getOperand(0);
8658 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008659 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008660 MVT VT = Op.getValueType().getSimpleVT();
8661 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008662
8663 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008664 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008665 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008666 SrcVT = VT;
8667 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008668 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008669 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008670 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008671 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008672 }
8673
8674 // At this point the operands and the result should have the same
8675 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008676
Evan Cheng68c47cb2007-01-05 07:55:56 +00008677 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008678 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008679 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008680 const fltSemantics &Sem = APFloat::IEEEdouble;
8681 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8682 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008683 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008684 const fltSemantics &Sem = APFloat::IEEEsingle;
8685 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8686 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8687 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8688 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008689 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008690 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008691 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008692 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008693 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008694 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008695 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008696
8697 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008698 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008699 // Op0 is MVT::f32, Op1 is MVT::f64.
8700 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8701 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8702 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008703 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008705 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008706 }
8707
Evan Cheng73d6cf12007-01-05 21:37:56 +00008708 // Clear first operand sign bit.
8709 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008711 const fltSemantics &Sem = APFloat::IEEEdouble;
8712 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8713 APInt(64, ~(1ULL << 63)))));
8714 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008715 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008716 const fltSemantics &Sem = APFloat::IEEEsingle;
8717 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8718 APInt(32, ~(1U << 31)))));
8719 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8720 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8721 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008722 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008723 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008724 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008725 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008726 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008727 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008728 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008729
8730 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008731 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008732}
8733
Craig Topper55b24052012-09-11 06:15:32 +00008734static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008735 SDValue N0 = Op.getOperand(0);
8736 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008737 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008738
8739 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8740 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8741 DAG.getConstant(1, VT));
8742 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8743}
8744
Michael Liaof966e4e2012-09-13 20:24:54 +00008745// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8746//
Craig Topperb99bafe2013-01-21 06:21:54 +00008747SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8748 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008749 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8750
8751 if (!Subtarget->hasSSE41())
8752 return SDValue();
8753
8754 if (!Op->hasOneUse())
8755 return SDValue();
8756
8757 SDNode *N = Op.getNode();
8758 DebugLoc DL = N->getDebugLoc();
8759
8760 SmallVector<SDValue, 8> Opnds;
8761 DenseMap<SDValue, unsigned> VecInMap;
8762 EVT VT = MVT::Other;
8763
8764 // Recognize a special case where a vector is casted into wide integer to
8765 // test all 0s.
8766 Opnds.push_back(N->getOperand(0));
8767 Opnds.push_back(N->getOperand(1));
8768
8769 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8770 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8771 // BFS traverse all OR'd operands.
8772 if (I->getOpcode() == ISD::OR) {
8773 Opnds.push_back(I->getOperand(0));
8774 Opnds.push_back(I->getOperand(1));
8775 // Re-evaluate the number of nodes to be traversed.
8776 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8777 continue;
8778 }
8779
8780 // Quit if a non-EXTRACT_VECTOR_ELT
8781 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8782 return SDValue();
8783
8784 // Quit if without a constant index.
8785 SDValue Idx = I->getOperand(1);
8786 if (!isa<ConstantSDNode>(Idx))
8787 return SDValue();
8788
8789 SDValue ExtractedFromVec = I->getOperand(0);
8790 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8791 if (M == VecInMap.end()) {
8792 VT = ExtractedFromVec.getValueType();
8793 // Quit if not 128/256-bit vector.
8794 if (!VT.is128BitVector() && !VT.is256BitVector())
8795 return SDValue();
8796 // Quit if not the same type.
8797 if (VecInMap.begin() != VecInMap.end() &&
8798 VT != VecInMap.begin()->first.getValueType())
8799 return SDValue();
8800 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8801 }
8802 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8803 }
8804
8805 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008806 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008807
8808 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8809 SmallVector<SDValue, 8> VecIns;
8810
8811 for (DenseMap<SDValue, unsigned>::const_iterator
8812 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8813 // Quit if not all elements are used.
8814 if (I->second != FullMask)
8815 return SDValue();
8816 VecIns.push_back(I->first);
8817 }
8818
8819 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8820
8821 // Cast all vectors into TestVT for PTEST.
8822 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8823 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8824
8825 // If more than one full vectors are evaluated, OR them first before PTEST.
8826 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8827 // Each iteration will OR 2 nodes and append the result until there is only
8828 // 1 node left, i.e. the final OR'd value of all vectors.
8829 SDValue LHS = VecIns[Slot];
8830 SDValue RHS = VecIns[Slot + 1];
8831 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8832 }
8833
8834 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8835 VecIns.back(), VecIns.back());
8836}
8837
Dan Gohman076aee32009-03-04 19:44:21 +00008838/// Emit nodes that will be selected as "test Op0,Op0", or something
8839/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008840SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008841 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008842 DebugLoc dl = Op.getDebugLoc();
8843
Dan Gohman31125812009-03-07 01:58:32 +00008844 // CF and OF aren't always set the way we want. Determine which
8845 // of these we need.
8846 bool NeedCF = false;
8847 bool NeedOF = false;
8848 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008849 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008850 case X86::COND_A: case X86::COND_AE:
8851 case X86::COND_B: case X86::COND_BE:
8852 NeedCF = true;
8853 break;
8854 case X86::COND_G: case X86::COND_GE:
8855 case X86::COND_L: case X86::COND_LE:
8856 case X86::COND_O: case X86::COND_NO:
8857 NeedOF = true;
8858 break;
Dan Gohman31125812009-03-07 01:58:32 +00008859 }
8860
Dan Gohman076aee32009-03-04 19:44:21 +00008861 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008862 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8863 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008864 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8865 // Emit a CMP with 0, which is the TEST pattern.
8866 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8867 DAG.getConstant(0, Op.getValueType()));
8868
8869 unsigned Opcode = 0;
8870 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008871
8872 // Truncate operations may prevent the merge of the SETCC instruction
8873 // and the arithmetic intruction before it. Attempt to truncate the operands
8874 // of the arithmetic instruction and use a reduced bit-width instruction.
8875 bool NeedTruncation = false;
8876 SDValue ArithOp = Op;
8877 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8878 SDValue Arith = Op->getOperand(0);
8879 // Both the trunc and the arithmetic op need to have one user each.
8880 if (Arith->hasOneUse())
8881 switch (Arith.getOpcode()) {
8882 default: break;
8883 case ISD::ADD:
8884 case ISD::SUB:
8885 case ISD::AND:
8886 case ISD::OR:
8887 case ISD::XOR: {
8888 NeedTruncation = true;
8889 ArithOp = Arith;
8890 }
8891 }
8892 }
8893
8894 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8895 // which may be the result of a CAST. We use the variable 'Op', which is the
8896 // non-casted variable when we check for possible users.
8897 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008898 case ISD::ADD:
8899 // Due to an isel shortcoming, be conservative if this add is likely to be
8900 // selected as part of a load-modify-store instruction. When the root node
8901 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8902 // uses of other nodes in the match, such as the ADD in this case. This
8903 // leads to the ADD being left around and reselected, with the result being
8904 // two adds in the output. Alas, even if none our users are stores, that
8905 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8906 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8907 // climbing the DAG back to the root, and it doesn't seem to be worth the
8908 // effort.
8909 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008910 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8911 if (UI->getOpcode() != ISD::CopyToReg &&
8912 UI->getOpcode() != ISD::SETCC &&
8913 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008914 goto default_case;
8915
8916 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008917 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008918 // An add of one will be selected as an INC.
8919 if (C->getAPIntValue() == 1) {
8920 Opcode = X86ISD::INC;
8921 NumOperands = 1;
8922 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008923 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008924
8925 // An add of negative one (subtract of one) will be selected as a DEC.
8926 if (C->getAPIntValue().isAllOnesValue()) {
8927 Opcode = X86ISD::DEC;
8928 NumOperands = 1;
8929 break;
8930 }
Dan Gohman076aee32009-03-04 19:44:21 +00008931 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008932
8933 // Otherwise use a regular EFLAGS-setting add.
8934 Opcode = X86ISD::ADD;
8935 NumOperands = 2;
8936 break;
8937 case ISD::AND: {
8938 // If the primary and result isn't used, don't bother using X86ISD::AND,
8939 // because a TEST instruction will be better.
8940 bool NonFlagUse = false;
8941 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8942 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8943 SDNode *User = *UI;
8944 unsigned UOpNo = UI.getOperandNo();
8945 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8946 // Look pass truncate.
8947 UOpNo = User->use_begin().getOperandNo();
8948 User = *User->use_begin();
8949 }
8950
8951 if (User->getOpcode() != ISD::BRCOND &&
8952 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008953 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008954 NonFlagUse = true;
8955 break;
8956 }
Dan Gohman076aee32009-03-04 19:44:21 +00008957 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008958
8959 if (!NonFlagUse)
8960 break;
8961 }
8962 // FALL THROUGH
8963 case ISD::SUB:
8964 case ISD::OR:
8965 case ISD::XOR:
8966 // Due to the ISEL shortcoming noted above, be conservative if this op is
8967 // likely to be selected as part of a load-modify-store instruction.
8968 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8969 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8970 if (UI->getOpcode() == ISD::STORE)
8971 goto default_case;
8972
8973 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008974 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008975 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008976 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008977 case ISD::XOR: Opcode = X86ISD::XOR; break;
8978 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008979 case ISD::OR: {
8980 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8981 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8982 if (EFLAGS.getNode())
8983 return EFLAGS;
8984 }
8985 Opcode = X86ISD::OR;
8986 break;
8987 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008988 }
8989
8990 NumOperands = 2;
8991 break;
8992 case X86ISD::ADD:
8993 case X86ISD::SUB:
8994 case X86ISD::INC:
8995 case X86ISD::DEC:
8996 case X86ISD::OR:
8997 case X86ISD::XOR:
8998 case X86ISD::AND:
8999 return SDValue(Op.getNode(), 1);
9000 default:
9001 default_case:
9002 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009003 }
9004
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009005 // If we found that truncation is beneficial, perform the truncation and
9006 // update 'Op'.
9007 if (NeedTruncation) {
9008 EVT VT = Op.getValueType();
9009 SDValue WideVal = Op->getOperand(0);
9010 EVT WideVT = WideVal.getValueType();
9011 unsigned ConvertedOp = 0;
9012 // Use a target machine opcode to prevent further DAGCombine
9013 // optimizations that may separate the arithmetic operations
9014 // from the setcc node.
9015 switch (WideVal.getOpcode()) {
9016 default: break;
9017 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9018 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9019 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9020 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9021 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9022 }
9023
9024 if (ConvertedOp) {
9025 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9026 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9027 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9028 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9029 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9030 }
9031 }
9032 }
9033
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009034 if (Opcode == 0)
9035 // Emit a CMP with 0, which is the TEST pattern.
9036 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9037 DAG.getConstant(0, Op.getValueType()));
9038
9039 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9040 SmallVector<SDValue, 4> Ops;
9041 for (unsigned i = 0; i != NumOperands; ++i)
9042 Ops.push_back(Op.getOperand(i));
9043
9044 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9045 DAG.ReplaceAllUsesWith(Op, New);
9046 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009047}
9048
9049/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9050/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009051SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009052 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009053 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9054 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009055 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009056
9057 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009058 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9059 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9060 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9061 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9062 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9063 Op0, Op1);
9064 return SDValue(Sub.getNode(), 1);
9065 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009066 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009067}
9068
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009069/// Convert a comparison if required by the subtarget.
9070SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9071 SelectionDAG &DAG) const {
9072 // If the subtarget does not support the FUCOMI instruction, floating-point
9073 // comparisons have to be converted.
9074 if (Subtarget->hasCMov() ||
9075 Cmp.getOpcode() != X86ISD::CMP ||
9076 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9077 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9078 return Cmp;
9079
9080 // The instruction selector will select an FUCOM instruction instead of
9081 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9082 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9083 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9084 DebugLoc dl = Cmp.getDebugLoc();
9085 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9086 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9087 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9088 DAG.getConstant(8, MVT::i8));
9089 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9090 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9091}
9092
Evan Cheng4e544802012-12-05 00:10:38 +00009093static bool isAllOnes(SDValue V) {
9094 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9095 return C && C->isAllOnesValue();
9096}
9097
Evan Chengd40d03e2010-01-06 19:38:29 +00009098/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9099/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009100SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9101 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009102 SDValue Op0 = And.getOperand(0);
9103 SDValue Op1 = And.getOperand(1);
9104 if (Op0.getOpcode() == ISD::TRUNCATE)
9105 Op0 = Op0.getOperand(0);
9106 if (Op1.getOpcode() == ISD::TRUNCATE)
9107 Op1 = Op1.getOperand(0);
9108
Evan Chengd40d03e2010-01-06 19:38:29 +00009109 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009110 if (Op1.getOpcode() == ISD::SHL)
9111 std::swap(Op0, Op1);
9112 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009113 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9114 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009115 // If we looked past a truncate, check that it's only truncating away
9116 // known zeros.
9117 unsigned BitWidth = Op0.getValueSizeInBits();
9118 unsigned AndBitWidth = And.getValueSizeInBits();
9119 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009120 APInt Zeros, Ones;
9121 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009122 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9123 return SDValue();
9124 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009125 LHS = Op1;
9126 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009127 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009128 } else if (Op1.getOpcode() == ISD::Constant) {
9129 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009130 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009131 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009132
9133 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009134 LHS = AndLHS.getOperand(0);
9135 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009136 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009137
9138 // Use BT if the immediate can't be encoded in a TEST instruction.
9139 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9140 LHS = AndLHS;
9141 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9142 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009143 }
Evan Cheng0488db92007-09-25 01:57:46 +00009144
Evan Chengd40d03e2010-01-06 19:38:29 +00009145 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009146 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9147 // the condition code later.
9148 bool Invert = false;
9149 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9150 Invert = true;
9151 LHS = LHS.getOperand(0);
9152 }
9153
Evan Chenge5b51ac2010-04-17 06:13:15 +00009154 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009155 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009156 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009157 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009158 // Also promote i16 to i32 for performance / code size reason.
9159 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009160 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009161 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009162
Evan Chengd40d03e2010-01-06 19:38:29 +00009163 // If the operand types disagree, extend the shift amount to match. Since
9164 // BT ignores high bits (like shifts) we can use anyextend.
9165 if (LHS.getValueType() != RHS.getValueType())
9166 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009167
Evan Chengd40d03e2010-01-06 19:38:29 +00009168 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009169 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9170 // Flip the condition if the LHS was a not instruction
9171 if (Invert)
9172 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009173 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9174 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009175 }
9176
Evan Cheng54de3ea2010-01-05 06:52:31 +00009177 return SDValue();
9178}
9179
Craig Topper89af15e2011-09-18 08:03:58 +00009180// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009181// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009182static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009183 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009184
Craig Topper7a9a28b2012-08-12 02:23:29 +00009185 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009186 "Unsupported value type for operation");
9187
Craig Topper66ddd152012-04-27 22:54:43 +00009188 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009189 DebugLoc dl = Op.getDebugLoc();
9190 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009191
9192 // Extract the LHS vectors
9193 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009194 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9195 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009196
9197 // Extract the RHS vectors
9198 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009199 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9200 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009201
9202 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009203 MVT EltVT = VT.getVectorElementType();
9204 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009205 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9206 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9207 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9208}
9209
Craig Topper26827f32013-01-20 09:02:22 +00009210static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9211 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009212 SDValue Cond;
9213 SDValue Op0 = Op.getOperand(0);
9214 SDValue Op1 = Op.getOperand(1);
9215 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009216 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009217 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009218 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009219 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009220
9221 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009222#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009223 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009224 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9225#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009226
Craig Topper523908d2012-08-13 02:34:03 +00009227 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009228 bool Swap = false;
9229
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009230 // SSE Condition code mapping:
9231 // 0 - EQ
9232 // 1 - LT
9233 // 2 - LE
9234 // 3 - UNORD
9235 // 4 - NEQ
9236 // 5 - NLT
9237 // 6 - NLE
9238 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009239 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009240 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009241 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009242 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009243 case ISD::SETOGT:
9244 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009245 case ISD::SETLT:
9246 case ISD::SETOLT: SSECC = 1; break;
9247 case ISD::SETOGE:
9248 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009249 case ISD::SETLE:
9250 case ISD::SETOLE: SSECC = 2; break;
9251 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009252 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009253 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009254 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009255 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009256 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009257 case ISD::SETUGT: SSECC = 6; break;
9258 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009259 case ISD::SETUEQ:
9260 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009261 }
9262 if (Swap)
9263 std::swap(Op0, Op1);
9264
Nate Begemanfb8ead02008-07-25 19:05:58 +00009265 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009266 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009267 unsigned CC0, CC1;
9268 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009269 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009270 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9271 } else {
9272 assert(SetCCOpcode == ISD::SETONE);
9273 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009274 }
Craig Topper523908d2012-08-13 02:34:03 +00009275
9276 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9277 DAG.getConstant(CC0, MVT::i8));
9278 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9279 DAG.getConstant(CC1, MVT::i8));
9280 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009281 }
9282 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009283 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9284 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009285 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009286
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009287 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009288 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009289 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009290
Nate Begeman30a0de92008-07-17 16:51:19 +00009291 // We are handling one of the integer comparisons here. Since SSE only has
9292 // GT and EQ comparisons for integer, swapping operands and multiple
9293 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009294 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009295 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009296
Nate Begeman30a0de92008-07-17 16:51:19 +00009297 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009298 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009299 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009300 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009301 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009302 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009303 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009304 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009305 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009306 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009307 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009308 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009309 }
9310 if (Swap)
9311 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009312
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009313 // Check that the operation in question is available (most are plain SSE2,
9314 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009315 if (VT == MVT::v2i64) {
9316 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9317 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009318 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9319 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009320 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009321 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9322
9323 // First cast everything to the right type,
9324 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9325 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9326
9327 // Do the compare.
9328 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9329
9330 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009331 const int Mask[] = { 1, 0, 3, 2 };
9332 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9333 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009334
9335 if (Invert)
9336 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9337
9338 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9339 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009340 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009341
Nate Begeman30a0de92008-07-17 16:51:19 +00009342 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9343 // bits of the inputs before performing those operations.
9344 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009345 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009346 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9347 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009348 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009349 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9350 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009351 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9352 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009353 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009354
Dale Johannesenace16102009-02-03 19:33:06 +00009355 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009356
9357 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009358 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009359 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009360
Nate Begeman30a0de92008-07-17 16:51:19 +00009361 return Result;
9362}
Evan Cheng0488db92007-09-25 01:57:46 +00009363
Craig Topper26827f32013-01-20 09:02:22 +00009364SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9365
9366 MVT VT = Op.getValueType().getSimpleVT();
9367
9368 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9369
9370 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9371 SDValue Op0 = Op.getOperand(0);
9372 SDValue Op1 = Op.getOperand(1);
9373 DebugLoc dl = Op.getDebugLoc();
9374 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9375
9376 // Optimize to BT if possible.
9377 // Lower (X & (1 << N)) == 0 to BT(X, N).
9378 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9379 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9380 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9381 Op1.getOpcode() == ISD::Constant &&
9382 cast<ConstantSDNode>(Op1)->isNullValue() &&
9383 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9384 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9385 if (NewSetCC.getNode())
9386 return NewSetCC;
9387 }
9388
9389 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9390 // these.
9391 if (Op1.getOpcode() == ISD::Constant &&
9392 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9393 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9394 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9395
9396 // If the input is a setcc, then reuse the input setcc or use a new one with
9397 // the inverted condition.
9398 if (Op0.getOpcode() == X86ISD::SETCC) {
9399 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9400 bool Invert = (CC == ISD::SETNE) ^
9401 cast<ConstantSDNode>(Op1)->isNullValue();
9402 if (!Invert) return Op0;
9403
9404 CCode = X86::GetOppositeBranchCondition(CCode);
9405 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9406 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9407 }
9408 }
9409
9410 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9411 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9412 if (X86CC == X86::COND_INVALID)
9413 return SDValue();
9414
9415 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9416 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9417 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9418 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9419}
9420
Evan Cheng370e5342008-12-03 08:38:43 +00009421// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009422static bool isX86LogicalCmp(SDValue Op) {
9423 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009424 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9425 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009426 return true;
9427 if (Op.getResNo() == 1 &&
9428 (Opc == X86ISD::ADD ||
9429 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009430 Opc == X86ISD::ADC ||
9431 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009432 Opc == X86ISD::SMUL ||
9433 Opc == X86ISD::UMUL ||
9434 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009435 Opc == X86ISD::DEC ||
9436 Opc == X86ISD::OR ||
9437 Opc == X86ISD::XOR ||
9438 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009439 return true;
9440
Chris Lattner9637d5b2010-12-05 07:49:54 +00009441 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9442 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009443
Dan Gohman076aee32009-03-04 19:44:21 +00009444 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009445}
9446
Chris Lattnera2b56002010-12-05 01:23:24 +00009447static bool isZero(SDValue V) {
9448 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9449 return C && C->isNullValue();
9450}
9451
Evan Chengb64dd5f2012-08-07 22:21:00 +00009452static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9453 if (V.getOpcode() != ISD::TRUNCATE)
9454 return false;
9455
9456 SDValue VOp0 = V.getOperand(0);
9457 unsigned InBits = VOp0.getValueSizeInBits();
9458 unsigned Bits = V.getValueSizeInBits();
9459 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9460}
9461
Dan Gohmand858e902010-04-17 15:26:15 +00009462SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009463 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009464 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009465 SDValue Op1 = Op.getOperand(1);
9466 SDValue Op2 = Op.getOperand(2);
9467 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009468 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009469
Dan Gohman1a492952009-10-20 16:22:37 +00009470 if (Cond.getOpcode() == ISD::SETCC) {
9471 SDValue NewCond = LowerSETCC(Cond, DAG);
9472 if (NewCond.getNode())
9473 Cond = NewCond;
9474 }
Evan Cheng734503b2006-09-11 02:19:56 +00009475
Chris Lattnera2b56002010-12-05 01:23:24 +00009476 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009477 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009478 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009479 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009480 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009481 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9482 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009483 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009484
Chris Lattnera2b56002010-12-05 01:23:24 +00009485 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009486
9487 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009488 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9489 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009490
9491 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009492 // Apply further optimizations for special cases
9493 // (select (x != 0), -1, 0) -> neg & sbb
9494 // (select (x == 0), 0, -1) -> neg & sbb
9495 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009496 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009497 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9498 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009499 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9500 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009501 CmpOp0);
9502 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9503 DAG.getConstant(X86::COND_B, MVT::i8),
9504 SDValue(Neg.getNode(), 1));
9505 return Res;
9506 }
9507
Chris Lattnera2b56002010-12-05 01:23:24 +00009508 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9509 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009510 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009511
Chris Lattner96908b12010-12-05 02:00:51 +00009512 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009513 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9514 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009515
Chris Lattner96908b12010-12-05 02:00:51 +00009516 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9517 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009518
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009519 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009520 if (N2C == 0 || !N2C->isNullValue())
9521 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9522 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009523 }
9524 }
9525
Chris Lattnera2b56002010-12-05 01:23:24 +00009526 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009527 if (Cond.getOpcode() == ISD::AND &&
9528 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9529 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009530 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009531 Cond = Cond.getOperand(0);
9532 }
9533
Evan Cheng3f41d662007-10-08 22:16:29 +00009534 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9535 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009536 unsigned CondOpcode = Cond.getOpcode();
9537 if (CondOpcode == X86ISD::SETCC ||
9538 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009539 CC = Cond.getOperand(0);
9540
Dan Gohman475871a2008-07-27 21:46:04 +00009541 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009542 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009543 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009544
Evan Cheng3f41d662007-10-08 22:16:29 +00009545 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009546 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009547 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009548 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009549
Chris Lattnerd1980a52009-03-12 06:52:53 +00009550 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9551 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009552 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009553 addTest = false;
9554 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009555 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9556 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9557 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9558 Cond.getOperand(0).getValueType() != MVT::i8)) {
9559 SDValue LHS = Cond.getOperand(0);
9560 SDValue RHS = Cond.getOperand(1);
9561 unsigned X86Opcode;
9562 unsigned X86Cond;
9563 SDVTList VTs;
9564 switch (CondOpcode) {
9565 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9566 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9567 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9568 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9569 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9570 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9571 default: llvm_unreachable("unexpected overflowing operator");
9572 }
9573 if (CondOpcode == ISD::UMULO)
9574 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9575 MVT::i32);
9576 else
9577 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9578
9579 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9580
9581 if (CondOpcode == ISD::UMULO)
9582 Cond = X86Op.getValue(2);
9583 else
9584 Cond = X86Op.getValue(1);
9585
9586 CC = DAG.getConstant(X86Cond, MVT::i8);
9587 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009588 }
9589
9590 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009591 // Look pass the truncate if the high bits are known zero.
9592 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9593 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009594
9595 // We know the result of AND is compared against zero. Try to match
9596 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009597 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009598 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009599 if (NewSetCC.getNode()) {
9600 CC = NewSetCC.getOperand(0);
9601 Cond = NewSetCC.getOperand(1);
9602 addTest = false;
9603 }
9604 }
9605 }
9606
9607 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009609 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009610 }
9611
Benjamin Kramere915ff32010-12-22 23:09:28 +00009612 // a < b ? -1 : 0 -> RES = ~setcc_carry
9613 // a < b ? 0 : -1 -> RES = setcc_carry
9614 // a >= b ? -1 : 0 -> RES = setcc_carry
9615 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009616 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009617 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009618 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9619
9620 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9621 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9622 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9623 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9624 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9625 return DAG.getNOT(DL, Res, Res.getValueType());
9626 return Res;
9627 }
9628 }
9629
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009630 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9631 // widen the cmov and push the truncate through. This avoids introducing a new
9632 // branch during isel and doesn't add any extensions.
9633 if (Op.getValueType() == MVT::i8 &&
9634 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9635 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9636 if (T1.getValueType() == T2.getValueType() &&
9637 // Blacklist CopyFromReg to avoid partial register stalls.
9638 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9639 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009640 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009641 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9642 }
9643 }
9644
Evan Cheng0488db92007-09-25 01:57:46 +00009645 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9646 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009647 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009648 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009649 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009650}
9651
Nadav Rotem1a330af2012-12-27 22:47:16 +00009652SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9653 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009654 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009655 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009656 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009657 DebugLoc dl = Op->getDebugLoc();
9658
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009659 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9660 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9661 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009662
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009663 if (Subtarget->hasInt256())
9664 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009665
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009666 // Optimize vectors in AVX mode
9667 // Sign extend v8i16 to v8i32 and
9668 // v4i32 to v4i64
9669 //
9670 // Divide input vector into two parts
9671 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9672 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9673 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009674
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009675 unsigned NumElems = InVT.getVectorNumElements();
9676 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009677
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009678 SmallVector<int,8> ShufMask1(NumElems, -1);
9679 for (unsigned i = 0; i != NumElems/2; ++i)
9680 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009681
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009682 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009683
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009684 SmallVector<int,8> ShufMask2(NumElems, -1);
9685 for (unsigned i = 0; i != NumElems/2; ++i)
9686 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009687
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009688 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009689
Craig Toppera080daf2013-01-20 21:50:27 +00009690 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009691 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009692
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009693 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9694 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009695
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009696 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009697}
9698
Evan Cheng370e5342008-12-03 08:38:43 +00009699// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9700// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9701// from the AND / OR.
9702static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9703 Opc = Op.getOpcode();
9704 if (Opc != ISD::OR && Opc != ISD::AND)
9705 return false;
9706 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9707 Op.getOperand(0).hasOneUse() &&
9708 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9709 Op.getOperand(1).hasOneUse());
9710}
9711
Evan Cheng961d6d42009-02-02 08:19:07 +00009712// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9713// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009714static bool isXor1OfSetCC(SDValue Op) {
9715 if (Op.getOpcode() != ISD::XOR)
9716 return false;
9717 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9718 if (N1C && N1C->getAPIntValue() == 1) {
9719 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9720 Op.getOperand(0).hasOneUse();
9721 }
9722 return false;
9723}
9724
Dan Gohmand858e902010-04-17 15:26:15 +00009725SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009726 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009727 SDValue Chain = Op.getOperand(0);
9728 SDValue Cond = Op.getOperand(1);
9729 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009730 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009731 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009732 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009733
Dan Gohman1a492952009-10-20 16:22:37 +00009734 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009735 // Check for setcc([su]{add,sub,mul}o == 0).
9736 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9737 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9738 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9739 Cond.getOperand(0).getResNo() == 1 &&
9740 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9741 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9742 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9743 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9744 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9745 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9746 Inverted = true;
9747 Cond = Cond.getOperand(0);
9748 } else {
9749 SDValue NewCond = LowerSETCC(Cond, DAG);
9750 if (NewCond.getNode())
9751 Cond = NewCond;
9752 }
Dan Gohman1a492952009-10-20 16:22:37 +00009753 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009754#if 0
9755 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009756 else if (Cond.getOpcode() == X86ISD::ADD ||
9757 Cond.getOpcode() == X86ISD::SUB ||
9758 Cond.getOpcode() == X86ISD::SMUL ||
9759 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009760 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009761#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009762
Evan Chengad9c0a32009-12-15 00:53:42 +00009763 // Look pass (and (setcc_carry (cmp ...)), 1).
9764 if (Cond.getOpcode() == ISD::AND &&
9765 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9766 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009767 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009768 Cond = Cond.getOperand(0);
9769 }
9770
Evan Cheng3f41d662007-10-08 22:16:29 +00009771 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9772 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009773 unsigned CondOpcode = Cond.getOpcode();
9774 if (CondOpcode == X86ISD::SETCC ||
9775 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009776 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009777
Dan Gohman475871a2008-07-27 21:46:04 +00009778 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009779 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009780 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009781 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009782 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009783 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009784 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009785 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009786 default: break;
9787 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009788 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009789 // These can only come from an arithmetic instruction with overflow,
9790 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009791 Cond = Cond.getNode()->getOperand(1);
9792 addTest = false;
9793 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009794 }
Evan Cheng0488db92007-09-25 01:57:46 +00009795 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009796 }
9797 CondOpcode = Cond.getOpcode();
9798 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9799 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9800 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9801 Cond.getOperand(0).getValueType() != MVT::i8)) {
9802 SDValue LHS = Cond.getOperand(0);
9803 SDValue RHS = Cond.getOperand(1);
9804 unsigned X86Opcode;
9805 unsigned X86Cond;
9806 SDVTList VTs;
9807 switch (CondOpcode) {
9808 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9809 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9810 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9811 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9812 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9813 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9814 default: llvm_unreachable("unexpected overflowing operator");
9815 }
9816 if (Inverted)
9817 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9818 if (CondOpcode == ISD::UMULO)
9819 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9820 MVT::i32);
9821 else
9822 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9823
9824 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9825
9826 if (CondOpcode == ISD::UMULO)
9827 Cond = X86Op.getValue(2);
9828 else
9829 Cond = X86Op.getValue(1);
9830
9831 CC = DAG.getConstant(X86Cond, MVT::i8);
9832 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009833 } else {
9834 unsigned CondOpc;
9835 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9836 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009837 if (CondOpc == ISD::OR) {
9838 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9839 // two branches instead of an explicit OR instruction with a
9840 // separate test.
9841 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009842 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009843 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009844 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009845 Chain, Dest, CC, Cmp);
9846 CC = Cond.getOperand(1).getOperand(0);
9847 Cond = Cmp;
9848 addTest = false;
9849 }
9850 } else { // ISD::AND
9851 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9852 // two branches instead of an explicit AND instruction with a
9853 // separate test. However, we only do this if this block doesn't
9854 // have a fall-through edge, because this requires an explicit
9855 // jmp when the condition is false.
9856 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009857 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009858 Op.getNode()->hasOneUse()) {
9859 X86::CondCode CCode =
9860 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9861 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009862 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009863 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009864 // Look for an unconditional branch following this conditional branch.
9865 // We need this because we need to reverse the successors in order
9866 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009867 if (User->getOpcode() == ISD::BR) {
9868 SDValue FalseBB = User->getOperand(1);
9869 SDNode *NewBR =
9870 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009871 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009872 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009873 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009874
Dale Johannesene4d209d2009-02-03 20:21:25 +00009875 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009876 Chain, Dest, CC, Cmp);
9877 X86::CondCode CCode =
9878 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9879 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009880 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009881 Cond = Cmp;
9882 addTest = false;
9883 }
9884 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009885 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009886 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9887 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9888 // It should be transformed during dag combiner except when the condition
9889 // is set by a arithmetics with overflow node.
9890 X86::CondCode CCode =
9891 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9892 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009893 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009894 Cond = Cond.getOperand(0).getOperand(1);
9895 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009896 } else if (Cond.getOpcode() == ISD::SETCC &&
9897 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9898 // For FCMP_OEQ, we can emit
9899 // two branches instead of an explicit AND instruction with a
9900 // separate test. However, we only do this if this block doesn't
9901 // have a fall-through edge, because this requires an explicit
9902 // jmp when the condition is false.
9903 if (Op.getNode()->hasOneUse()) {
9904 SDNode *User = *Op.getNode()->use_begin();
9905 // Look for an unconditional branch following this conditional branch.
9906 // We need this because we need to reverse the successors in order
9907 // to implement FCMP_OEQ.
9908 if (User->getOpcode() == ISD::BR) {
9909 SDValue FalseBB = User->getOperand(1);
9910 SDNode *NewBR =
9911 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9912 assert(NewBR == User);
9913 (void)NewBR;
9914 Dest = FalseBB;
9915
9916 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9917 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009918 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009919 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9920 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9921 Chain, Dest, CC, Cmp);
9922 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9923 Cond = Cmp;
9924 addTest = false;
9925 }
9926 }
9927 } else if (Cond.getOpcode() == ISD::SETCC &&
9928 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9929 // For FCMP_UNE, we can emit
9930 // two branches instead of an explicit AND instruction with a
9931 // separate test. However, we only do this if this block doesn't
9932 // have a fall-through edge, because this requires an explicit
9933 // jmp when the condition is false.
9934 if (Op.getNode()->hasOneUse()) {
9935 SDNode *User = *Op.getNode()->use_begin();
9936 // Look for an unconditional branch following this conditional branch.
9937 // We need this because we need to reverse the successors in order
9938 // to implement FCMP_UNE.
9939 if (User->getOpcode() == ISD::BR) {
9940 SDValue FalseBB = User->getOperand(1);
9941 SDNode *NewBR =
9942 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9943 assert(NewBR == User);
9944 (void)NewBR;
9945
9946 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9947 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009948 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009949 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9950 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9951 Chain, Dest, CC, Cmp);
9952 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9953 Cond = Cmp;
9954 addTest = false;
9955 Dest = FalseBB;
9956 }
9957 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009958 }
Evan Cheng0488db92007-09-25 01:57:46 +00009959 }
9960
9961 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009962 // Look pass the truncate if the high bits are known zero.
9963 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9964 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009965
9966 // We know the result of AND is compared against zero. Try to match
9967 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009968 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009969 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9970 if (NewSetCC.getNode()) {
9971 CC = NewSetCC.getOperand(0);
9972 Cond = NewSetCC.getOperand(1);
9973 addTest = false;
9974 }
9975 }
9976 }
9977
9978 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009979 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009980 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009981 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009982 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009983 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009984 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009985}
9986
Anton Korobeynikove060b532007-04-17 19:34:00 +00009987// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9988// Calls to _alloca is needed to probe the stack when allocating more than 4k
9989// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9990// that the guard pages used by the OS virtual memory manager are allocated in
9991// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009992SDValue
9993X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009994 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009995 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009996 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009997 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009998 "are being used");
9999 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010000 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010001
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010002 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010003 SDValue Chain = Op.getOperand(0);
10004 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010005 // FIXME: Ensure alignment here
10006
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010007 bool Is64Bit = Subtarget->is64Bit();
10008 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010009
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010010 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010011 MachineFunction &MF = DAG.getMachineFunction();
10012 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010013
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010014 if (Is64Bit) {
10015 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010016 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010017 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010018
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010019 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010020 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010021 if (I->hasNestAttr())
10022 report_fatal_error("Cannot use segmented stacks with functions that "
10023 "have nested arguments.");
10024 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010025
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010026 const TargetRegisterClass *AddrRegClass =
10027 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10028 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10029 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10030 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10031 DAG.getRegister(Vreg, SPTy));
10032 SDValue Ops1[2] = { Value, Chain };
10033 return DAG.getMergeValues(Ops1, 2, dl);
10034 } else {
10035 SDValue Flag;
10036 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010037
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010038 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10039 Flag = Chain.getValue(1);
10040 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010041
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010042 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10043 Flag = Chain.getValue(1);
10044
Michael Liaoc5c970e2012-10-31 04:14:09 +000010045 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10046 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010047
10048 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10049 return DAG.getMergeValues(Ops1, 2, dl);
10050 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010051}
10052
Dan Gohmand858e902010-04-17 15:26:15 +000010053SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010054 MachineFunction &MF = DAG.getMachineFunction();
10055 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10056
Dan Gohman69de1932008-02-06 22:27:42 +000010057 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010058 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010059
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010060 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010061 // vastart just stores the address of the VarArgsFrameIndex slot into the
10062 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010063 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10064 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010065 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10066 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010067 }
10068
10069 // __va_list_tag:
10070 // gp_offset (0 - 6 * 8)
10071 // fp_offset (48 - 48 + 8 * 16)
10072 // overflow_arg_area (point to parameters coming in memory).
10073 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010074 SmallVector<SDValue, 8> MemOps;
10075 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010076 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010077 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010078 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10079 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010080 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010081 MemOps.push_back(Store);
10082
10083 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010084 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010085 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010086 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010087 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10088 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010089 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010090 MemOps.push_back(Store);
10091
10092 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010093 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010094 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010095 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10096 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010097 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10098 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010099 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010100 MemOps.push_back(Store);
10101
10102 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010103 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010104 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010105 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10106 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010107 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10108 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010109 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010110 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010111 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010112}
10113
Dan Gohmand858e902010-04-17 15:26:15 +000010114SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010115 assert(Subtarget->is64Bit() &&
10116 "LowerVAARG only handles 64-bit va_arg!");
10117 assert((Subtarget->isTargetLinux() ||
10118 Subtarget->isTargetDarwin()) &&
10119 "Unhandled target in LowerVAARG");
10120 assert(Op.getNode()->getNumOperands() == 4);
10121 SDValue Chain = Op.getOperand(0);
10122 SDValue SrcPtr = Op.getOperand(1);
10123 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10124 unsigned Align = Op.getConstantOperandVal(3);
10125 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010126
Dan Gohman320afb82010-10-12 18:00:49 +000010127 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010128 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010129 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010130 uint8_t ArgMode;
10131
10132 // Decide which area this value should be read from.
10133 // TODO: Implement the AMD64 ABI in its entirety. This simple
10134 // selection mechanism works only for the basic types.
10135 if (ArgVT == MVT::f80) {
10136 llvm_unreachable("va_arg for f80 not yet implemented");
10137 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10138 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10139 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10140 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10141 } else {
10142 llvm_unreachable("Unhandled argument type in LowerVAARG");
10143 }
10144
10145 if (ArgMode == 2) {
10146 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010147 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010148 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010149 .getFunction()->getAttributes()
10150 .hasAttribute(AttributeSet::FunctionIndex,
10151 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010152 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010153 }
10154
10155 // Insert VAARG_64 node into the DAG
10156 // VAARG_64 returns two values: Variable Argument Address, Chain
10157 SmallVector<SDValue, 11> InstOps;
10158 InstOps.push_back(Chain);
10159 InstOps.push_back(SrcPtr);
10160 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10161 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10162 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10163 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10164 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10165 VTs, &InstOps[0], InstOps.size(),
10166 MVT::i64,
10167 MachinePointerInfo(SV),
10168 /*Align=*/0,
10169 /*Volatile=*/false,
10170 /*ReadMem=*/true,
10171 /*WriteMem=*/true);
10172 Chain = VAARG.getValue(1);
10173
10174 // Load the next argument and return it
10175 return DAG.getLoad(ArgVT, dl,
10176 Chain,
10177 VAARG,
10178 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010179 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010180}
10181
Craig Topper55b24052012-09-11 06:15:32 +000010182static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10183 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010184 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010185 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010186 SDValue Chain = Op.getOperand(0);
10187 SDValue DstPtr = Op.getOperand(1);
10188 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010189 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10190 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010191 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010192
Chris Lattnere72f2022010-09-21 05:40:29 +000010193 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010194 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010195 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010196 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010197}
10198
Craig Topper80e46362012-01-23 06:16:53 +000010199// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10200// may or may not be a constant. Takes immediate version of shift as input.
10201static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10202 SDValue SrcOp, SDValue ShAmt,
10203 SelectionDAG &DAG) {
10204 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10205
10206 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010207 // Constant may be a TargetConstant. Use a regular constant.
10208 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010209 switch (Opc) {
10210 default: llvm_unreachable("Unknown target vector shift node");
10211 case X86ISD::VSHLI:
10212 case X86ISD::VSRLI:
10213 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010214 return DAG.getNode(Opc, dl, VT, SrcOp,
10215 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010216 }
10217 }
10218
10219 // Change opcode to non-immediate version
10220 switch (Opc) {
10221 default: llvm_unreachable("Unknown target vector shift node");
10222 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10223 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10224 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10225 }
10226
10227 // Need to build a vector containing shift amount
10228 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10229 SDValue ShOps[4];
10230 ShOps[0] = ShAmt;
10231 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010232 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010233 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010234
10235 // The return type has to be a 128-bit type with the same element
10236 // type as the input type.
10237 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10238 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10239
10240 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010241 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10242}
10243
Craig Topper55b24052012-09-11 06:15:32 +000010244static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010245 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010246 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010247 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010248 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010249 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010250 case Intrinsic::x86_sse_comieq_ss:
10251 case Intrinsic::x86_sse_comilt_ss:
10252 case Intrinsic::x86_sse_comile_ss:
10253 case Intrinsic::x86_sse_comigt_ss:
10254 case Intrinsic::x86_sse_comige_ss:
10255 case Intrinsic::x86_sse_comineq_ss:
10256 case Intrinsic::x86_sse_ucomieq_ss:
10257 case Intrinsic::x86_sse_ucomilt_ss:
10258 case Intrinsic::x86_sse_ucomile_ss:
10259 case Intrinsic::x86_sse_ucomigt_ss:
10260 case Intrinsic::x86_sse_ucomige_ss:
10261 case Intrinsic::x86_sse_ucomineq_ss:
10262 case Intrinsic::x86_sse2_comieq_sd:
10263 case Intrinsic::x86_sse2_comilt_sd:
10264 case Intrinsic::x86_sse2_comile_sd:
10265 case Intrinsic::x86_sse2_comigt_sd:
10266 case Intrinsic::x86_sse2_comige_sd:
10267 case Intrinsic::x86_sse2_comineq_sd:
10268 case Intrinsic::x86_sse2_ucomieq_sd:
10269 case Intrinsic::x86_sse2_ucomilt_sd:
10270 case Intrinsic::x86_sse2_ucomile_sd:
10271 case Intrinsic::x86_sse2_ucomigt_sd:
10272 case Intrinsic::x86_sse2_ucomige_sd:
10273 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010274 unsigned Opc;
10275 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010276 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010277 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010278 case Intrinsic::x86_sse_comieq_ss:
10279 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010280 Opc = X86ISD::COMI;
10281 CC = ISD::SETEQ;
10282 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010283 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010284 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010285 Opc = X86ISD::COMI;
10286 CC = ISD::SETLT;
10287 break;
10288 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010289 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010290 Opc = X86ISD::COMI;
10291 CC = ISD::SETLE;
10292 break;
10293 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010294 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010295 Opc = X86ISD::COMI;
10296 CC = ISD::SETGT;
10297 break;
10298 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010299 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010300 Opc = X86ISD::COMI;
10301 CC = ISD::SETGE;
10302 break;
10303 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010304 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010305 Opc = X86ISD::COMI;
10306 CC = ISD::SETNE;
10307 break;
10308 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010309 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010310 Opc = X86ISD::UCOMI;
10311 CC = ISD::SETEQ;
10312 break;
10313 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010314 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010315 Opc = X86ISD::UCOMI;
10316 CC = ISD::SETLT;
10317 break;
10318 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010319 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010320 Opc = X86ISD::UCOMI;
10321 CC = ISD::SETLE;
10322 break;
10323 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010324 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010325 Opc = X86ISD::UCOMI;
10326 CC = ISD::SETGT;
10327 break;
10328 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010329 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010330 Opc = X86ISD::UCOMI;
10331 CC = ISD::SETGE;
10332 break;
10333 case Intrinsic::x86_sse_ucomineq_ss:
10334 case Intrinsic::x86_sse2_ucomineq_sd:
10335 Opc = X86ISD::UCOMI;
10336 CC = ISD::SETNE;
10337 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010338 }
Evan Cheng734503b2006-09-11 02:19:56 +000010339
Dan Gohman475871a2008-07-27 21:46:04 +000010340 SDValue LHS = Op.getOperand(1);
10341 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010342 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010343 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010344 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10345 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10346 DAG.getConstant(X86CC, MVT::i8), Cond);
10347 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010348 }
Craig Topper6d688152012-08-14 07:43:25 +000010349
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010350 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010351 case Intrinsic::x86_sse2_pmulu_dq:
10352 case Intrinsic::x86_avx2_pmulu_dq:
10353 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10354 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010355
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010356 // SSE2/AVX2 sub with unsigned saturation intrinsics
10357 case Intrinsic::x86_sse2_psubus_b:
10358 case Intrinsic::x86_sse2_psubus_w:
10359 case Intrinsic::x86_avx2_psubus_b:
10360 case Intrinsic::x86_avx2_psubus_w:
10361 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10362 Op.getOperand(1), Op.getOperand(2));
10363
Craig Topper6d688152012-08-14 07:43:25 +000010364 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010365 case Intrinsic::x86_sse3_hadd_ps:
10366 case Intrinsic::x86_sse3_hadd_pd:
10367 case Intrinsic::x86_avx_hadd_ps_256:
10368 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010369 case Intrinsic::x86_sse3_hsub_ps:
10370 case Intrinsic::x86_sse3_hsub_pd:
10371 case Intrinsic::x86_avx_hsub_ps_256:
10372 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010373 case Intrinsic::x86_ssse3_phadd_w_128:
10374 case Intrinsic::x86_ssse3_phadd_d_128:
10375 case Intrinsic::x86_avx2_phadd_w:
10376 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010377 case Intrinsic::x86_ssse3_phsub_w_128:
10378 case Intrinsic::x86_ssse3_phsub_d_128:
10379 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010380 case Intrinsic::x86_avx2_phsub_d: {
10381 unsigned Opcode;
10382 switch (IntNo) {
10383 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10384 case Intrinsic::x86_sse3_hadd_ps:
10385 case Intrinsic::x86_sse3_hadd_pd:
10386 case Intrinsic::x86_avx_hadd_ps_256:
10387 case Intrinsic::x86_avx_hadd_pd_256:
10388 Opcode = X86ISD::FHADD;
10389 break;
10390 case Intrinsic::x86_sse3_hsub_ps:
10391 case Intrinsic::x86_sse3_hsub_pd:
10392 case Intrinsic::x86_avx_hsub_ps_256:
10393 case Intrinsic::x86_avx_hsub_pd_256:
10394 Opcode = X86ISD::FHSUB;
10395 break;
10396 case Intrinsic::x86_ssse3_phadd_w_128:
10397 case Intrinsic::x86_ssse3_phadd_d_128:
10398 case Intrinsic::x86_avx2_phadd_w:
10399 case Intrinsic::x86_avx2_phadd_d:
10400 Opcode = X86ISD::HADD;
10401 break;
10402 case Intrinsic::x86_ssse3_phsub_w_128:
10403 case Intrinsic::x86_ssse3_phsub_d_128:
10404 case Intrinsic::x86_avx2_phsub_w:
10405 case Intrinsic::x86_avx2_phsub_d:
10406 Opcode = X86ISD::HSUB;
10407 break;
10408 }
10409 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010410 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010411 }
10412
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010413 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10414 case Intrinsic::x86_sse2_pmaxu_b:
10415 case Intrinsic::x86_sse41_pmaxuw:
10416 case Intrinsic::x86_sse41_pmaxud:
10417 case Intrinsic::x86_avx2_pmaxu_b:
10418 case Intrinsic::x86_avx2_pmaxu_w:
10419 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010420 case Intrinsic::x86_sse2_pminu_b:
10421 case Intrinsic::x86_sse41_pminuw:
10422 case Intrinsic::x86_sse41_pminud:
10423 case Intrinsic::x86_avx2_pminu_b:
10424 case Intrinsic::x86_avx2_pminu_w:
10425 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010426 case Intrinsic::x86_sse41_pmaxsb:
10427 case Intrinsic::x86_sse2_pmaxs_w:
10428 case Intrinsic::x86_sse41_pmaxsd:
10429 case Intrinsic::x86_avx2_pmaxs_b:
10430 case Intrinsic::x86_avx2_pmaxs_w:
10431 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010432 case Intrinsic::x86_sse41_pminsb:
10433 case Intrinsic::x86_sse2_pmins_w:
10434 case Intrinsic::x86_sse41_pminsd:
10435 case Intrinsic::x86_avx2_pmins_b:
10436 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010437 case Intrinsic::x86_avx2_pmins_d: {
10438 unsigned Opcode;
10439 switch (IntNo) {
10440 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10441 case Intrinsic::x86_sse2_pmaxu_b:
10442 case Intrinsic::x86_sse41_pmaxuw:
10443 case Intrinsic::x86_sse41_pmaxud:
10444 case Intrinsic::x86_avx2_pmaxu_b:
10445 case Intrinsic::x86_avx2_pmaxu_w:
10446 case Intrinsic::x86_avx2_pmaxu_d:
10447 Opcode = X86ISD::UMAX;
10448 break;
10449 case Intrinsic::x86_sse2_pminu_b:
10450 case Intrinsic::x86_sse41_pminuw:
10451 case Intrinsic::x86_sse41_pminud:
10452 case Intrinsic::x86_avx2_pminu_b:
10453 case Intrinsic::x86_avx2_pminu_w:
10454 case Intrinsic::x86_avx2_pminu_d:
10455 Opcode = X86ISD::UMIN;
10456 break;
10457 case Intrinsic::x86_sse41_pmaxsb:
10458 case Intrinsic::x86_sse2_pmaxs_w:
10459 case Intrinsic::x86_sse41_pmaxsd:
10460 case Intrinsic::x86_avx2_pmaxs_b:
10461 case Intrinsic::x86_avx2_pmaxs_w:
10462 case Intrinsic::x86_avx2_pmaxs_d:
10463 Opcode = X86ISD::SMAX;
10464 break;
10465 case Intrinsic::x86_sse41_pminsb:
10466 case Intrinsic::x86_sse2_pmins_w:
10467 case Intrinsic::x86_sse41_pminsd:
10468 case Intrinsic::x86_avx2_pmins_b:
10469 case Intrinsic::x86_avx2_pmins_w:
10470 case Intrinsic::x86_avx2_pmins_d:
10471 Opcode = X86ISD::SMIN;
10472 break;
10473 }
10474 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010475 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010476 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010477
Craig Topper6d183e42012-12-29 16:44:25 +000010478 // SSE/SSE2/AVX floating point max/min intrinsics.
10479 case Intrinsic::x86_sse_max_ps:
10480 case Intrinsic::x86_sse2_max_pd:
10481 case Intrinsic::x86_avx_max_ps_256:
10482 case Intrinsic::x86_avx_max_pd_256:
10483 case Intrinsic::x86_sse_min_ps:
10484 case Intrinsic::x86_sse2_min_pd:
10485 case Intrinsic::x86_avx_min_ps_256:
10486 case Intrinsic::x86_avx_min_pd_256: {
10487 unsigned Opcode;
10488 switch (IntNo) {
10489 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10490 case Intrinsic::x86_sse_max_ps:
10491 case Intrinsic::x86_sse2_max_pd:
10492 case Intrinsic::x86_avx_max_ps_256:
10493 case Intrinsic::x86_avx_max_pd_256:
10494 Opcode = X86ISD::FMAX;
10495 break;
10496 case Intrinsic::x86_sse_min_ps:
10497 case Intrinsic::x86_sse2_min_pd:
10498 case Intrinsic::x86_avx_min_ps_256:
10499 case Intrinsic::x86_avx_min_pd_256:
10500 Opcode = X86ISD::FMIN;
10501 break;
10502 }
10503 return DAG.getNode(Opcode, dl, Op.getValueType(),
10504 Op.getOperand(1), Op.getOperand(2));
10505 }
10506
Craig Topper6d688152012-08-14 07:43:25 +000010507 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010508 case Intrinsic::x86_avx2_psllv_d:
10509 case Intrinsic::x86_avx2_psllv_q:
10510 case Intrinsic::x86_avx2_psllv_d_256:
10511 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010512 case Intrinsic::x86_avx2_psrlv_d:
10513 case Intrinsic::x86_avx2_psrlv_q:
10514 case Intrinsic::x86_avx2_psrlv_d_256:
10515 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010516 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010517 case Intrinsic::x86_avx2_psrav_d_256: {
10518 unsigned Opcode;
10519 switch (IntNo) {
10520 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10521 case Intrinsic::x86_avx2_psllv_d:
10522 case Intrinsic::x86_avx2_psllv_q:
10523 case Intrinsic::x86_avx2_psllv_d_256:
10524 case Intrinsic::x86_avx2_psllv_q_256:
10525 Opcode = ISD::SHL;
10526 break;
10527 case Intrinsic::x86_avx2_psrlv_d:
10528 case Intrinsic::x86_avx2_psrlv_q:
10529 case Intrinsic::x86_avx2_psrlv_d_256:
10530 case Intrinsic::x86_avx2_psrlv_q_256:
10531 Opcode = ISD::SRL;
10532 break;
10533 case Intrinsic::x86_avx2_psrav_d:
10534 case Intrinsic::x86_avx2_psrav_d_256:
10535 Opcode = ISD::SRA;
10536 break;
10537 }
10538 return DAG.getNode(Opcode, dl, Op.getValueType(),
10539 Op.getOperand(1), Op.getOperand(2));
10540 }
10541
Craig Topper969ba282012-01-25 06:43:11 +000010542 case Intrinsic::x86_ssse3_pshuf_b_128:
10543 case Intrinsic::x86_avx2_pshuf_b:
10544 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10545 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010546
Craig Topper969ba282012-01-25 06:43:11 +000010547 case Intrinsic::x86_ssse3_psign_b_128:
10548 case Intrinsic::x86_ssse3_psign_w_128:
10549 case Intrinsic::x86_ssse3_psign_d_128:
10550 case Intrinsic::x86_avx2_psign_b:
10551 case Intrinsic::x86_avx2_psign_w:
10552 case Intrinsic::x86_avx2_psign_d:
10553 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10554 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010555
Craig Toppere566cd02012-01-26 07:18:03 +000010556 case Intrinsic::x86_sse41_insertps:
10557 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10558 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010559
Craig Toppere566cd02012-01-26 07:18:03 +000010560 case Intrinsic::x86_avx_vperm2f128_ps_256:
10561 case Intrinsic::x86_avx_vperm2f128_pd_256:
10562 case Intrinsic::x86_avx_vperm2f128_si_256:
10563 case Intrinsic::x86_avx2_vperm2i128:
10564 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10565 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010566
Craig Topperffa6c402012-04-16 07:13:00 +000010567 case Intrinsic::x86_avx2_permd:
10568 case Intrinsic::x86_avx2_permps:
10569 // Operands intentionally swapped. Mask is last operand to intrinsic,
10570 // but second operand for node/intruction.
10571 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10572 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010573
Craig Topper22d8f0d2012-12-29 18:18:20 +000010574 case Intrinsic::x86_sse_sqrt_ps:
10575 case Intrinsic::x86_sse2_sqrt_pd:
10576 case Intrinsic::x86_avx_sqrt_ps_256:
10577 case Intrinsic::x86_avx_sqrt_pd_256:
10578 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10579
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010580 // ptest and testp intrinsics. The intrinsic these come from are designed to
10581 // return an integer value, not just an instruction so lower it to the ptest
10582 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010583 case Intrinsic::x86_sse41_ptestz:
10584 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010585 case Intrinsic::x86_sse41_ptestnzc:
10586 case Intrinsic::x86_avx_ptestz_256:
10587 case Intrinsic::x86_avx_ptestc_256:
10588 case Intrinsic::x86_avx_ptestnzc_256:
10589 case Intrinsic::x86_avx_vtestz_ps:
10590 case Intrinsic::x86_avx_vtestc_ps:
10591 case Intrinsic::x86_avx_vtestnzc_ps:
10592 case Intrinsic::x86_avx_vtestz_pd:
10593 case Intrinsic::x86_avx_vtestc_pd:
10594 case Intrinsic::x86_avx_vtestnzc_pd:
10595 case Intrinsic::x86_avx_vtestz_ps_256:
10596 case Intrinsic::x86_avx_vtestc_ps_256:
10597 case Intrinsic::x86_avx_vtestnzc_ps_256:
10598 case Intrinsic::x86_avx_vtestz_pd_256:
10599 case Intrinsic::x86_avx_vtestc_pd_256:
10600 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10601 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010602 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010603 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010604 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010605 case Intrinsic::x86_avx_vtestz_ps:
10606 case Intrinsic::x86_avx_vtestz_pd:
10607 case Intrinsic::x86_avx_vtestz_ps_256:
10608 case Intrinsic::x86_avx_vtestz_pd_256:
10609 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010610 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010611 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010612 // ZF = 1
10613 X86CC = X86::COND_E;
10614 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010615 case Intrinsic::x86_avx_vtestc_ps:
10616 case Intrinsic::x86_avx_vtestc_pd:
10617 case Intrinsic::x86_avx_vtestc_ps_256:
10618 case Intrinsic::x86_avx_vtestc_pd_256:
10619 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010620 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010621 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010622 // CF = 1
10623 X86CC = X86::COND_B;
10624 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010625 case Intrinsic::x86_avx_vtestnzc_ps:
10626 case Intrinsic::x86_avx_vtestnzc_pd:
10627 case Intrinsic::x86_avx_vtestnzc_ps_256:
10628 case Intrinsic::x86_avx_vtestnzc_pd_256:
10629 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010630 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010631 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010632 // ZF and CF = 0
10633 X86CC = X86::COND_A;
10634 break;
10635 }
Eric Christopherfd179292009-08-27 18:07:15 +000010636
Eric Christopher71c67532009-07-29 00:28:05 +000010637 SDValue LHS = Op.getOperand(1);
10638 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010639 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10640 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010641 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10642 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10643 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010644 }
Evan Cheng5759f972008-05-04 09:15:50 +000010645
Craig Topper80e46362012-01-23 06:16:53 +000010646 // SSE/AVX shift intrinsics
10647 case Intrinsic::x86_sse2_psll_w:
10648 case Intrinsic::x86_sse2_psll_d:
10649 case Intrinsic::x86_sse2_psll_q:
10650 case Intrinsic::x86_avx2_psll_w:
10651 case Intrinsic::x86_avx2_psll_d:
10652 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010653 case Intrinsic::x86_sse2_psrl_w:
10654 case Intrinsic::x86_sse2_psrl_d:
10655 case Intrinsic::x86_sse2_psrl_q:
10656 case Intrinsic::x86_avx2_psrl_w:
10657 case Intrinsic::x86_avx2_psrl_d:
10658 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010659 case Intrinsic::x86_sse2_psra_w:
10660 case Intrinsic::x86_sse2_psra_d:
10661 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010662 case Intrinsic::x86_avx2_psra_d: {
10663 unsigned Opcode;
10664 switch (IntNo) {
10665 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10666 case Intrinsic::x86_sse2_psll_w:
10667 case Intrinsic::x86_sse2_psll_d:
10668 case Intrinsic::x86_sse2_psll_q:
10669 case Intrinsic::x86_avx2_psll_w:
10670 case Intrinsic::x86_avx2_psll_d:
10671 case Intrinsic::x86_avx2_psll_q:
10672 Opcode = X86ISD::VSHL;
10673 break;
10674 case Intrinsic::x86_sse2_psrl_w:
10675 case Intrinsic::x86_sse2_psrl_d:
10676 case Intrinsic::x86_sse2_psrl_q:
10677 case Intrinsic::x86_avx2_psrl_w:
10678 case Intrinsic::x86_avx2_psrl_d:
10679 case Intrinsic::x86_avx2_psrl_q:
10680 Opcode = X86ISD::VSRL;
10681 break;
10682 case Intrinsic::x86_sse2_psra_w:
10683 case Intrinsic::x86_sse2_psra_d:
10684 case Intrinsic::x86_avx2_psra_w:
10685 case Intrinsic::x86_avx2_psra_d:
10686 Opcode = X86ISD::VSRA;
10687 break;
10688 }
10689 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010690 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010691 }
10692
10693 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010694 case Intrinsic::x86_sse2_pslli_w:
10695 case Intrinsic::x86_sse2_pslli_d:
10696 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010697 case Intrinsic::x86_avx2_pslli_w:
10698 case Intrinsic::x86_avx2_pslli_d:
10699 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010700 case Intrinsic::x86_sse2_psrli_w:
10701 case Intrinsic::x86_sse2_psrli_d:
10702 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010703 case Intrinsic::x86_avx2_psrli_w:
10704 case Intrinsic::x86_avx2_psrli_d:
10705 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010706 case Intrinsic::x86_sse2_psrai_w:
10707 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010708 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010709 case Intrinsic::x86_avx2_psrai_d: {
10710 unsigned Opcode;
10711 switch (IntNo) {
10712 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10713 case Intrinsic::x86_sse2_pslli_w:
10714 case Intrinsic::x86_sse2_pslli_d:
10715 case Intrinsic::x86_sse2_pslli_q:
10716 case Intrinsic::x86_avx2_pslli_w:
10717 case Intrinsic::x86_avx2_pslli_d:
10718 case Intrinsic::x86_avx2_pslli_q:
10719 Opcode = X86ISD::VSHLI;
10720 break;
10721 case Intrinsic::x86_sse2_psrli_w:
10722 case Intrinsic::x86_sse2_psrli_d:
10723 case Intrinsic::x86_sse2_psrli_q:
10724 case Intrinsic::x86_avx2_psrli_w:
10725 case Intrinsic::x86_avx2_psrli_d:
10726 case Intrinsic::x86_avx2_psrli_q:
10727 Opcode = X86ISD::VSRLI;
10728 break;
10729 case Intrinsic::x86_sse2_psrai_w:
10730 case Intrinsic::x86_sse2_psrai_d:
10731 case Intrinsic::x86_avx2_psrai_w:
10732 case Intrinsic::x86_avx2_psrai_d:
10733 Opcode = X86ISD::VSRAI;
10734 break;
10735 }
10736 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010737 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010738 }
10739
Craig Topper4feb6472012-08-06 06:22:36 +000010740 case Intrinsic::x86_sse42_pcmpistria128:
10741 case Intrinsic::x86_sse42_pcmpestria128:
10742 case Intrinsic::x86_sse42_pcmpistric128:
10743 case Intrinsic::x86_sse42_pcmpestric128:
10744 case Intrinsic::x86_sse42_pcmpistrio128:
10745 case Intrinsic::x86_sse42_pcmpestrio128:
10746 case Intrinsic::x86_sse42_pcmpistris128:
10747 case Intrinsic::x86_sse42_pcmpestris128:
10748 case Intrinsic::x86_sse42_pcmpistriz128:
10749 case Intrinsic::x86_sse42_pcmpestriz128: {
10750 unsigned Opcode;
10751 unsigned X86CC;
10752 switch (IntNo) {
10753 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10754 case Intrinsic::x86_sse42_pcmpistria128:
10755 Opcode = X86ISD::PCMPISTRI;
10756 X86CC = X86::COND_A;
10757 break;
10758 case Intrinsic::x86_sse42_pcmpestria128:
10759 Opcode = X86ISD::PCMPESTRI;
10760 X86CC = X86::COND_A;
10761 break;
10762 case Intrinsic::x86_sse42_pcmpistric128:
10763 Opcode = X86ISD::PCMPISTRI;
10764 X86CC = X86::COND_B;
10765 break;
10766 case Intrinsic::x86_sse42_pcmpestric128:
10767 Opcode = X86ISD::PCMPESTRI;
10768 X86CC = X86::COND_B;
10769 break;
10770 case Intrinsic::x86_sse42_pcmpistrio128:
10771 Opcode = X86ISD::PCMPISTRI;
10772 X86CC = X86::COND_O;
10773 break;
10774 case Intrinsic::x86_sse42_pcmpestrio128:
10775 Opcode = X86ISD::PCMPESTRI;
10776 X86CC = X86::COND_O;
10777 break;
10778 case Intrinsic::x86_sse42_pcmpistris128:
10779 Opcode = X86ISD::PCMPISTRI;
10780 X86CC = X86::COND_S;
10781 break;
10782 case Intrinsic::x86_sse42_pcmpestris128:
10783 Opcode = X86ISD::PCMPESTRI;
10784 X86CC = X86::COND_S;
10785 break;
10786 case Intrinsic::x86_sse42_pcmpistriz128:
10787 Opcode = X86ISD::PCMPISTRI;
10788 X86CC = X86::COND_E;
10789 break;
10790 case Intrinsic::x86_sse42_pcmpestriz128:
10791 Opcode = X86ISD::PCMPESTRI;
10792 X86CC = X86::COND_E;
10793 break;
10794 }
10795 SmallVector<SDValue, 5> NewOps;
10796 NewOps.append(Op->op_begin()+1, Op->op_end());
10797 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10798 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10799 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10800 DAG.getConstant(X86CC, MVT::i8),
10801 SDValue(PCMP.getNode(), 1));
10802 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10803 }
Craig Topper6d688152012-08-14 07:43:25 +000010804
Craig Topper4feb6472012-08-06 06:22:36 +000010805 case Intrinsic::x86_sse42_pcmpistri128:
10806 case Intrinsic::x86_sse42_pcmpestri128: {
10807 unsigned Opcode;
10808 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10809 Opcode = X86ISD::PCMPISTRI;
10810 else
10811 Opcode = X86ISD::PCMPESTRI;
10812
10813 SmallVector<SDValue, 5> NewOps;
10814 NewOps.append(Op->op_begin()+1, Op->op_end());
10815 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10816 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10817 }
Craig Topper0e292372012-08-24 04:03:22 +000010818 case Intrinsic::x86_fma_vfmadd_ps:
10819 case Intrinsic::x86_fma_vfmadd_pd:
10820 case Intrinsic::x86_fma_vfmsub_ps:
10821 case Intrinsic::x86_fma_vfmsub_pd:
10822 case Intrinsic::x86_fma_vfnmadd_ps:
10823 case Intrinsic::x86_fma_vfnmadd_pd:
10824 case Intrinsic::x86_fma_vfnmsub_ps:
10825 case Intrinsic::x86_fma_vfnmsub_pd:
10826 case Intrinsic::x86_fma_vfmaddsub_ps:
10827 case Intrinsic::x86_fma_vfmaddsub_pd:
10828 case Intrinsic::x86_fma_vfmsubadd_ps:
10829 case Intrinsic::x86_fma_vfmsubadd_pd:
10830 case Intrinsic::x86_fma_vfmadd_ps_256:
10831 case Intrinsic::x86_fma_vfmadd_pd_256:
10832 case Intrinsic::x86_fma_vfmsub_ps_256:
10833 case Intrinsic::x86_fma_vfmsub_pd_256:
10834 case Intrinsic::x86_fma_vfnmadd_ps_256:
10835 case Intrinsic::x86_fma_vfnmadd_pd_256:
10836 case Intrinsic::x86_fma_vfnmsub_ps_256:
10837 case Intrinsic::x86_fma_vfnmsub_pd_256:
10838 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10839 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10840 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10841 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010842 unsigned Opc;
10843 switch (IntNo) {
10844 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10845 case Intrinsic::x86_fma_vfmadd_ps:
10846 case Intrinsic::x86_fma_vfmadd_pd:
10847 case Intrinsic::x86_fma_vfmadd_ps_256:
10848 case Intrinsic::x86_fma_vfmadd_pd_256:
10849 Opc = X86ISD::FMADD;
10850 break;
10851 case Intrinsic::x86_fma_vfmsub_ps:
10852 case Intrinsic::x86_fma_vfmsub_pd:
10853 case Intrinsic::x86_fma_vfmsub_ps_256:
10854 case Intrinsic::x86_fma_vfmsub_pd_256:
10855 Opc = X86ISD::FMSUB;
10856 break;
10857 case Intrinsic::x86_fma_vfnmadd_ps:
10858 case Intrinsic::x86_fma_vfnmadd_pd:
10859 case Intrinsic::x86_fma_vfnmadd_ps_256:
10860 case Intrinsic::x86_fma_vfnmadd_pd_256:
10861 Opc = X86ISD::FNMADD;
10862 break;
10863 case Intrinsic::x86_fma_vfnmsub_ps:
10864 case Intrinsic::x86_fma_vfnmsub_pd:
10865 case Intrinsic::x86_fma_vfnmsub_ps_256:
10866 case Intrinsic::x86_fma_vfnmsub_pd_256:
10867 Opc = X86ISD::FNMSUB;
10868 break;
10869 case Intrinsic::x86_fma_vfmaddsub_ps:
10870 case Intrinsic::x86_fma_vfmaddsub_pd:
10871 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10872 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10873 Opc = X86ISD::FMADDSUB;
10874 break;
10875 case Intrinsic::x86_fma_vfmsubadd_ps:
10876 case Intrinsic::x86_fma_vfmsubadd_pd:
10877 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10878 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10879 Opc = X86ISD::FMSUBADD;
10880 break;
10881 }
10882
10883 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10884 Op.getOperand(2), Op.getOperand(3));
10885 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010886 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010887}
Evan Cheng72261582005-12-20 06:22:03 +000010888
Craig Topper55b24052012-09-11 06:15:32 +000010889static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010890 DebugLoc dl = Op.getDebugLoc();
10891 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10892 switch (IntNo) {
10893 default: return SDValue(); // Don't custom lower most intrinsics.
10894
10895 // RDRAND intrinsics.
10896 case Intrinsic::x86_rdrand_16:
10897 case Intrinsic::x86_rdrand_32:
10898 case Intrinsic::x86_rdrand_64: {
10899 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010900 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10901 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010902
10903 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10904 // return the value from Rand, which is always 0, casted to i32.
10905 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10906 DAG.getConstant(1, Op->getValueType(1)),
10907 DAG.getConstant(X86::COND_B, MVT::i32),
10908 SDValue(Result.getNode(), 1) };
10909 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10910 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10911 Ops, 4);
10912
10913 // Return { result, isValid, chain }.
10914 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010915 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010916 }
10917 }
10918}
10919
Dan Gohmand858e902010-04-17 15:26:15 +000010920SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10921 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010922 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10923 MFI->setReturnAddressIsTaken(true);
10924
Bill Wendling64e87322009-01-16 19:25:27 +000010925 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010926 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010927 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010928
10929 if (Depth > 0) {
10930 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10931 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010932 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10933 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10934 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010935 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010936 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010937 }
10938
10939 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010940 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010941 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010942 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010943}
10944
Dan Gohmand858e902010-04-17 15:26:15 +000010945SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10947 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010948
Owen Andersone50ed302009-08-10 22:56:29 +000010949 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010950 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010951 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10952 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010953 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010954 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010955 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10956 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010957 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010958 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010959}
10960
Dan Gohman475871a2008-07-27 21:46:04 +000010961SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010962 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010963 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010964}
10965
Dan Gohmand858e902010-04-17 15:26:15 +000010966SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010967 SDValue Chain = Op.getOperand(0);
10968 SDValue Offset = Op.getOperand(1);
10969 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010970 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010971
Dan Gohmand8816272010-08-11 18:14:00 +000010972 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10973 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10974 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010975 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010976
Dan Gohmand8816272010-08-11 18:14:00 +000010977 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010978 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010979 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010980 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10981 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010982 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010983
Dale Johannesene4d209d2009-02-03 20:21:25 +000010984 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010985 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010986 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010987}
10988
Michael Liao6c0e04c2012-10-15 22:39:43 +000010989SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10990 SelectionDAG &DAG) const {
10991 DebugLoc DL = Op.getDebugLoc();
10992 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10993 DAG.getVTList(MVT::i32, MVT::Other),
10994 Op.getOperand(0), Op.getOperand(1));
10995}
10996
10997SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10998 SelectionDAG &DAG) const {
10999 DebugLoc DL = Op.getDebugLoc();
11000 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11001 Op.getOperand(0), Op.getOperand(1));
11002}
11003
Craig Topper55b24052012-09-11 06:15:32 +000011004static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011005 return Op.getOperand(0);
11006}
11007
11008SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11009 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011010 SDValue Root = Op.getOperand(0);
11011 SDValue Trmp = Op.getOperand(1); // trampoline
11012 SDValue FPtr = Op.getOperand(2); // nested function
11013 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011014 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011015
Dan Gohman69de1932008-02-06 22:27:42 +000011016 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011017 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011018
11019 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011020 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011021
11022 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011023 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11024 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011025
Michael Liao7abf67a2012-10-04 19:50:43 +000011026 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11027 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011028
11029 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11030
11031 // Load the pointer to the nested function into R11.
11032 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011033 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011034 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011035 Addr, MachinePointerInfo(TrmpAddr),
11036 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011037
Owen Anderson825b72b2009-08-11 20:47:22 +000011038 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11039 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011040 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11041 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011042 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011043
11044 // Load the 'nest' parameter value into R10.
11045 // R10 is specified in X86CallingConv.td
11046 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011047 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11048 DAG.getConstant(10, MVT::i64));
11049 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011050 Addr, MachinePointerInfo(TrmpAddr, 10),
11051 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011052
Owen Anderson825b72b2009-08-11 20:47:22 +000011053 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11054 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011055 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11056 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011057 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011058
11059 // Jump to the nested function.
11060 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011061 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11062 DAG.getConstant(20, MVT::i64));
11063 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011064 Addr, MachinePointerInfo(TrmpAddr, 20),
11065 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011066
11067 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011068 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11069 DAG.getConstant(22, MVT::i64));
11070 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011071 MachinePointerInfo(TrmpAddr, 22),
11072 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011073
Duncan Sands4a544a72011-09-06 13:37:06 +000011074 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011075 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011076 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011077 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011078 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011079 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011080
11081 switch (CC) {
11082 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011083 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011084 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011085 case CallingConv::X86_StdCall: {
11086 // Pass 'nest' parameter in ECX.
11087 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011088 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011089
11090 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011091 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011092 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011093
Chris Lattner58d74912008-03-12 17:45:29 +000011094 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011095 unsigned InRegCount = 0;
11096 unsigned Idx = 1;
11097
11098 for (FunctionType::param_iterator I = FTy->param_begin(),
11099 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011100 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011101 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011102 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011103
11104 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011105 report_fatal_error("Nest register in use - reduce number of inreg"
11106 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011107 }
11108 }
11109 break;
11110 }
11111 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011112 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011113 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011114 // Pass 'nest' parameter in EAX.
11115 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011116 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011117 break;
11118 }
11119
Dan Gohman475871a2008-07-27 21:46:04 +000011120 SDValue OutChains[4];
11121 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011122
Owen Anderson825b72b2009-08-11 20:47:22 +000011123 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11124 DAG.getConstant(10, MVT::i32));
11125 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011126
Chris Lattnera62fe662010-02-05 19:20:30 +000011127 // This is storing the opcode for MOV32ri.
11128 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011129 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011130 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011131 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011132 Trmp, MachinePointerInfo(TrmpAddr),
11133 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011134
Owen Anderson825b72b2009-08-11 20:47:22 +000011135 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11136 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011137 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11138 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011139 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011140
Chris Lattnera62fe662010-02-05 19:20:30 +000011141 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011142 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11143 DAG.getConstant(5, MVT::i32));
11144 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011145 MachinePointerInfo(TrmpAddr, 5),
11146 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011147
Owen Anderson825b72b2009-08-11 20:47:22 +000011148 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11149 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011150 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11151 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011152 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011153
Duncan Sands4a544a72011-09-06 13:37:06 +000011154 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011155 }
11156}
11157
Dan Gohmand858e902010-04-17 15:26:15 +000011158SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11159 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011160 /*
11161 The rounding mode is in bits 11:10 of FPSR, and has the following
11162 settings:
11163 00 Round to nearest
11164 01 Round to -inf
11165 10 Round to +inf
11166 11 Round to 0
11167
11168 FLT_ROUNDS, on the other hand, expects the following:
11169 -1 Undefined
11170 0 Round to 0
11171 1 Round to nearest
11172 2 Round to +inf
11173 3 Round to -inf
11174
11175 To perform the conversion, we do:
11176 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11177 */
11178
11179 MachineFunction &MF = DAG.getMachineFunction();
11180 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011181 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011182 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011183 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011184 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011185
11186 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011187 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011188 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011189
Chris Lattner2156b792010-09-22 01:11:26 +000011190 MachineMemOperand *MMO =
11191 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11192 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011193
Chris Lattner2156b792010-09-22 01:11:26 +000011194 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11195 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11196 DAG.getVTList(MVT::Other),
11197 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011198
11199 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011200 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011201 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011202
11203 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011204 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011205 DAG.getNode(ISD::SRL, DL, MVT::i16,
11206 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011207 CWD, DAG.getConstant(0x800, MVT::i16)),
11208 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011209 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011210 DAG.getNode(ISD::SRL, DL, MVT::i16,
11211 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011212 CWD, DAG.getConstant(0x400, MVT::i16)),
11213 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011214
Dan Gohman475871a2008-07-27 21:46:04 +000011215 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011216 DAG.getNode(ISD::AND, DL, MVT::i16,
11217 DAG.getNode(ISD::ADD, DL, MVT::i16,
11218 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011219 DAG.getConstant(1, MVT::i16)),
11220 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011221
Duncan Sands83ec4b62008-06-06 12:08:01 +000011222 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011223 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011224}
11225
Craig Topper55b24052012-09-11 06:15:32 +000011226static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011227 EVT VT = Op.getValueType();
11228 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011229 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011230 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011231
11232 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011233 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011234 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011235 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011236 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011237 }
Evan Cheng18efe262007-12-14 02:13:44 +000011238
Evan Cheng152804e2007-12-14 08:30:15 +000011239 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011240 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011241 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011242
11243 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011244 SDValue Ops[] = {
11245 Op,
11246 DAG.getConstant(NumBits+NumBits-1, OpVT),
11247 DAG.getConstant(X86::COND_E, MVT::i8),
11248 Op.getValue(1)
11249 };
11250 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011251
11252 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011253 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011254
Owen Anderson825b72b2009-08-11 20:47:22 +000011255 if (VT == MVT::i8)
11256 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011257 return Op;
11258}
11259
Craig Topper55b24052012-09-11 06:15:32 +000011260static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011261 EVT VT = Op.getValueType();
11262 EVT OpVT = VT;
11263 unsigned NumBits = VT.getSizeInBits();
11264 DebugLoc dl = Op.getDebugLoc();
11265
11266 Op = Op.getOperand(0);
11267 if (VT == MVT::i8) {
11268 // Zero extend to i32 since there is not an i8 bsr.
11269 OpVT = MVT::i32;
11270 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11271 }
11272
11273 // Issue a bsr (scan bits in reverse).
11274 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11275 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11276
11277 // And xor with NumBits-1.
11278 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11279
11280 if (VT == MVT::i8)
11281 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11282 return Op;
11283}
11284
Craig Topper55b24052012-09-11 06:15:32 +000011285static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011286 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011287 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011288 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011289 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011290
11291 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011292 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011293 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011294
11295 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011296 SDValue Ops[] = {
11297 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011298 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011299 DAG.getConstant(X86::COND_E, MVT::i8),
11300 Op.getValue(1)
11301 };
Chandler Carruth77821022011-12-24 12:12:34 +000011302 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011303}
11304
Craig Topper13894fa2011-08-24 06:14:18 +000011305// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11306// ones, and then concatenate the result back.
11307static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011308 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011309
Craig Topper7a9a28b2012-08-12 02:23:29 +000011310 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011311 "Unsupported value type for operation");
11312
Craig Topper66ddd152012-04-27 22:54:43 +000011313 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011314 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011315
11316 // Extract the LHS vectors
11317 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011318 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11319 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011320
11321 // Extract the RHS vectors
11322 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011323 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11324 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011325
11326 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11327 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11328
11329 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11330 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11331 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11332}
11333
Craig Topper55b24052012-09-11 06:15:32 +000011334static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011335 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011336 Op.getValueType().isInteger() &&
11337 "Only handle AVX 256-bit vector integer operation");
11338 return Lower256IntArith(Op, DAG);
11339}
11340
Craig Topper55b24052012-09-11 06:15:32 +000011341static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011342 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011343 Op.getValueType().isInteger() &&
11344 "Only handle AVX 256-bit vector integer operation");
11345 return Lower256IntArith(Op, DAG);
11346}
11347
Craig Topper55b24052012-09-11 06:15:32 +000011348static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11349 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011350 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011351 EVT VT = Op.getValueType();
11352
11353 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011354 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011355 return Lower256IntArith(Op, DAG);
11356
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011357 SDValue A = Op.getOperand(0);
11358 SDValue B = Op.getOperand(1);
11359
11360 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11361 if (VT == MVT::v4i32) {
11362 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11363 "Should not custom lower when pmuldq is available!");
11364
11365 // Extract the odd parts.
11366 const int UnpackMask[] = { 1, -1, 3, -1 };
11367 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11368 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11369
11370 // Multiply the even parts.
11371 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11372 // Now multiply odd parts.
11373 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11374
11375 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11376 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11377
11378 // Merge the two vectors back together with a shuffle. This expands into 2
11379 // shuffles.
11380 const int ShufMask[] = { 0, 4, 2, 6 };
11381 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11382 }
11383
Craig Topper5b209e82012-02-05 03:14:49 +000011384 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11385 "Only know how to lower V2I64/V4I64 multiply");
11386
Craig Topper5b209e82012-02-05 03:14:49 +000011387 // Ahi = psrlqi(a, 32);
11388 // Bhi = psrlqi(b, 32);
11389 //
11390 // AloBlo = pmuludq(a, b);
11391 // AloBhi = pmuludq(a, Bhi);
11392 // AhiBlo = pmuludq(Ahi, b);
11393
11394 // AloBhi = psllqi(AloBhi, 32);
11395 // AhiBlo = psllqi(AhiBlo, 32);
11396 // return AloBlo + AloBhi + AhiBlo;
11397
Craig Topper5b209e82012-02-05 03:14:49 +000011398 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011399
Craig Topper5b209e82012-02-05 03:14:49 +000011400 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11401 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011402
Craig Topper5b209e82012-02-05 03:14:49 +000011403 // Bit cast to 32-bit vectors for MULUDQ
11404 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11405 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11406 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11407 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11408 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011409
Craig Topper5b209e82012-02-05 03:14:49 +000011410 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11411 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11412 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011413
Craig Topper5b209e82012-02-05 03:14:49 +000011414 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11415 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011416
Dale Johannesene4d209d2009-02-03 20:21:25 +000011417 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011418 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011419}
11420
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011421SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11422 EVT VT = Op.getValueType();
11423 EVT EltTy = VT.getVectorElementType();
11424 unsigned NumElts = VT.getVectorNumElements();
11425 SDValue N0 = Op.getOperand(0);
11426 DebugLoc dl = Op.getDebugLoc();
11427
11428 // Lower sdiv X, pow2-const.
11429 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11430 if (!C)
11431 return SDValue();
11432
11433 APInt SplatValue, SplatUndef;
11434 unsigned MinSplatBits;
11435 bool HasAnyUndefs;
11436 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11437 return SDValue();
11438
11439 if ((SplatValue != 0) &&
11440 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11441 unsigned lg2 = SplatValue.countTrailingZeros();
11442 // Splat the sign bit.
11443 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11444 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11445 // Add (N0 < 0) ? abs2 - 1 : 0;
11446 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11447 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11448 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11449 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11450 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11451
11452 // If we're dividing by a positive value, we're done. Otherwise, we must
11453 // negate the result.
11454 if (SplatValue.isNonNegative())
11455 return SRA;
11456
11457 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11458 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11459 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11460 }
11461 return SDValue();
11462}
11463
Nadav Rotem43012222011-05-11 08:12:09 +000011464SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11465
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011466 EVT VT = Op.getValueType();
11467 DebugLoc dl = Op.getDebugLoc();
11468 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011469 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011470
Craig Topper1accb7e2012-01-10 06:54:16 +000011471 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011472 return SDValue();
11473
Nadav Rotem43012222011-05-11 08:12:09 +000011474 // Optimize shl/srl/sra with constant shift amount.
11475 if (isSplatVector(Amt.getNode())) {
11476 SDValue SclrAmt = Amt->getOperand(0);
11477 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11478 uint64_t ShiftAmt = C->getZExtValue();
11479
Craig Toppered2e13d2012-01-22 19:15:14 +000011480 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011481 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011482 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11483 if (Op.getOpcode() == ISD::SHL)
11484 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11485 DAG.getConstant(ShiftAmt, MVT::i32));
11486 if (Op.getOpcode() == ISD::SRL)
11487 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11488 DAG.getConstant(ShiftAmt, MVT::i32));
11489 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11490 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11491 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011492 }
11493
Craig Toppered2e13d2012-01-22 19:15:14 +000011494 if (VT == MVT::v16i8) {
11495 if (Op.getOpcode() == ISD::SHL) {
11496 // Make a large shift.
11497 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11498 DAG.getConstant(ShiftAmt, MVT::i32));
11499 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11500 // Zero out the rightmost bits.
11501 SmallVector<SDValue, 16> V(16,
11502 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11503 MVT::i8));
11504 return DAG.getNode(ISD::AND, dl, VT, SHL,
11505 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011506 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011507 if (Op.getOpcode() == ISD::SRL) {
11508 // Make a large shift.
11509 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11510 DAG.getConstant(ShiftAmt, MVT::i32));
11511 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11512 // Zero out the leftmost bits.
11513 SmallVector<SDValue, 16> V(16,
11514 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11515 MVT::i8));
11516 return DAG.getNode(ISD::AND, dl, VT, SRL,
11517 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11518 }
11519 if (Op.getOpcode() == ISD::SRA) {
11520 if (ShiftAmt == 7) {
11521 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011522 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011523 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011524 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011525
Craig Toppered2e13d2012-01-22 19:15:14 +000011526 // R s>> a === ((R u>> a) ^ m) - m
11527 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11528 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11529 MVT::i8));
11530 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11531 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11532 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11533 return Res;
11534 }
Craig Topper731dfd02012-04-23 03:42:40 +000011535 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011536 }
Craig Topper46154eb2011-11-11 07:39:23 +000011537
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011538 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011539 if (Op.getOpcode() == ISD::SHL) {
11540 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011541 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11542 DAG.getConstant(ShiftAmt, MVT::i32));
11543 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011544 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011545 SmallVector<SDValue, 32> V(32,
11546 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11547 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011548 return DAG.getNode(ISD::AND, dl, VT, SHL,
11549 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011550 }
Craig Topper0d86d462011-11-20 00:12:05 +000011551 if (Op.getOpcode() == ISD::SRL) {
11552 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011553 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11554 DAG.getConstant(ShiftAmt, MVT::i32));
11555 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011556 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011557 SmallVector<SDValue, 32> V(32,
11558 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11559 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011560 return DAG.getNode(ISD::AND, dl, VT, SRL,
11561 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11562 }
11563 if (Op.getOpcode() == ISD::SRA) {
11564 if (ShiftAmt == 7) {
11565 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011566 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011567 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011568 }
11569
11570 // R s>> a === ((R u>> a) ^ m) - m
11571 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11572 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11573 MVT::i8));
11574 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11575 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11576 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11577 return Res;
11578 }
Craig Topper731dfd02012-04-23 03:42:40 +000011579 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011580 }
Nadav Rotem43012222011-05-11 08:12:09 +000011581 }
11582 }
11583
11584 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011585 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011586 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011587
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011588 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011589 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011590 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11591 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11592 }
Nadav Rotem43012222011-05-11 08:12:09 +000011593 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011594 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011595
Nate Begeman51409212010-07-28 00:21:48 +000011596 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011597 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011598 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011599
Lang Hames8b99c1e2011-12-17 01:08:46 +000011600 // Turn 'a' into a mask suitable for VSELECT
11601 SDValue VSelM = DAG.getConstant(0x80, VT);
11602 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011603 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011604
Lang Hames8b99c1e2011-12-17 01:08:46 +000011605 SDValue CM1 = DAG.getConstant(0x0f, VT);
11606 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011607
Lang Hames8b99c1e2011-12-17 01:08:46 +000011608 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11609 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011610 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11611 DAG.getConstant(4, MVT::i32), DAG);
11612 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011613 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11614
Nate Begeman51409212010-07-28 00:21:48 +000011615 // a += a
11616 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011617 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011618 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011619
Lang Hames8b99c1e2011-12-17 01:08:46 +000011620 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11621 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011622 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11623 DAG.getConstant(2, MVT::i32), DAG);
11624 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011625 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11626
Nate Begeman51409212010-07-28 00:21:48 +000011627 // a += a
11628 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011629 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011630 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011631
Lang Hames8b99c1e2011-12-17 01:08:46 +000011632 // return VSELECT(r, r+r, a);
11633 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011634 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011635 return R;
11636 }
Craig Topper46154eb2011-11-11 07:39:23 +000011637
11638 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011639 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011640 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011641 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11642 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11643
11644 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011645 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11646 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011647
11648 // Recreate the shift amount vectors
11649 SDValue Amt1, Amt2;
11650 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11651 // Constant shift amount
11652 SmallVector<SDValue, 4> Amt1Csts;
11653 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011654 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011655 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011656 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011657 Amt2Csts.push_back(Amt->getOperand(i));
11658
11659 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11660 &Amt1Csts[0], NumElems/2);
11661 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11662 &Amt2Csts[0], NumElems/2);
11663 } else {
11664 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011665 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11666 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011667 }
11668
11669 // Issue new vector shifts for the smaller types
11670 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11671 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11672
11673 // Concatenate the result back
11674 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11675 }
11676
Nate Begeman51409212010-07-28 00:21:48 +000011677 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011678}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011679
Craig Topper55b24052012-09-11 06:15:32 +000011680static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011681 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11682 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011683 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11684 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011685 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011686 SDValue LHS = N->getOperand(0);
11687 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011688 unsigned BaseOp = 0;
11689 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011690 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011691 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011692 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011693 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011694 // A subtract of one will be selected as a INC. Note that INC doesn't
11695 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011696 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11697 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011698 BaseOp = X86ISD::INC;
11699 Cond = X86::COND_O;
11700 break;
11701 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011702 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011703 Cond = X86::COND_O;
11704 break;
11705 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011706 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011707 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011708 break;
11709 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011710 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11711 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011712 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11713 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011714 BaseOp = X86ISD::DEC;
11715 Cond = X86::COND_O;
11716 break;
11717 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011718 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011719 Cond = X86::COND_O;
11720 break;
11721 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011722 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011723 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011724 break;
11725 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011726 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011727 Cond = X86::COND_O;
11728 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011729 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11730 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11731 MVT::i32);
11732 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011733
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011734 SDValue SetCC =
11735 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11736 DAG.getConstant(X86::COND_O, MVT::i32),
11737 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011738
Dan Gohman6e5fda22011-07-22 18:45:15 +000011739 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011740 }
Bill Wendling74c37652008-12-09 22:08:41 +000011741 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011742
Bill Wendling61edeb52008-12-02 01:06:39 +000011743 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011745 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011746
Bill Wendling61edeb52008-12-02 01:06:39 +000011747 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011748 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11749 DAG.getConstant(Cond, MVT::i32),
11750 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011751
Dan Gohman6e5fda22011-07-22 18:45:15 +000011752 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011753}
11754
Chad Rosier30450e82011-12-22 22:35:21 +000011755SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11756 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011757 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011758 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11759 EVT VT = Op.getValueType();
11760
Craig Toppered2e13d2012-01-22 19:15:14 +000011761 if (!Subtarget->hasSSE2() || !VT.isVector())
11762 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011763
Craig Toppered2e13d2012-01-22 19:15:14 +000011764 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11765 ExtraVT.getScalarType().getSizeInBits();
11766 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11767
11768 switch (VT.getSimpleVT().SimpleTy) {
11769 default: return SDValue();
11770 case MVT::v8i32:
11771 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011772 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011773 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011774 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011775 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011776 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011777
Craig Toppered2e13d2012-01-22 19:15:14 +000011778 // Extract the LHS vectors
11779 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011780 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11781 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011782
Craig Toppered2e13d2012-01-22 19:15:14 +000011783 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11784 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011785
Craig Toppered2e13d2012-01-22 19:15:14 +000011786 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011787 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011788 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11789 ExtraNumElems/2);
11790 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011791
Craig Toppered2e13d2012-01-22 19:15:14 +000011792 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11793 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011794
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011795 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011796 }
11797 // fall through
11798 case MVT::v4i32:
11799 case MVT::v8i16: {
11800 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11801 Op.getOperand(0), ShAmt, DAG);
11802 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011803 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011804 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011805}
11806
Craig Topper55b24052012-09-11 06:15:32 +000011807static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11808 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011809 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011810
Eric Christopher77ed1352011-07-08 00:04:56 +000011811 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11812 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011813 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011814 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011815 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011816 SDValue Ops[] = {
11817 DAG.getRegister(X86::ESP, MVT::i32), // Base
11818 DAG.getTargetConstant(1, MVT::i8), // Scale
11819 DAG.getRegister(0, MVT::i32), // Index
11820 DAG.getTargetConstant(0, MVT::i32), // Disp
11821 DAG.getRegister(0, MVT::i32), // Segment.
11822 Zero,
11823 Chain
11824 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011825 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011826 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11827 array_lengthof(Ops));
11828 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011829 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011830
Eric Christopher9a9d2752010-07-22 02:48:34 +000011831 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011832 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011833 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011834
Chris Lattner132929a2010-08-14 17:26:09 +000011835 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11836 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11837 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11838 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011839
Chris Lattner132929a2010-08-14 17:26:09 +000011840 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11841 if (!Op1 && !Op2 && !Op3 && Op4)
11842 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011843
Chris Lattner132929a2010-08-14 17:26:09 +000011844 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11845 if (Op1 && !Op2 && !Op3 && !Op4)
11846 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011847
11848 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011849 // (MFENCE)>;
11850 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011851}
11852
Craig Topper55b24052012-09-11 06:15:32 +000011853static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11854 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011855 DebugLoc dl = Op.getDebugLoc();
11856 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11857 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11858 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11859 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11860
11861 // The only fence that needs an instruction is a sequentially-consistent
11862 // cross-thread fence.
11863 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11864 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11865 // no-sse2). There isn't any reason to disable it if the target processor
11866 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011867 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011868 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11869
11870 SDValue Chain = Op.getOperand(0);
11871 SDValue Zero = DAG.getConstant(0, MVT::i32);
11872 SDValue Ops[] = {
11873 DAG.getRegister(X86::ESP, MVT::i32), // Base
11874 DAG.getTargetConstant(1, MVT::i8), // Scale
11875 DAG.getRegister(0, MVT::i32), // Index
11876 DAG.getTargetConstant(0, MVT::i32), // Disp
11877 DAG.getRegister(0, MVT::i32), // Segment.
11878 Zero,
11879 Chain
11880 };
11881 SDNode *Res =
11882 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11883 array_lengthof(Ops));
11884 return SDValue(Res, 0);
11885 }
11886
11887 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11888 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11889}
11890
Craig Topper55b24052012-09-11 06:15:32 +000011891static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11892 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011893 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011894 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011895 unsigned Reg = 0;
11896 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011897 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011898 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011899 case MVT::i8: Reg = X86::AL; size = 1; break;
11900 case MVT::i16: Reg = X86::AX; size = 2; break;
11901 case MVT::i32: Reg = X86::EAX; size = 4; break;
11902 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011903 assert(Subtarget->is64Bit() && "Node not type legal!");
11904 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011905 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011906 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011907 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011908 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011909 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011910 Op.getOperand(1),
11911 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011912 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011913 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011914 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011915 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11916 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11917 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011918 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011919 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011920 return cpOut;
11921}
11922
Craig Topper55b24052012-09-11 06:15:32 +000011923static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11924 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011925 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011926 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011927 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011928 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011929 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011930 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11931 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011932 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011933 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11934 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011935 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011936 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011937 rdx.getValue(1)
11938 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011939 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011940}
11941
Craig Topper55b24052012-09-11 06:15:32 +000011942SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011943 EVT SrcVT = Op.getOperand(0).getValueType();
11944 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011945 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011946 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011947 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011948 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011949 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011950 // i64 <=> MMX conversions are Legal.
11951 if (SrcVT==MVT::i64 && DstVT.isVector())
11952 return Op;
11953 if (DstVT==MVT::i64 && SrcVT.isVector())
11954 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011955 // MMX <=> MMX conversions are Legal.
11956 if (SrcVT.isVector() && DstVT.isVector())
11957 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011958 // All other conversions need to be expanded.
11959 return SDValue();
11960}
Chris Lattner5b856542010-12-20 00:59:46 +000011961
Craig Topper55b24052012-09-11 06:15:32 +000011962static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011963 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011964 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011965 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011966 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011967 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011968 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011969 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011970 Node->getOperand(0),
11971 Node->getOperand(1), negOp,
11972 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011973 cast<AtomicSDNode>(Node)->getAlignment(),
11974 cast<AtomicSDNode>(Node)->getOrdering(),
11975 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011976}
11977
Eli Friedman327236c2011-08-24 20:50:09 +000011978static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11979 SDNode *Node = Op.getNode();
11980 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011981 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011982
11983 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011984 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11985 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11986 // (The only way to get a 16-byte store is cmpxchg16b)
11987 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11988 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11989 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011990 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11991 cast<AtomicSDNode>(Node)->getMemoryVT(),
11992 Node->getOperand(0),
11993 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011994 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011995 cast<AtomicSDNode>(Node)->getOrdering(),
11996 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011997 return Swap.getValue(1);
11998 }
11999 // Other atomic stores have a simple pattern.
12000 return Op;
12001}
12002
Chris Lattner5b856542010-12-20 00:59:46 +000012003static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12004 EVT VT = Op.getNode()->getValueType(0);
12005
12006 // Let legalize expand this if it isn't a legal type yet.
12007 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12008 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012009
Chris Lattner5b856542010-12-20 00:59:46 +000012010 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012011
Chris Lattner5b856542010-12-20 00:59:46 +000012012 unsigned Opc;
12013 bool ExtraOp = false;
12014 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012015 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012016 case ISD::ADDC: Opc = X86ISD::ADD; break;
12017 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12018 case ISD::SUBC: Opc = X86ISD::SUB; break;
12019 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12020 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012021
Chris Lattner5b856542010-12-20 00:59:46 +000012022 if (!ExtraOp)
12023 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12024 Op.getOperand(1));
12025 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12026 Op.getOperand(1), Op.getOperand(2));
12027}
12028
Evan Cheng8688a582013-01-29 02:32:37 +000012029SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012030 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012031
Evan Cheng8688a582013-01-29 02:32:37 +000012032 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12033 // which returns the values in two XMM registers.
12034 DebugLoc dl = Op.getDebugLoc();
12035 SDValue Arg = Op.getOperand(0);
12036 EVT ArgVT = Arg.getValueType();
12037 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012038
Evan Cheng8688a582013-01-29 02:32:37 +000012039 ArgListTy Args;
12040 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012041
Evan Cheng8688a582013-01-29 02:32:37 +000012042 Entry.Node = Arg;
12043 Entry.Ty = ArgTy;
12044 Entry.isSExt = false;
12045 Entry.isZExt = false;
12046 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012047
12048 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12049 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12050 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012051 const char *LibcallName = (ArgVT == MVT::f64)
12052 ? "__sincos_stret" : "__sincosf_stret";
12053 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012054
Evan Cheng8688a582013-01-29 02:32:37 +000012055 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12056 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012057 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12058 false, false, false, false, 0,
12059 CallingConv::C, /*isTaillCall=*/false,
12060 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12061 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012062 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012063 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012064}
12065
Evan Cheng0db9fe62006-04-25 20:13:52 +000012066/// LowerOperation - Provide custom lowering hooks for some operations.
12067///
Dan Gohmand858e902010-04-17 15:26:15 +000012068SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012069 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012070 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012071 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012072 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12073 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12074 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012075 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012076 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012077 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012078 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012079 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12080 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12081 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012082 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12083 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012084 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12085 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12086 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012087 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012088 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012089 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012090 case ISD::SHL_PARTS:
12091 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012092 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012093 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012094 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012095 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012096 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12097 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12098 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012099 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012100 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012101 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012102 case ISD::FABS: return LowerFABS(Op, DAG);
12103 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012104 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012105 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012106 case ISD::SETCC: return LowerSETCC(Op, DAG);
12107 case ISD::SELECT: return LowerSELECT(Op, DAG);
12108 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012109 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012110 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012111 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012112 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012113 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012114 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012115 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12116 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012117 case ISD::FRAME_TO_ARGS_OFFSET:
12118 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012119 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012120 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012121 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12122 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012123 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12124 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012125 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012126 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012127 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012128 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012129 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012130 case ISD::SRA:
12131 case ISD::SRL:
12132 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012133 case ISD::SADDO:
12134 case ISD::UADDO:
12135 case ISD::SSUBO:
12136 case ISD::USUBO:
12137 case ISD::SMULO:
12138 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012139 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012140 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012141 case ISD::ADDC:
12142 case ISD::ADDE:
12143 case ISD::SUBC:
12144 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012145 case ISD::ADD: return LowerADD(Op, DAG);
12146 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012147 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012148 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012149 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012150}
12151
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012152static void ReplaceATOMIC_LOAD(SDNode *Node,
12153 SmallVectorImpl<SDValue> &Results,
12154 SelectionDAG &DAG) {
12155 DebugLoc dl = Node->getDebugLoc();
12156 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12157
12158 // Convert wide load -> cmpxchg8b/cmpxchg16b
12159 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12160 // (The only way to get a 16-byte load is cmpxchg16b)
12161 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012162 SDValue Zero = DAG.getConstant(0, VT);
12163 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012164 Node->getOperand(0),
12165 Node->getOperand(1), Zero, Zero,
12166 cast<AtomicSDNode>(Node)->getMemOperand(),
12167 cast<AtomicSDNode>(Node)->getOrdering(),
12168 cast<AtomicSDNode>(Node)->getSynchScope());
12169 Results.push_back(Swap.getValue(0));
12170 Results.push_back(Swap.getValue(1));
12171}
12172
Craig Topperc0878702012-08-17 06:55:11 +000012173static void
Duncan Sands1607f052008-12-01 11:39:25 +000012174ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012175 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012176 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012177 assert (Node->getValueType(0) == MVT::i64 &&
12178 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012179
12180 SDValue Chain = Node->getOperand(0);
12181 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012182 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012183 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012184 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012185 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012186 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012187 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012188 SDValue Result =
12189 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12190 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012191 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012192 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012193 Results.push_back(Result.getValue(2));
12194}
12195
Duncan Sands126d9072008-07-04 11:47:58 +000012196/// ReplaceNodeResults - Replace a node with an illegal result type
12197/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012198void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12199 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012200 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012201 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012202 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012203 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012204 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012205 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012206 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012207 case ISD::ADDC:
12208 case ISD::ADDE:
12209 case ISD::SUBC:
12210 case ISD::SUBE:
12211 // We don't want to expand or promote these.
12212 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012213 case ISD::FP_TO_SINT:
12214 case ISD::FP_TO_UINT: {
12215 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12216
12217 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12218 return;
12219
Eli Friedman948e95a2009-05-23 09:59:16 +000012220 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012221 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012222 SDValue FIST = Vals.first, StackSlot = Vals.second;
12223 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012224 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012225 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012226 if (StackSlot.getNode() != 0)
12227 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12228 MachinePointerInfo(),
12229 false, false, false, 0));
12230 else
12231 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012232 }
12233 return;
12234 }
Michael Liao991b6a22012-10-24 04:09:32 +000012235 case ISD::UINT_TO_FP: {
12236 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12237 N->getValueType(0) != MVT::v2f32)
12238 return;
12239 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12240 N->getOperand(0));
12241 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12242 MVT::f64);
12243 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12244 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12245 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12246 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12247 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12248 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12249 return;
12250 }
Michael Liao44c2d612012-10-10 16:53:28 +000012251 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012252 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12253 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012254 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12255 Results.push_back(V);
12256 return;
12257 }
Duncan Sands1607f052008-12-01 11:39:25 +000012258 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012259 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012260 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012261 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012262 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012263 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012264 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012265 eax.getValue(2));
12266 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12267 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012268 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012269 Results.push_back(edx.getValue(1));
12270 return;
12271 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012272 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012273 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012274 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012275 bool Regs64bit = T == MVT::i128;
12276 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012277 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012278 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12279 DAG.getConstant(0, HalfT));
12280 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12281 DAG.getConstant(1, HalfT));
12282 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12283 Regs64bit ? X86::RAX : X86::EAX,
12284 cpInL, SDValue());
12285 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12286 Regs64bit ? X86::RDX : X86::EDX,
12287 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012288 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012289 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12290 DAG.getConstant(0, HalfT));
12291 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12292 DAG.getConstant(1, HalfT));
12293 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12294 Regs64bit ? X86::RBX : X86::EBX,
12295 swapInL, cpInH.getValue(1));
12296 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012297 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012298 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012299 SDValue Ops[] = { swapInH.getValue(0),
12300 N->getOperand(1),
12301 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012302 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012303 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012304 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12305 X86ISD::LCMPXCHG8_DAG;
12306 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012307 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012308 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12309 Regs64bit ? X86::RAX : X86::EAX,
12310 HalfT, Result.getValue(1));
12311 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12312 Regs64bit ? X86::RDX : X86::EDX,
12313 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012314 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012315 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012316 Results.push_back(cpOutH.getValue(1));
12317 return;
12318 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012319 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012320 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012321 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012322 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012323 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012324 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012325 case ISD::ATOMIC_LOAD_MAX:
12326 case ISD::ATOMIC_LOAD_MIN:
12327 case ISD::ATOMIC_LOAD_UMAX:
12328 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012329 case ISD::ATOMIC_SWAP: {
12330 unsigned Opc;
12331 switch (N->getOpcode()) {
12332 default: llvm_unreachable("Unexpected opcode");
12333 case ISD::ATOMIC_LOAD_ADD:
12334 Opc = X86ISD::ATOMADD64_DAG;
12335 break;
12336 case ISD::ATOMIC_LOAD_AND:
12337 Opc = X86ISD::ATOMAND64_DAG;
12338 break;
12339 case ISD::ATOMIC_LOAD_NAND:
12340 Opc = X86ISD::ATOMNAND64_DAG;
12341 break;
12342 case ISD::ATOMIC_LOAD_OR:
12343 Opc = X86ISD::ATOMOR64_DAG;
12344 break;
12345 case ISD::ATOMIC_LOAD_SUB:
12346 Opc = X86ISD::ATOMSUB64_DAG;
12347 break;
12348 case ISD::ATOMIC_LOAD_XOR:
12349 Opc = X86ISD::ATOMXOR64_DAG;
12350 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012351 case ISD::ATOMIC_LOAD_MAX:
12352 Opc = X86ISD::ATOMMAX64_DAG;
12353 break;
12354 case ISD::ATOMIC_LOAD_MIN:
12355 Opc = X86ISD::ATOMMIN64_DAG;
12356 break;
12357 case ISD::ATOMIC_LOAD_UMAX:
12358 Opc = X86ISD::ATOMUMAX64_DAG;
12359 break;
12360 case ISD::ATOMIC_LOAD_UMIN:
12361 Opc = X86ISD::ATOMUMIN64_DAG;
12362 break;
Craig Topperc0878702012-08-17 06:55:11 +000012363 case ISD::ATOMIC_SWAP:
12364 Opc = X86ISD::ATOMSWAP64_DAG;
12365 break;
12366 }
12367 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012368 return;
Craig Topperc0878702012-08-17 06:55:11 +000012369 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012370 case ISD::ATOMIC_LOAD:
12371 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012372 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012373}
12374
Evan Cheng72261582005-12-20 06:22:03 +000012375const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12376 switch (Opcode) {
12377 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012378 case X86ISD::BSF: return "X86ISD::BSF";
12379 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012380 case X86ISD::SHLD: return "X86ISD::SHLD";
12381 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012382 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012383 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012384 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012385 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012386 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012387 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012388 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12389 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12390 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012391 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012392 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012393 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012394 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012395 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012396 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012397 case X86ISD::COMI: return "X86ISD::COMI";
12398 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012399 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012400 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012401 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12402 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012403 case X86ISD::CMOV: return "X86ISD::CMOV";
12404 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012405 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012406 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12407 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012408 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012409 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012410 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012411 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012412 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012413 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12414 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012415 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012416 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012417 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012418 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012419 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012420 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012421 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012422 case X86ISD::HADD: return "X86ISD::HADD";
12423 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012424 case X86ISD::FHADD: return "X86ISD::FHADD";
12425 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012426 case X86ISD::UMAX: return "X86ISD::UMAX";
12427 case X86ISD::UMIN: return "X86ISD::UMIN";
12428 case X86ISD::SMAX: return "X86ISD::SMAX";
12429 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012430 case X86ISD::FMAX: return "X86ISD::FMAX";
12431 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012432 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12433 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012434 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12435 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012436 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012437 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012438 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012439 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12440 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012441 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012442 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012443 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012444 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012445 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12446 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012447 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12448 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12449 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12450 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12451 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12452 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012453 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012454 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012455 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012456 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12457 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012458 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012459 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012460 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12461 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012462 case X86ISD::VSHL: return "X86ISD::VSHL";
12463 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012464 case X86ISD::VSRA: return "X86ISD::VSRA";
12465 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12466 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12467 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012468 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012469 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12470 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012471 case X86ISD::ADD: return "X86ISD::ADD";
12472 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012473 case X86ISD::ADC: return "X86ISD::ADC";
12474 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012475 case X86ISD::SMUL: return "X86ISD::SMUL";
12476 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012477 case X86ISD::INC: return "X86ISD::INC";
12478 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012479 case X86ISD::OR: return "X86ISD::OR";
12480 case X86ISD::XOR: return "X86ISD::XOR";
12481 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012482 case X86ISD::BLSI: return "X86ISD::BLSI";
12483 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12484 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012485 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012486 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012487 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012488 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012489 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12490 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012491 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012492 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012493 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012494 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012495 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012496 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12497 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012498 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12499 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12500 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012501 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12502 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012503 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12504 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012505 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012506 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012507 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012508 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12509 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012510 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012511 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012512 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012513 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012514 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012515 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012516 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012517 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012518 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012519 case X86ISD::FMADD: return "X86ISD::FMADD";
12520 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12521 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12522 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12523 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12524 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012525 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12526 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012527 }
12528}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012529
Chris Lattnerc9addb72007-03-30 23:15:24 +000012530// isLegalAddressingMode - Return true if the addressing mode represented
12531// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012532bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012533 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012534 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012535 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012536 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012537
Chris Lattnerc9addb72007-03-30 23:15:24 +000012538 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012539 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012540 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012541
Chris Lattnerc9addb72007-03-30 23:15:24 +000012542 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012543 unsigned GVFlags =
12544 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012545
Chris Lattnerdfed4132009-07-10 07:38:24 +000012546 // If a reference to this global requires an extra load, we can't fold it.
12547 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012548 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012549
Chris Lattnerdfed4132009-07-10 07:38:24 +000012550 // If BaseGV requires a register for the PIC base, we cannot also have a
12551 // BaseReg specified.
12552 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012553 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012554
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012555 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012556 if ((M != CodeModel::Small || R != Reloc::Static) &&
12557 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012558 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012559 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012560
Chris Lattnerc9addb72007-03-30 23:15:24 +000012561 switch (AM.Scale) {
12562 case 0:
12563 case 1:
12564 case 2:
12565 case 4:
12566 case 8:
12567 // These scales always work.
12568 break;
12569 case 3:
12570 case 5:
12571 case 9:
12572 // These scales are formed with basereg+scalereg. Only accept if there is
12573 // no basereg yet.
12574 if (AM.HasBaseReg)
12575 return false;
12576 break;
12577 default: // Other stuff never works.
12578 return false;
12579 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012580
Chris Lattnerc9addb72007-03-30 23:15:24 +000012581 return true;
12582}
12583
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012584bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012585 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012586 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012587 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12588 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012589 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012590}
12591
Evan Cheng70e10d32012-07-17 06:53:39 +000012592bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012593 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012594}
12595
12596bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012597 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012598 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012599}
12600
Owen Andersone50ed302009-08-10 22:56:29 +000012601bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012602 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012603 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012604 unsigned NumBits1 = VT1.getSizeInBits();
12605 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012606 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012607}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012608
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012609bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012610 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012611 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012612}
12613
Owen Andersone50ed302009-08-10 22:56:29 +000012614bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012615 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012616 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012617}
12618
Evan Cheng2766a472012-12-06 19:13:27 +000012619bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12620 EVT VT1 = Val.getValueType();
12621 if (isZExtFree(VT1, VT2))
12622 return true;
12623
12624 if (Val.getOpcode() != ISD::LOAD)
12625 return false;
12626
12627 if (!VT1.isSimple() || !VT1.isInteger() ||
12628 !VT2.isSimple() || !VT2.isInteger())
12629 return false;
12630
12631 switch (VT1.getSimpleVT().SimpleTy) {
12632 default: break;
12633 case MVT::i8:
12634 case MVT::i16:
12635 case MVT::i32:
12636 // X86 has 8, 16, and 32-bit zero-extending loads.
12637 return true;
12638 }
12639
12640 return false;
12641}
12642
Owen Andersone50ed302009-08-10 22:56:29 +000012643bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012644 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012645 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012646}
12647
Evan Cheng60c07e12006-07-05 22:17:51 +000012648/// isShuffleMaskLegal - Targets can use this to indicate that they only
12649/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12650/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12651/// are assumed to be legal.
12652bool
Eric Christopherfd179292009-08-27 18:07:15 +000012653X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012654 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012655 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012656 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012657 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012658
Nate Begemana09008b2009-10-19 02:17:23 +000012659 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012660 return (VT.getVectorNumElements() == 2 ||
12661 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12662 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012663 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012664 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012665 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12666 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012667 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012668 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12669 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12670 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12671 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012672}
12673
Dan Gohman7d8143f2008-04-09 20:09:42 +000012674bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012675X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012676 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012677 unsigned NumElts = VT.getVectorNumElements();
12678 // FIXME: This collection of masks seems suspect.
12679 if (NumElts == 2)
12680 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012681 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012682 return (isMOVLMask(Mask, VT) ||
12683 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012684 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12685 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012686 }
12687 return false;
12688}
12689
12690//===----------------------------------------------------------------------===//
12691// X86 Scheduler Hooks
12692//===----------------------------------------------------------------------===//
12693
Michael Liaobe02a902012-11-08 07:28:54 +000012694/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012695static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12696 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012697 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012698
12699 const BasicBlock *BB = MBB->getBasicBlock();
12700 MachineFunction::iterator I = MBB;
12701 ++I;
12702
12703 // For the v = xbegin(), we generate
12704 //
12705 // thisMBB:
12706 // xbegin sinkMBB
12707 //
12708 // mainMBB:
12709 // eax = -1
12710 //
12711 // sinkMBB:
12712 // v = eax
12713
12714 MachineBasicBlock *thisMBB = MBB;
12715 MachineFunction *MF = MBB->getParent();
12716 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12717 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12718 MF->insert(I, mainMBB);
12719 MF->insert(I, sinkMBB);
12720
12721 // Transfer the remainder of BB and its successor edges to sinkMBB.
12722 sinkMBB->splice(sinkMBB->begin(), MBB,
12723 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12724 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12725
12726 // thisMBB:
12727 // xbegin sinkMBB
12728 // # fallthrough to mainMBB
12729 // # abortion to sinkMBB
12730 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12731 thisMBB->addSuccessor(mainMBB);
12732 thisMBB->addSuccessor(sinkMBB);
12733
12734 // mainMBB:
12735 // EAX = -1
12736 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12737 mainMBB->addSuccessor(sinkMBB);
12738
12739 // sinkMBB:
12740 // EAX is live into the sinkMBB
12741 sinkMBB->addLiveIn(X86::EAX);
12742 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12743 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12744 .addReg(X86::EAX);
12745
12746 MI->eraseFromParent();
12747 return sinkMBB;
12748}
12749
Michael Liaob118a072012-09-20 03:06:15 +000012750// Get CMPXCHG opcode for the specified data type.
12751static unsigned getCmpXChgOpcode(EVT VT) {
12752 switch (VT.getSimpleVT().SimpleTy) {
12753 case MVT::i8: return X86::LCMPXCHG8;
12754 case MVT::i16: return X86::LCMPXCHG16;
12755 case MVT::i32: return X86::LCMPXCHG32;
12756 case MVT::i64: return X86::LCMPXCHG64;
12757 default:
12758 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012759 }
Michael Liaob118a072012-09-20 03:06:15 +000012760 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012761}
12762
Michael Liaob118a072012-09-20 03:06:15 +000012763// Get LOAD opcode for the specified data type.
12764static unsigned getLoadOpcode(EVT VT) {
12765 switch (VT.getSimpleVT().SimpleTy) {
12766 case MVT::i8: return X86::MOV8rm;
12767 case MVT::i16: return X86::MOV16rm;
12768 case MVT::i32: return X86::MOV32rm;
12769 case MVT::i64: return X86::MOV64rm;
12770 default:
12771 break;
12772 }
12773 llvm_unreachable("Invalid operand size!");
12774}
12775
12776// Get opcode of the non-atomic one from the specified atomic instruction.
12777static unsigned getNonAtomicOpcode(unsigned Opc) {
12778 switch (Opc) {
12779 case X86::ATOMAND8: return X86::AND8rr;
12780 case X86::ATOMAND16: return X86::AND16rr;
12781 case X86::ATOMAND32: return X86::AND32rr;
12782 case X86::ATOMAND64: return X86::AND64rr;
12783 case X86::ATOMOR8: return X86::OR8rr;
12784 case X86::ATOMOR16: return X86::OR16rr;
12785 case X86::ATOMOR32: return X86::OR32rr;
12786 case X86::ATOMOR64: return X86::OR64rr;
12787 case X86::ATOMXOR8: return X86::XOR8rr;
12788 case X86::ATOMXOR16: return X86::XOR16rr;
12789 case X86::ATOMXOR32: return X86::XOR32rr;
12790 case X86::ATOMXOR64: return X86::XOR64rr;
12791 }
12792 llvm_unreachable("Unhandled atomic-load-op opcode!");
12793}
12794
12795// Get opcode of the non-atomic one from the specified atomic instruction with
12796// extra opcode.
12797static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12798 unsigned &ExtraOpc) {
12799 switch (Opc) {
12800 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12801 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12802 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12803 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012804 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012805 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12806 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12807 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012808 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012809 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12810 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12811 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012812 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012813 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12814 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12815 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012816 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012817 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12818 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12819 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12820 }
12821 llvm_unreachable("Unhandled atomic-load-op opcode!");
12822}
12823
12824// Get opcode of the non-atomic one from the specified atomic instruction for
12825// 64-bit data type on 32-bit target.
12826static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12827 switch (Opc) {
12828 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12829 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12830 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12831 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12832 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12833 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012834 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12835 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12836 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12837 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012838 }
12839 llvm_unreachable("Unhandled atomic-load-op opcode!");
12840}
12841
12842// Get opcode of the non-atomic one from the specified atomic instruction for
12843// 64-bit data type on 32-bit target with extra opcode.
12844static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12845 unsigned &HiOpc,
12846 unsigned &ExtraOpc) {
12847 switch (Opc) {
12848 case X86::ATOMNAND6432:
12849 ExtraOpc = X86::NOT32r;
12850 HiOpc = X86::AND32rr;
12851 return X86::AND32rr;
12852 }
12853 llvm_unreachable("Unhandled atomic-load-op opcode!");
12854}
12855
12856// Get pseudo CMOV opcode from the specified data type.
12857static unsigned getPseudoCMOVOpc(EVT VT) {
12858 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012859 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012860 case MVT::i16: return X86::CMOV_GR16;
12861 case MVT::i32: return X86::CMOV_GR32;
12862 default:
12863 break;
12864 }
12865 llvm_unreachable("Unknown CMOV opcode!");
12866}
12867
12868// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12869// They will be translated into a spin-loop or compare-exchange loop from
12870//
12871// ...
12872// dst = atomic-fetch-op MI.addr, MI.val
12873// ...
12874//
12875// to
12876//
12877// ...
12878// EAX = LOAD MI.addr
12879// loop:
12880// t1 = OP MI.val, EAX
12881// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12882// JNE loop
12883// sink:
12884// dst = EAX
12885// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012886MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012887X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12888 MachineBasicBlock *MBB) const {
12889 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12890 DebugLoc DL = MI->getDebugLoc();
12891
12892 MachineFunction *MF = MBB->getParent();
12893 MachineRegisterInfo &MRI = MF->getRegInfo();
12894
12895 const BasicBlock *BB = MBB->getBasicBlock();
12896 MachineFunction::iterator I = MBB;
12897 ++I;
12898
Michael Liao13d08bf2013-01-22 21:47:38 +000012899 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000012900 "Unexpected number of operands");
12901
12902 assert(MI->hasOneMemOperand() &&
12903 "Expected atomic-load-op to have one memoperand");
12904
12905 // Memory Reference
12906 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12907 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12908
12909 unsigned DstReg, SrcReg;
12910 unsigned MemOpndSlot;
12911
12912 unsigned CurOp = 0;
12913
12914 DstReg = MI->getOperand(CurOp++).getReg();
12915 MemOpndSlot = CurOp;
12916 CurOp += X86::AddrNumOperands;
12917 SrcReg = MI->getOperand(CurOp++).getReg();
12918
12919 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012920 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012921 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12922
12923 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12924 unsigned LOADOpc = getLoadOpcode(VT);
12925
12926 // For the atomic load-arith operator, we generate
12927 //
12928 // thisMBB:
12929 // EAX = LOAD [MI.addr]
12930 // mainMBB:
12931 // t1 = OP MI.val, EAX
12932 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12933 // JNE mainMBB
12934 // sinkMBB:
12935
12936 MachineBasicBlock *thisMBB = MBB;
12937 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12938 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12939 MF->insert(I, mainMBB);
12940 MF->insert(I, sinkMBB);
12941
12942 MachineInstrBuilder MIB;
12943
12944 // Transfer the remainder of BB and its successor edges to sinkMBB.
12945 sinkMBB->splice(sinkMBB->begin(), MBB,
12946 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12947 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12948
12949 // thisMBB:
12950 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12951 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12952 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12953 MIB.setMemRefs(MMOBegin, MMOEnd);
12954
12955 thisMBB->addSuccessor(mainMBB);
12956
12957 // mainMBB:
12958 MachineBasicBlock *origMainMBB = mainMBB;
12959 mainMBB->addLiveIn(AccPhyReg);
12960
12961 // Copy AccPhyReg as it is used more than once.
12962 unsigned AccReg = MRI.createVirtualRegister(RC);
12963 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12964 .addReg(AccPhyReg);
12965
12966 unsigned t1 = MRI.createVirtualRegister(RC);
12967 unsigned Opc = MI->getOpcode();
12968 switch (Opc) {
12969 default:
12970 llvm_unreachable("Unhandled atomic-load-op opcode!");
12971 case X86::ATOMAND8:
12972 case X86::ATOMAND16:
12973 case X86::ATOMAND32:
12974 case X86::ATOMAND64:
12975 case X86::ATOMOR8:
12976 case X86::ATOMOR16:
12977 case X86::ATOMOR32:
12978 case X86::ATOMOR64:
12979 case X86::ATOMXOR8:
12980 case X86::ATOMXOR16:
12981 case X86::ATOMXOR32:
12982 case X86::ATOMXOR64: {
12983 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12984 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12985 .addReg(AccReg);
12986 break;
12987 }
12988 case X86::ATOMNAND8:
12989 case X86::ATOMNAND16:
12990 case X86::ATOMNAND32:
12991 case X86::ATOMNAND64: {
12992 unsigned t2 = MRI.createVirtualRegister(RC);
12993 unsigned NOTOpc;
12994 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12995 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12996 .addReg(AccReg);
12997 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12998 break;
12999 }
Michael Liao08382492012-09-21 03:00:17 +000013000 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013001 case X86::ATOMMAX16:
13002 case X86::ATOMMAX32:
13003 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013004 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013005 case X86::ATOMMIN16:
13006 case X86::ATOMMIN32:
13007 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013008 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013009 case X86::ATOMUMAX16:
13010 case X86::ATOMUMAX32:
13011 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013012 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013013 case X86::ATOMUMIN16:
13014 case X86::ATOMUMIN32:
13015 case X86::ATOMUMIN64: {
13016 unsigned CMPOpc;
13017 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13018
13019 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13020 .addReg(SrcReg)
13021 .addReg(AccReg);
13022
13023 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013024 if (VT != MVT::i8) {
13025 // Native support
13026 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
13027 .addReg(SrcReg)
13028 .addReg(AccReg);
13029 } else {
13030 // Promote i8 to i32 to use CMOV32
13031 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
13032 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13033 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
13034 unsigned t2 = MRI.createVirtualRegister(RC32);
13035
13036 unsigned Undef = MRI.createVirtualRegister(RC32);
13037 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13038
13039 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13040 .addReg(Undef)
13041 .addReg(SrcReg)
13042 .addImm(X86::sub_8bit);
13043 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13044 .addReg(Undef)
13045 .addReg(AccReg)
13046 .addImm(X86::sub_8bit);
13047
13048 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
13049 .addReg(SrcReg32)
13050 .addReg(AccReg32);
13051
13052 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
13053 .addReg(t2, 0, X86::sub_8bit);
13054 }
Michael Liaob118a072012-09-20 03:06:15 +000013055 } else {
13056 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013057 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013058 "Invalid atomic-load-op transformation!");
13059 unsigned SelOpc = getPseudoCMOVOpc(VT);
13060 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13061 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
13062 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
13063 .addReg(SrcReg).addReg(AccReg)
13064 .addImm(CC);
13065 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13066 }
13067 break;
13068 }
13069 }
13070
13071 // Copy AccPhyReg back from virtual register.
13072 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
13073 .addReg(AccReg);
13074
13075 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13076 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13077 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13078 MIB.addReg(t1);
13079 MIB.setMemRefs(MMOBegin, MMOEnd);
13080
13081 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13082
13083 mainMBB->addSuccessor(origMainMBB);
13084 mainMBB->addSuccessor(sinkMBB);
13085
13086 // sinkMBB:
13087 sinkMBB->addLiveIn(AccPhyReg);
13088
13089 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13090 TII->get(TargetOpcode::COPY), DstReg)
13091 .addReg(AccPhyReg);
13092
13093 MI->eraseFromParent();
13094 return sinkMBB;
13095}
13096
13097// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13098// instructions. They will be translated into a spin-loop or compare-exchange
13099// loop from
13100//
13101// ...
13102// dst = atomic-fetch-op MI.addr, MI.val
13103// ...
13104//
13105// to
13106//
13107// ...
13108// EAX = LOAD [MI.addr + 0]
13109// EDX = LOAD [MI.addr + 4]
13110// loop:
13111// EBX = OP MI.val.lo, EAX
13112// ECX = OP MI.val.hi, EDX
13113// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13114// JNE loop
13115// sink:
13116// dst = EDX:EAX
13117// ...
13118MachineBasicBlock *
13119X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13120 MachineBasicBlock *MBB) const {
13121 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13122 DebugLoc DL = MI->getDebugLoc();
13123
13124 MachineFunction *MF = MBB->getParent();
13125 MachineRegisterInfo &MRI = MF->getRegInfo();
13126
13127 const BasicBlock *BB = MBB->getBasicBlock();
13128 MachineFunction::iterator I = MBB;
13129 ++I;
13130
Michael Liao13d08bf2013-01-22 21:47:38 +000013131 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013132 "Unexpected number of operands");
13133
13134 assert(MI->hasOneMemOperand() &&
13135 "Expected atomic-load-op32 to have one memoperand");
13136
13137 // Memory Reference
13138 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13139 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13140
13141 unsigned DstLoReg, DstHiReg;
13142 unsigned SrcLoReg, SrcHiReg;
13143 unsigned MemOpndSlot;
13144
13145 unsigned CurOp = 0;
13146
13147 DstLoReg = MI->getOperand(CurOp++).getReg();
13148 DstHiReg = MI->getOperand(CurOp++).getReg();
13149 MemOpndSlot = CurOp;
13150 CurOp += X86::AddrNumOperands;
13151 SrcLoReg = MI->getOperand(CurOp++).getReg();
13152 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013153
Craig Topperc9099502012-04-20 06:31:50 +000013154 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013155 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013156
Michael Liaob118a072012-09-20 03:06:15 +000013157 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13158 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013159
Michael Liaob118a072012-09-20 03:06:15 +000013160 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013161 //
Michael Liaob118a072012-09-20 03:06:15 +000013162 // thisMBB:
13163 // EAX = LOAD [MI.addr + 0]
13164 // EDX = LOAD [MI.addr + 4]
13165 // mainMBB:
13166 // EBX = OP MI.vallo, EAX
13167 // ECX = OP MI.valhi, EDX
13168 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13169 // JNE mainMBB
13170 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013171
Mon P Wang63307c32008-05-05 19:05:59 +000013172 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013173 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13174 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13175 MF->insert(I, mainMBB);
13176 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013177
Michael Liaob118a072012-09-20 03:06:15 +000013178 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013179
Michael Liaob118a072012-09-20 03:06:15 +000013180 // Transfer the remainder of BB and its successor edges to sinkMBB.
13181 sinkMBB->splice(sinkMBB->begin(), MBB,
13182 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13183 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013184
Michael Liaob118a072012-09-20 03:06:15 +000013185 // thisMBB:
13186 // Lo
13187 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13188 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13189 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13190 MIB.setMemRefs(MMOBegin, MMOEnd);
13191 // Hi
13192 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13193 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013194 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013195 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013196 else
Michael Liaob118a072012-09-20 03:06:15 +000013197 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13198 }
13199 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013200
Michael Liaob118a072012-09-20 03:06:15 +000013201 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013202
Michael Liaob118a072012-09-20 03:06:15 +000013203 // mainMBB:
13204 MachineBasicBlock *origMainMBB = mainMBB;
13205 mainMBB->addLiveIn(X86::EAX);
13206 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013207
Michael Liaob118a072012-09-20 03:06:15 +000013208 // Copy EDX:EAX as they are used more than once.
13209 unsigned LoReg = MRI.createVirtualRegister(RC);
13210 unsigned HiReg = MRI.createVirtualRegister(RC);
13211 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13212 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013213
Michael Liaob118a072012-09-20 03:06:15 +000013214 unsigned t1L = MRI.createVirtualRegister(RC);
13215 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013216
Michael Liaob118a072012-09-20 03:06:15 +000013217 unsigned Opc = MI->getOpcode();
13218 switch (Opc) {
13219 default:
13220 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13221 case X86::ATOMAND6432:
13222 case X86::ATOMOR6432:
13223 case X86::ATOMXOR6432:
13224 case X86::ATOMADD6432:
13225 case X86::ATOMSUB6432: {
13226 unsigned HiOpc;
13227 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013228 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13229 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013230 break;
13231 }
13232 case X86::ATOMNAND6432: {
13233 unsigned HiOpc, NOTOpc;
13234 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13235 unsigned t2L = MRI.createVirtualRegister(RC);
13236 unsigned t2H = MRI.createVirtualRegister(RC);
13237 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13238 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13239 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13240 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13241 break;
13242 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013243 case X86::ATOMMAX6432:
13244 case X86::ATOMMIN6432:
13245 case X86::ATOMUMAX6432:
13246 case X86::ATOMUMIN6432: {
13247 unsigned HiOpc;
13248 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13249 unsigned cL = MRI.createVirtualRegister(RC8);
13250 unsigned cH = MRI.createVirtualRegister(RC8);
13251 unsigned cL32 = MRI.createVirtualRegister(RC);
13252 unsigned cH32 = MRI.createVirtualRegister(RC);
13253 unsigned cc = MRI.createVirtualRegister(RC);
13254 // cl := cmp src_lo, lo
13255 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13256 .addReg(SrcLoReg).addReg(LoReg);
13257 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13258 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13259 // ch := cmp src_hi, hi
13260 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13261 .addReg(SrcHiReg).addReg(HiReg);
13262 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13263 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13264 // cc := if (src_hi == hi) ? cl : ch;
13265 if (Subtarget->hasCMov()) {
13266 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13267 .addReg(cH32).addReg(cL32);
13268 } else {
13269 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13270 .addReg(cH32).addReg(cL32)
13271 .addImm(X86::COND_E);
13272 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13273 }
13274 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13275 if (Subtarget->hasCMov()) {
13276 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13277 .addReg(SrcLoReg).addReg(LoReg);
13278 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13279 .addReg(SrcHiReg).addReg(HiReg);
13280 } else {
13281 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13282 .addReg(SrcLoReg).addReg(LoReg)
13283 .addImm(X86::COND_NE);
13284 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13285 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13286 .addReg(SrcHiReg).addReg(HiReg)
13287 .addImm(X86::COND_NE);
13288 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13289 }
13290 break;
13291 }
Michael Liaob118a072012-09-20 03:06:15 +000013292 case X86::ATOMSWAP6432: {
13293 unsigned HiOpc;
13294 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13295 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13296 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13297 break;
13298 }
13299 }
Mon P Wang63307c32008-05-05 19:05:59 +000013300
Michael Liaob118a072012-09-20 03:06:15 +000013301 // Copy EDX:EAX back from HiReg:LoReg
13302 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13303 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13304 // Copy ECX:EBX from t1H:t1L
13305 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13306 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013307
Michael Liaob118a072012-09-20 03:06:15 +000013308 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13309 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13310 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13311 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013312
Michael Liaob118a072012-09-20 03:06:15 +000013313 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013314
Michael Liaob118a072012-09-20 03:06:15 +000013315 mainMBB->addSuccessor(origMainMBB);
13316 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013317
Michael Liaob118a072012-09-20 03:06:15 +000013318 // sinkMBB:
13319 sinkMBB->addLiveIn(X86::EAX);
13320 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013321
Michael Liaob118a072012-09-20 03:06:15 +000013322 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13323 TII->get(TargetOpcode::COPY), DstLoReg)
13324 .addReg(X86::EAX);
13325 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13326 TII->get(TargetOpcode::COPY), DstHiReg)
13327 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013328
Michael Liaob118a072012-09-20 03:06:15 +000013329 MI->eraseFromParent();
13330 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013331}
13332
Eric Christopherf83a5de2009-08-27 18:08:16 +000013333// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013334// or XMM0_V32I8 in AVX all of this code can be replaced with that
13335// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013336static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13337 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013338 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013339 switch (MI->getOpcode()) {
13340 default: llvm_unreachable("illegal opcode!");
13341 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13342 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13343 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13344 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13345 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13346 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13347 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13348 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013349 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013350
Craig Topper8aae8dd2012-11-10 08:57:41 +000013351 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013352 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013353
Craig Topper52ea2452012-11-10 09:25:36 +000013354 unsigned NumArgs = MI->getNumOperands();
13355 for (unsigned i = 1; i < NumArgs; ++i) {
13356 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013357 if (!(Op.isReg() && Op.isImplicit()))
13358 MIB.addOperand(Op);
13359 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013360 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013361 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13362
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013363 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013364 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013365 .addReg(X86::XMM0);
13366
Dan Gohman14152b42010-07-06 20:24:04 +000013367 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013368 return BB;
13369}
13370
Craig Topper9c7ae012012-11-10 01:23:36 +000013371// FIXME: Custom handling because TableGen doesn't support multiple implicit
13372// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013373static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13374 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013375 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013376 switch (MI->getOpcode()) {
13377 default: llvm_unreachable("illegal opcode!");
13378 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13379 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13380 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13381 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13382 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13383 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13384 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13385 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013386 }
13387
Craig Topper8aae8dd2012-11-10 08:57:41 +000013388 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013389 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013390
Craig Topper52ea2452012-11-10 09:25:36 +000013391 unsigned NumArgs = MI->getNumOperands(); // remove the results
13392 for (unsigned i = 1; i < NumArgs; ++i) {
13393 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013394 if (!(Op.isReg() && Op.isImplicit()))
13395 MIB.addOperand(Op);
13396 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013397 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013398 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13399
13400 BuildMI(*BB, MI, dl,
13401 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13402 .addReg(X86::ECX);
13403
13404 MI->eraseFromParent();
13405 return BB;
13406}
13407
Craig Topper2da36912012-11-11 22:45:02 +000013408static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13409 const TargetInstrInfo *TII,
13410 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013411 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013412
Eric Christopher228232b2010-11-30 07:20:12 +000013413 // Address into RAX/EAX, other two args into ECX, EDX.
13414 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13415 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13416 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13417 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013418 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013419
Eric Christopher228232b2010-11-30 07:20:12 +000013420 unsigned ValOps = X86::AddrNumOperands;
13421 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13422 .addReg(MI->getOperand(ValOps).getReg());
13423 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13424 .addReg(MI->getOperand(ValOps+1).getReg());
13425
13426 // The instruction doesn't actually take any operands though.
13427 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013428
Eric Christopher228232b2010-11-30 07:20:12 +000013429 MI->eraseFromParent(); // The pseudo is gone now.
13430 return BB;
13431}
13432
13433MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013434X86TargetLowering::EmitVAARG64WithCustomInserter(
13435 MachineInstr *MI,
13436 MachineBasicBlock *MBB) const {
13437 // Emit va_arg instruction on X86-64.
13438
13439 // Operands to this pseudo-instruction:
13440 // 0 ) Output : destination address (reg)
13441 // 1-5) Input : va_list address (addr, i64mem)
13442 // 6 ) ArgSize : Size (in bytes) of vararg type
13443 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13444 // 8 ) Align : Alignment of type
13445 // 9 ) EFLAGS (implicit-def)
13446
13447 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13448 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13449
13450 unsigned DestReg = MI->getOperand(0).getReg();
13451 MachineOperand &Base = MI->getOperand(1);
13452 MachineOperand &Scale = MI->getOperand(2);
13453 MachineOperand &Index = MI->getOperand(3);
13454 MachineOperand &Disp = MI->getOperand(4);
13455 MachineOperand &Segment = MI->getOperand(5);
13456 unsigned ArgSize = MI->getOperand(6).getImm();
13457 unsigned ArgMode = MI->getOperand(7).getImm();
13458 unsigned Align = MI->getOperand(8).getImm();
13459
13460 // Memory Reference
13461 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13462 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13463 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13464
13465 // Machine Information
13466 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13467 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13468 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13469 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13470 DebugLoc DL = MI->getDebugLoc();
13471
13472 // struct va_list {
13473 // i32 gp_offset
13474 // i32 fp_offset
13475 // i64 overflow_area (address)
13476 // i64 reg_save_area (address)
13477 // }
13478 // sizeof(va_list) = 24
13479 // alignment(va_list) = 8
13480
13481 unsigned TotalNumIntRegs = 6;
13482 unsigned TotalNumXMMRegs = 8;
13483 bool UseGPOffset = (ArgMode == 1);
13484 bool UseFPOffset = (ArgMode == 2);
13485 unsigned MaxOffset = TotalNumIntRegs * 8 +
13486 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13487
13488 /* Align ArgSize to a multiple of 8 */
13489 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13490 bool NeedsAlign = (Align > 8);
13491
13492 MachineBasicBlock *thisMBB = MBB;
13493 MachineBasicBlock *overflowMBB;
13494 MachineBasicBlock *offsetMBB;
13495 MachineBasicBlock *endMBB;
13496
13497 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13498 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13499 unsigned OffsetReg = 0;
13500
13501 if (!UseGPOffset && !UseFPOffset) {
13502 // If we only pull from the overflow region, we don't create a branch.
13503 // We don't need to alter control flow.
13504 OffsetDestReg = 0; // unused
13505 OverflowDestReg = DestReg;
13506
13507 offsetMBB = NULL;
13508 overflowMBB = thisMBB;
13509 endMBB = thisMBB;
13510 } else {
13511 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13512 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13513 // If not, pull from overflow_area. (branch to overflowMBB)
13514 //
13515 // thisMBB
13516 // | .
13517 // | .
13518 // offsetMBB overflowMBB
13519 // | .
13520 // | .
13521 // endMBB
13522
13523 // Registers for the PHI in endMBB
13524 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13525 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13526
13527 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13528 MachineFunction *MF = MBB->getParent();
13529 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13530 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13531 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13532
13533 MachineFunction::iterator MBBIter = MBB;
13534 ++MBBIter;
13535
13536 // Insert the new basic blocks
13537 MF->insert(MBBIter, offsetMBB);
13538 MF->insert(MBBIter, overflowMBB);
13539 MF->insert(MBBIter, endMBB);
13540
13541 // Transfer the remainder of MBB and its successor edges to endMBB.
13542 endMBB->splice(endMBB->begin(), thisMBB,
13543 llvm::next(MachineBasicBlock::iterator(MI)),
13544 thisMBB->end());
13545 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13546
13547 // Make offsetMBB and overflowMBB successors of thisMBB
13548 thisMBB->addSuccessor(offsetMBB);
13549 thisMBB->addSuccessor(overflowMBB);
13550
13551 // endMBB is a successor of both offsetMBB and overflowMBB
13552 offsetMBB->addSuccessor(endMBB);
13553 overflowMBB->addSuccessor(endMBB);
13554
13555 // Load the offset value into a register
13556 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13557 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13558 .addOperand(Base)
13559 .addOperand(Scale)
13560 .addOperand(Index)
13561 .addDisp(Disp, UseFPOffset ? 4 : 0)
13562 .addOperand(Segment)
13563 .setMemRefs(MMOBegin, MMOEnd);
13564
13565 // Check if there is enough room left to pull this argument.
13566 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13567 .addReg(OffsetReg)
13568 .addImm(MaxOffset + 8 - ArgSizeA8);
13569
13570 // Branch to "overflowMBB" if offset >= max
13571 // Fall through to "offsetMBB" otherwise
13572 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13573 .addMBB(overflowMBB);
13574 }
13575
13576 // In offsetMBB, emit code to use the reg_save_area.
13577 if (offsetMBB) {
13578 assert(OffsetReg != 0);
13579
13580 // Read the reg_save_area address.
13581 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13582 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13583 .addOperand(Base)
13584 .addOperand(Scale)
13585 .addOperand(Index)
13586 .addDisp(Disp, 16)
13587 .addOperand(Segment)
13588 .setMemRefs(MMOBegin, MMOEnd);
13589
13590 // Zero-extend the offset
13591 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13592 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13593 .addImm(0)
13594 .addReg(OffsetReg)
13595 .addImm(X86::sub_32bit);
13596
13597 // Add the offset to the reg_save_area to get the final address.
13598 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13599 .addReg(OffsetReg64)
13600 .addReg(RegSaveReg);
13601
13602 // Compute the offset for the next argument
13603 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13604 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13605 .addReg(OffsetReg)
13606 .addImm(UseFPOffset ? 16 : 8);
13607
13608 // Store it back into the va_list.
13609 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13610 .addOperand(Base)
13611 .addOperand(Scale)
13612 .addOperand(Index)
13613 .addDisp(Disp, UseFPOffset ? 4 : 0)
13614 .addOperand(Segment)
13615 .addReg(NextOffsetReg)
13616 .setMemRefs(MMOBegin, MMOEnd);
13617
13618 // Jump to endMBB
13619 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13620 .addMBB(endMBB);
13621 }
13622
13623 //
13624 // Emit code to use overflow area
13625 //
13626
13627 // Load the overflow_area address into a register.
13628 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13629 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13630 .addOperand(Base)
13631 .addOperand(Scale)
13632 .addOperand(Index)
13633 .addDisp(Disp, 8)
13634 .addOperand(Segment)
13635 .setMemRefs(MMOBegin, MMOEnd);
13636
13637 // If we need to align it, do so. Otherwise, just copy the address
13638 // to OverflowDestReg.
13639 if (NeedsAlign) {
13640 // Align the overflow address
13641 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13642 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13643
13644 // aligned_addr = (addr + (align-1)) & ~(align-1)
13645 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13646 .addReg(OverflowAddrReg)
13647 .addImm(Align-1);
13648
13649 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13650 .addReg(TmpReg)
13651 .addImm(~(uint64_t)(Align-1));
13652 } else {
13653 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13654 .addReg(OverflowAddrReg);
13655 }
13656
13657 // Compute the next overflow address after this argument.
13658 // (the overflow address should be kept 8-byte aligned)
13659 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13660 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13661 .addReg(OverflowDestReg)
13662 .addImm(ArgSizeA8);
13663
13664 // Store the new overflow address.
13665 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13666 .addOperand(Base)
13667 .addOperand(Scale)
13668 .addOperand(Index)
13669 .addDisp(Disp, 8)
13670 .addOperand(Segment)
13671 .addReg(NextAddrReg)
13672 .setMemRefs(MMOBegin, MMOEnd);
13673
13674 // If we branched, emit the PHI to the front of endMBB.
13675 if (offsetMBB) {
13676 BuildMI(*endMBB, endMBB->begin(), DL,
13677 TII->get(X86::PHI), DestReg)
13678 .addReg(OffsetDestReg).addMBB(offsetMBB)
13679 .addReg(OverflowDestReg).addMBB(overflowMBB);
13680 }
13681
13682 // Erase the pseudo instruction
13683 MI->eraseFromParent();
13684
13685 return endMBB;
13686}
13687
13688MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013689X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13690 MachineInstr *MI,
13691 MachineBasicBlock *MBB) const {
13692 // Emit code to save XMM registers to the stack. The ABI says that the
13693 // number of registers to save is given in %al, so it's theoretically
13694 // possible to do an indirect jump trick to avoid saving all of them,
13695 // however this code takes a simpler approach and just executes all
13696 // of the stores if %al is non-zero. It's less code, and it's probably
13697 // easier on the hardware branch predictor, and stores aren't all that
13698 // expensive anyway.
13699
13700 // Create the new basic blocks. One block contains all the XMM stores,
13701 // and one block is the final destination regardless of whether any
13702 // stores were performed.
13703 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13704 MachineFunction *F = MBB->getParent();
13705 MachineFunction::iterator MBBIter = MBB;
13706 ++MBBIter;
13707 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13708 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13709 F->insert(MBBIter, XMMSaveMBB);
13710 F->insert(MBBIter, EndMBB);
13711
Dan Gohman14152b42010-07-06 20:24:04 +000013712 // Transfer the remainder of MBB and its successor edges to EndMBB.
13713 EndMBB->splice(EndMBB->begin(), MBB,
13714 llvm::next(MachineBasicBlock::iterator(MI)),
13715 MBB->end());
13716 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13717
Dan Gohmand6708ea2009-08-15 01:38:56 +000013718 // The original block will now fall through to the XMM save block.
13719 MBB->addSuccessor(XMMSaveMBB);
13720 // The XMMSaveMBB will fall through to the end block.
13721 XMMSaveMBB->addSuccessor(EndMBB);
13722
13723 // Now add the instructions.
13724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13725 DebugLoc DL = MI->getDebugLoc();
13726
13727 unsigned CountReg = MI->getOperand(0).getReg();
13728 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13729 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13730
13731 if (!Subtarget->isTargetWin64()) {
13732 // If %al is 0, branch around the XMM save block.
13733 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013734 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013735 MBB->addSuccessor(EndMBB);
13736 }
13737
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013738 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013739 // In the XMM save block, save all the XMM argument registers.
13740 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13741 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013742 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013743 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013744 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013745 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013746 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013747 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013748 .addFrameIndex(RegSaveFrameIndex)
13749 .addImm(/*Scale=*/1)
13750 .addReg(/*IndexReg=*/0)
13751 .addImm(/*Disp=*/Offset)
13752 .addReg(/*Segment=*/0)
13753 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013754 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013755 }
13756
Dan Gohman14152b42010-07-06 20:24:04 +000013757 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013758
13759 return EndMBB;
13760}
Mon P Wang63307c32008-05-05 19:05:59 +000013761
Lang Hames6e3f7e42012-02-03 01:13:49 +000013762// The EFLAGS operand of SelectItr might be missing a kill marker
13763// because there were multiple uses of EFLAGS, and ISel didn't know
13764// which to mark. Figure out whether SelectItr should have had a
13765// kill marker, and set it if it should. Returns the correct kill
13766// marker value.
13767static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13768 MachineBasicBlock* BB,
13769 const TargetRegisterInfo* TRI) {
13770 // Scan forward through BB for a use/def of EFLAGS.
13771 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13772 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013773 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013774 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013775 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013776 if (mi.definesRegister(X86::EFLAGS))
13777 break; // Should have kill-flag - update below.
13778 }
13779
13780 // If we hit the end of the block, check whether EFLAGS is live into a
13781 // successor.
13782 if (miI == BB->end()) {
13783 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13784 sEnd = BB->succ_end();
13785 sItr != sEnd; ++sItr) {
13786 MachineBasicBlock* succ = *sItr;
13787 if (succ->isLiveIn(X86::EFLAGS))
13788 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013789 }
13790 }
13791
Lang Hames6e3f7e42012-02-03 01:13:49 +000013792 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13793 // out. SelectMI should have a kill flag on EFLAGS.
13794 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013795 return true;
13796}
13797
Evan Cheng60c07e12006-07-05 22:17:51 +000013798MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013799X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013800 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013801 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13802 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013803
Chris Lattner52600972009-09-02 05:57:00 +000013804 // To "insert" a SELECT_CC instruction, we actually have to insert the
13805 // diamond control-flow pattern. The incoming instruction knows the
13806 // destination vreg to set, the condition code register to branch on, the
13807 // true/false values to select between, and a branch opcode to use.
13808 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13809 MachineFunction::iterator It = BB;
13810 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013811
Chris Lattner52600972009-09-02 05:57:00 +000013812 // thisMBB:
13813 // ...
13814 // TrueVal = ...
13815 // cmpTY ccX, r1, r2
13816 // bCC copy1MBB
13817 // fallthrough --> copy0MBB
13818 MachineBasicBlock *thisMBB = BB;
13819 MachineFunction *F = BB->getParent();
13820 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13821 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013822 F->insert(It, copy0MBB);
13823 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013824
Bill Wendling730c07e2010-06-25 20:48:10 +000013825 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13826 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013827 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13828 if (!MI->killsRegister(X86::EFLAGS) &&
13829 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13830 copy0MBB->addLiveIn(X86::EFLAGS);
13831 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013832 }
13833
Dan Gohman14152b42010-07-06 20:24:04 +000013834 // Transfer the remainder of BB and its successor edges to sinkMBB.
13835 sinkMBB->splice(sinkMBB->begin(), BB,
13836 llvm::next(MachineBasicBlock::iterator(MI)),
13837 BB->end());
13838 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13839
13840 // Add the true and fallthrough blocks as its successors.
13841 BB->addSuccessor(copy0MBB);
13842 BB->addSuccessor(sinkMBB);
13843
13844 // Create the conditional branch instruction.
13845 unsigned Opc =
13846 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13847 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13848
Chris Lattner52600972009-09-02 05:57:00 +000013849 // copy0MBB:
13850 // %FalseValue = ...
13851 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013852 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013853
Chris Lattner52600972009-09-02 05:57:00 +000013854 // sinkMBB:
13855 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13856 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013857 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13858 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013859 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13860 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13861
Dan Gohman14152b42010-07-06 20:24:04 +000013862 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013863 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013864}
13865
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013866MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013867X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13868 bool Is64Bit) const {
13869 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13870 DebugLoc DL = MI->getDebugLoc();
13871 MachineFunction *MF = BB->getParent();
13872 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13873
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013874 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013875
13876 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13877 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13878
13879 // BB:
13880 // ... [Till the alloca]
13881 // If stacklet is not large enough, jump to mallocMBB
13882 //
13883 // bumpMBB:
13884 // Allocate by subtracting from RSP
13885 // Jump to continueMBB
13886 //
13887 // mallocMBB:
13888 // Allocate by call to runtime
13889 //
13890 // continueMBB:
13891 // ...
13892 // [rest of original BB]
13893 //
13894
13895 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13896 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13897 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13898
13899 MachineRegisterInfo &MRI = MF->getRegInfo();
13900 const TargetRegisterClass *AddrRegClass =
13901 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13902
13903 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13904 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13905 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013906 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013907 sizeVReg = MI->getOperand(1).getReg(),
13908 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13909
13910 MachineFunction::iterator MBBIter = BB;
13911 ++MBBIter;
13912
13913 MF->insert(MBBIter, bumpMBB);
13914 MF->insert(MBBIter, mallocMBB);
13915 MF->insert(MBBIter, continueMBB);
13916
13917 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13918 (MachineBasicBlock::iterator(MI)), BB->end());
13919 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13920
13921 // Add code to the main basic block to check if the stack limit has been hit,
13922 // and if so, jump to mallocMBB otherwise to bumpMBB.
13923 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013924 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013925 .addReg(tmpSPVReg).addReg(sizeVReg);
13926 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013927 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013928 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013929 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13930
13931 // bumpMBB simply decreases the stack pointer, since we know the current
13932 // stacklet has enough space.
13933 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013934 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013935 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013936 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013937 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13938
13939 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013940 const uint32_t *RegMask =
13941 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013942 if (Is64Bit) {
13943 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13944 .addReg(sizeVReg);
13945 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013946 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013947 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013948 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013949 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013950 } else {
13951 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13952 .addImm(12);
13953 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13954 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013955 .addExternalSymbol("__morestack_allocate_stack_space")
13956 .addRegMask(RegMask)
13957 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013958 }
13959
13960 if (!Is64Bit)
13961 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13962 .addImm(16);
13963
13964 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13965 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13966 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13967
13968 // Set up the CFG correctly.
13969 BB->addSuccessor(bumpMBB);
13970 BB->addSuccessor(mallocMBB);
13971 mallocMBB->addSuccessor(continueMBB);
13972 bumpMBB->addSuccessor(continueMBB);
13973
13974 // Take care of the PHI nodes.
13975 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13976 MI->getOperand(0).getReg())
13977 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13978 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13979
13980 // Delete the original pseudo instruction.
13981 MI->eraseFromParent();
13982
13983 // And we're done.
13984 return continueMBB;
13985}
13986
13987MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013988X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013989 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013990 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13991 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013992
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013993 assert(!Subtarget->isTargetEnvMacho());
13994
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013995 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13996 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013997
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013998 if (Subtarget->isTargetWin64()) {
13999 if (Subtarget->isTargetCygMing()) {
14000 // ___chkstk(Mingw64):
14001 // Clobbers R10, R11, RAX and EFLAGS.
14002 // Updates RSP.
14003 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14004 .addExternalSymbol("___chkstk")
14005 .addReg(X86::RAX, RegState::Implicit)
14006 .addReg(X86::RSP, RegState::Implicit)
14007 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14008 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14009 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14010 } else {
14011 // __chkstk(MSVCRT): does not update stack pointer.
14012 // Clobbers R10, R11 and EFLAGS.
14013 // FIXME: RAX(allocated size) might be reused and not killed.
14014 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14015 .addExternalSymbol("__chkstk")
14016 .addReg(X86::RAX, RegState::Implicit)
14017 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14018 // RAX has the offset to subtracted from RSP.
14019 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14020 .addReg(X86::RSP)
14021 .addReg(X86::RAX);
14022 }
14023 } else {
14024 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014025 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14026
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014027 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14028 .addExternalSymbol(StackProbeSymbol)
14029 .addReg(X86::EAX, RegState::Implicit)
14030 .addReg(X86::ESP, RegState::Implicit)
14031 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14032 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14033 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14034 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014035
Dan Gohman14152b42010-07-06 20:24:04 +000014036 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014037 return BB;
14038}
Chris Lattner52600972009-09-02 05:57:00 +000014039
14040MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014041X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14042 MachineBasicBlock *BB) const {
14043 // This is pretty easy. We're taking the value that we received from
14044 // our load from the relocation, sticking it in either RDI (x86-64)
14045 // or EAX and doing an indirect call. The return value will then
14046 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014047 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014048 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014049 DebugLoc DL = MI->getDebugLoc();
14050 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014051
14052 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014053 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014054
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014055 // Get a register mask for the lowered call.
14056 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14057 // proper register mask.
14058 const uint32_t *RegMask =
14059 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014060 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014061 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14062 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014063 .addReg(X86::RIP)
14064 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014065 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014066 MI->getOperand(3).getTargetFlags())
14067 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014068 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014069 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014070 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014071 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014072 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14073 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014074 .addReg(0)
14075 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014076 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014077 MI->getOperand(3).getTargetFlags())
14078 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014079 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014080 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014081 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014082 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014083 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14084 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014085 .addReg(TII->getGlobalBaseReg(F))
14086 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014087 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014088 MI->getOperand(3).getTargetFlags())
14089 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014090 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014091 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014092 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014093 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014094
Dan Gohman14152b42010-07-06 20:24:04 +000014095 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014096 return BB;
14097}
14098
14099MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014100X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14101 MachineBasicBlock *MBB) const {
14102 DebugLoc DL = MI->getDebugLoc();
14103 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14104
14105 MachineFunction *MF = MBB->getParent();
14106 MachineRegisterInfo &MRI = MF->getRegInfo();
14107
14108 const BasicBlock *BB = MBB->getBasicBlock();
14109 MachineFunction::iterator I = MBB;
14110 ++I;
14111
14112 // Memory Reference
14113 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14114 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14115
14116 unsigned DstReg;
14117 unsigned MemOpndSlot = 0;
14118
14119 unsigned CurOp = 0;
14120
14121 DstReg = MI->getOperand(CurOp++).getReg();
14122 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14123 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14124 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14125 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14126
14127 MemOpndSlot = CurOp;
14128
14129 MVT PVT = getPointerTy();
14130 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14131 "Invalid Pointer Size!");
14132
14133 // For v = setjmp(buf), we generate
14134 //
14135 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014136 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014137 // SjLjSetup restoreMBB
14138 //
14139 // mainMBB:
14140 // v_main = 0
14141 //
14142 // sinkMBB:
14143 // v = phi(main, restore)
14144 //
14145 // restoreMBB:
14146 // v_restore = 1
14147
14148 MachineBasicBlock *thisMBB = MBB;
14149 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14150 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14151 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14152 MF->insert(I, mainMBB);
14153 MF->insert(I, sinkMBB);
14154 MF->push_back(restoreMBB);
14155
14156 MachineInstrBuilder MIB;
14157
14158 // Transfer the remainder of BB and its successor edges to sinkMBB.
14159 sinkMBB->splice(sinkMBB->begin(), MBB,
14160 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14161 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14162
14163 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014164 unsigned PtrStoreOpc = 0;
14165 unsigned LabelReg = 0;
14166 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14167 Reloc::Model RM = getTargetMachine().getRelocationModel();
14168 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14169 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014170
Michael Liao281ae5a2012-10-17 02:22:27 +000014171 // Prepare IP either in reg or imm.
14172 if (!UseImmLabel) {
14173 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14174 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14175 LabelReg = MRI.createVirtualRegister(PtrRC);
14176 if (Subtarget->is64Bit()) {
14177 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14178 .addReg(X86::RIP)
14179 .addImm(0)
14180 .addReg(0)
14181 .addMBB(restoreMBB)
14182 .addReg(0);
14183 } else {
14184 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14185 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14186 .addReg(XII->getGlobalBaseReg(MF))
14187 .addImm(0)
14188 .addReg(0)
14189 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14190 .addReg(0);
14191 }
14192 } else
14193 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014194 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014195 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014196 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14197 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014198 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014199 else
14200 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14201 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014202 if (!UseImmLabel)
14203 MIB.addReg(LabelReg);
14204 else
14205 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014206 MIB.setMemRefs(MMOBegin, MMOEnd);
14207 // Setup
14208 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14209 .addMBB(restoreMBB);
14210 MIB.addRegMask(RegInfo->getNoPreservedMask());
14211 thisMBB->addSuccessor(mainMBB);
14212 thisMBB->addSuccessor(restoreMBB);
14213
14214 // mainMBB:
14215 // EAX = 0
14216 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14217 mainMBB->addSuccessor(sinkMBB);
14218
14219 // sinkMBB:
14220 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14221 TII->get(X86::PHI), DstReg)
14222 .addReg(mainDstReg).addMBB(mainMBB)
14223 .addReg(restoreDstReg).addMBB(restoreMBB);
14224
14225 // restoreMBB:
14226 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14227 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14228 restoreMBB->addSuccessor(sinkMBB);
14229
14230 MI->eraseFromParent();
14231 return sinkMBB;
14232}
14233
14234MachineBasicBlock *
14235X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14236 MachineBasicBlock *MBB) const {
14237 DebugLoc DL = MI->getDebugLoc();
14238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14239
14240 MachineFunction *MF = MBB->getParent();
14241 MachineRegisterInfo &MRI = MF->getRegInfo();
14242
14243 // Memory Reference
14244 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14245 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14246
14247 MVT PVT = getPointerTy();
14248 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14249 "Invalid Pointer Size!");
14250
14251 const TargetRegisterClass *RC =
14252 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14253 unsigned Tmp = MRI.createVirtualRegister(RC);
14254 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14255 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14256 unsigned SP = RegInfo->getStackRegister();
14257
14258 MachineInstrBuilder MIB;
14259
Michael Liao281ae5a2012-10-17 02:22:27 +000014260 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14261 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014262
14263 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14264 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14265
14266 // Reload FP
14267 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14268 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14269 MIB.addOperand(MI->getOperand(i));
14270 MIB.setMemRefs(MMOBegin, MMOEnd);
14271 // Reload IP
14272 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14273 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14274 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014275 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014276 else
14277 MIB.addOperand(MI->getOperand(i));
14278 }
14279 MIB.setMemRefs(MMOBegin, MMOEnd);
14280 // Reload SP
14281 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14282 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14283 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014284 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014285 else
14286 MIB.addOperand(MI->getOperand(i));
14287 }
14288 MIB.setMemRefs(MMOBegin, MMOEnd);
14289 // Jump
14290 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14291
14292 MI->eraseFromParent();
14293 return MBB;
14294}
14295
14296MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014297X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014298 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014299 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014300 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014301 case X86::TAILJMPd64:
14302 case X86::TAILJMPr64:
14303 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014304 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014305 case X86::TCRETURNdi64:
14306 case X86::TCRETURNri64:
14307 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014308 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014309 case X86::WIN_ALLOCA:
14310 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014311 case X86::SEG_ALLOCA_32:
14312 return EmitLoweredSegAlloca(MI, BB, false);
14313 case X86::SEG_ALLOCA_64:
14314 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014315 case X86::TLSCall_32:
14316 case X86::TLSCall_64:
14317 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014318 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014319 case X86::CMOV_FR32:
14320 case X86::CMOV_FR64:
14321 case X86::CMOV_V4F32:
14322 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014323 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014324 case X86::CMOV_V8F32:
14325 case X86::CMOV_V4F64:
14326 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014327 case X86::CMOV_GR16:
14328 case X86::CMOV_GR32:
14329 case X86::CMOV_RFP32:
14330 case X86::CMOV_RFP64:
14331 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014332 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014333
Dale Johannesen849f2142007-07-03 00:53:03 +000014334 case X86::FP32_TO_INT16_IN_MEM:
14335 case X86::FP32_TO_INT32_IN_MEM:
14336 case X86::FP32_TO_INT64_IN_MEM:
14337 case X86::FP64_TO_INT16_IN_MEM:
14338 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014339 case X86::FP64_TO_INT64_IN_MEM:
14340 case X86::FP80_TO_INT16_IN_MEM:
14341 case X86::FP80_TO_INT32_IN_MEM:
14342 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014343 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14344 DebugLoc DL = MI->getDebugLoc();
14345
Evan Cheng60c07e12006-07-05 22:17:51 +000014346 // Change the floating point control register to use "round towards zero"
14347 // mode when truncating to an integer value.
14348 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014349 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014350 addFrameReference(BuildMI(*BB, MI, DL,
14351 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014352
14353 // Load the old value of the high byte of the control word...
14354 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014355 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014356 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014357 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014358
14359 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014360 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014361 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014362
14363 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014364 addFrameReference(BuildMI(*BB, MI, DL,
14365 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014366
14367 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014368 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014369 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014370
14371 // Get the X86 opcode to use.
14372 unsigned Opc;
14373 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014374 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014375 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14376 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14377 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14378 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14379 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14380 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014381 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14382 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14383 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014384 }
14385
14386 X86AddressMode AM;
14387 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014388 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014389 AM.BaseType = X86AddressMode::RegBase;
14390 AM.Base.Reg = Op.getReg();
14391 } else {
14392 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014393 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014394 }
14395 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014396 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014397 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014398 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014399 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014400 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014401 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014402 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014403 AM.GV = Op.getGlobal();
14404 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014405 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014406 }
Dan Gohman14152b42010-07-06 20:24:04 +000014407 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014408 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014409
14410 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014411 addFrameReference(BuildMI(*BB, MI, DL,
14412 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014413
Dan Gohman14152b42010-07-06 20:24:04 +000014414 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014415 return BB;
14416 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014417 // String/text processing lowering.
14418 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014419 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014420 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014421 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014422 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014423 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014424 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014425 case X86::VPCMPESTRM128MEM:
14426 assert(Subtarget->hasSSE42() &&
14427 "Target must have SSE4.2 or AVX features enabled");
14428 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014429
14430 // String/text processing lowering.
14431 case X86::PCMPISTRIREG:
14432 case X86::VPCMPISTRIREG:
14433 case X86::PCMPISTRIMEM:
14434 case X86::VPCMPISTRIMEM:
14435 case X86::PCMPESTRIREG:
14436 case X86::VPCMPESTRIREG:
14437 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014438 case X86::VPCMPESTRIMEM:
14439 assert(Subtarget->hasSSE42() &&
14440 "Target must have SSE4.2 or AVX features enabled");
14441 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014442
Craig Topper8aae8dd2012-11-10 08:57:41 +000014443 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014444 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014445 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014446
Michael Liaobe02a902012-11-08 07:28:54 +000014447 // xbegin
14448 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014449 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014450
Craig Topper8aae8dd2012-11-10 08:57:41 +000014451 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014452 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014453 case X86::ATOMAND16:
14454 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014455 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014456 // Fall through
14457 case X86::ATOMOR8:
14458 case X86::ATOMOR16:
14459 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014460 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014461 // Fall through
14462 case X86::ATOMXOR16:
14463 case X86::ATOMXOR8:
14464 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014465 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014466 // Fall through
14467 case X86::ATOMNAND8:
14468 case X86::ATOMNAND16:
14469 case X86::ATOMNAND32:
14470 case X86::ATOMNAND64:
14471 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014472 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014473 case X86::ATOMMAX16:
14474 case X86::ATOMMAX32:
14475 case X86::ATOMMAX64:
14476 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014477 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014478 case X86::ATOMMIN16:
14479 case X86::ATOMMIN32:
14480 case X86::ATOMMIN64:
14481 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014482 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014483 case X86::ATOMUMAX16:
14484 case X86::ATOMUMAX32:
14485 case X86::ATOMUMAX64:
14486 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014487 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014488 case X86::ATOMUMIN16:
14489 case X86::ATOMUMIN32:
14490 case X86::ATOMUMIN64:
14491 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014492
14493 // This group does 64-bit operations on a 32-bit host.
14494 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014495 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014496 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014497 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014498 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014499 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014500 case X86::ATOMMAX6432:
14501 case X86::ATOMMIN6432:
14502 case X86::ATOMUMAX6432:
14503 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014504 case X86::ATOMSWAP6432:
14505 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014506
Dan Gohmand6708ea2009-08-15 01:38:56 +000014507 case X86::VASTART_SAVE_XMM_REGS:
14508 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014509
14510 case X86::VAARG_64:
14511 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014512
14513 case X86::EH_SjLj_SetJmp32:
14514 case X86::EH_SjLj_SetJmp64:
14515 return emitEHSjLjSetJmp(MI, BB);
14516
14517 case X86::EH_SjLj_LongJmp32:
14518 case X86::EH_SjLj_LongJmp64:
14519 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014520 }
14521}
14522
14523//===----------------------------------------------------------------------===//
14524// X86 Optimization Hooks
14525//===----------------------------------------------------------------------===//
14526
Dan Gohman475871a2008-07-27 21:46:04 +000014527void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014528 APInt &KnownZero,
14529 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014530 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014531 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014532 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014533 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014534 assert((Opc >= ISD::BUILTIN_OP_END ||
14535 Opc == ISD::INTRINSIC_WO_CHAIN ||
14536 Opc == ISD::INTRINSIC_W_CHAIN ||
14537 Opc == ISD::INTRINSIC_VOID) &&
14538 "Should use MaskedValueIsZero if you don't know whether Op"
14539 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014540
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014541 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014542 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014543 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014544 case X86ISD::ADD:
14545 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014546 case X86ISD::ADC:
14547 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014548 case X86ISD::SMUL:
14549 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014550 case X86ISD::INC:
14551 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014552 case X86ISD::OR:
14553 case X86ISD::XOR:
14554 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014555 // These nodes' second result is a boolean.
14556 if (Op.getResNo() == 0)
14557 break;
14558 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014559 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014560 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014561 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014562 case ISD::INTRINSIC_WO_CHAIN: {
14563 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14564 unsigned NumLoBits = 0;
14565 switch (IntId) {
14566 default: break;
14567 case Intrinsic::x86_sse_movmsk_ps:
14568 case Intrinsic::x86_avx_movmsk_ps_256:
14569 case Intrinsic::x86_sse2_movmsk_pd:
14570 case Intrinsic::x86_avx_movmsk_pd_256:
14571 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014572 case Intrinsic::x86_sse2_pmovmskb_128:
14573 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014574 // High bits of movmskp{s|d}, pmovmskb are known zero.
14575 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014576 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014577 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14578 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14579 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14580 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14581 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14582 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014583 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014584 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014585 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014586 break;
14587 }
14588 }
14589 break;
14590 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014591 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014592}
Chris Lattner259e97c2006-01-31 19:43:35 +000014593
Owen Andersonbc146b02010-09-21 20:42:50 +000014594unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14595 unsigned Depth) const {
14596 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14597 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14598 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014599
Owen Andersonbc146b02010-09-21 20:42:50 +000014600 // Fallback case.
14601 return 1;
14602}
14603
Evan Cheng206ee9d2006-07-07 08:33:52 +000014604/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014605/// node is a GlobalAddress + offset.
14606bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014607 const GlobalValue* &GA,
14608 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014609 if (N->getOpcode() == X86ISD::Wrapper) {
14610 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014611 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014612 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014613 return true;
14614 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014615 }
Evan Chengad4196b2008-05-12 19:56:52 +000014616 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014617}
14618
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014619/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14620/// same as extracting the high 128-bit part of 256-bit vector and then
14621/// inserting the result into the low part of a new 256-bit vector
14622static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14623 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014624 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014625
14626 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014627 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014628 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14629 SVOp->getMaskElt(j) >= 0)
14630 return false;
14631
14632 return true;
14633}
14634
14635/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14636/// same as extracting the low 128-bit part of 256-bit vector and then
14637/// inserting the result into the high part of a new 256-bit vector
14638static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14639 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014640 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014641
14642 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014643 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014644 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14645 SVOp->getMaskElt(j) >= 0)
14646 return false;
14647
14648 return true;
14649}
14650
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014651/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14652static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014653 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014654 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014655 DebugLoc dl = N->getDebugLoc();
14656 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14657 SDValue V1 = SVOp->getOperand(0);
14658 SDValue V2 = SVOp->getOperand(1);
14659 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014660 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014661
14662 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14663 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14664 //
14665 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014666 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014667 // V UNDEF BUILD_VECTOR UNDEF
14668 // \ / \ /
14669 // CONCAT_VECTOR CONCAT_VECTOR
14670 // \ /
14671 // \ /
14672 // RESULT: V + zero extended
14673 //
14674 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14675 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14676 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14677 return SDValue();
14678
14679 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14680 return SDValue();
14681
14682 // To match the shuffle mask, the first half of the mask should
14683 // be exactly the first vector, and all the rest a splat with the
14684 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014685 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014686 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14687 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14688 return SDValue();
14689
Chad Rosier3d1161e2012-01-03 21:05:52 +000014690 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14691 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014692 if (Ld->hasNUsesOfValue(1, 0)) {
14693 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14694 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14695 SDValue ResNode =
14696 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14697 Ld->getMemoryVT(),
14698 Ld->getPointerInfo(),
14699 Ld->getAlignment(),
14700 false/*isVolatile*/, true/*ReadMem*/,
14701 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014702
14703 // Make sure the newly-created LOAD is in the same position as Ld in
14704 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14705 // and update uses of Ld's output chain to use the TokenFactor.
14706 if (Ld->hasAnyUseOfValue(1)) {
14707 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14708 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14709 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14710 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14711 SDValue(ResNode.getNode(), 1));
14712 }
14713
Chad Rosier42726832012-05-07 18:47:44 +000014714 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14715 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014716 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014717
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014718 // Emit a zeroed vector and insert the desired subvector on its
14719 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014720 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014721 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014722 return DCI.CombineTo(N, InsV);
14723 }
14724
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014725 //===--------------------------------------------------------------------===//
14726 // Combine some shuffles into subvector extracts and inserts:
14727 //
14728
14729 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14730 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014731 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14732 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014733 return DCI.CombineTo(N, InsV);
14734 }
14735
14736 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14737 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014738 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14739 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014740 return DCI.CombineTo(N, InsV);
14741 }
14742
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014743 return SDValue();
14744}
14745
14746/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014747static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014748 TargetLowering::DAGCombinerInfo &DCI,
14749 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014750 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014751 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014752
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014753 // Don't create instructions with illegal types after legalize types has run.
14754 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14755 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14756 return SDValue();
14757
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014758 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014759 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014760 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014761 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014762
14763 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014764 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014765 return SDValue();
14766
14767 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14768 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14769 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014770 SmallVector<SDValue, 16> Elts;
14771 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014772 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014773
Nate Begemanfdea31a2010-03-24 20:49:50 +000014774 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014775}
Evan Chengd880b972008-05-09 21:53:03 +000014776
Nadav Roteme12bf182013-01-04 17:35:21 +000014777/// PerformTruncateCombine - Converts truncate operation to
14778/// a sequence of vector shuffle operations.
14779/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014780static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14781 TargetLowering::DAGCombinerInfo &DCI,
14782 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014783 return SDValue();
14784}
14785
Craig Topper89f4e662012-03-20 07:17:59 +000014786/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14787/// specific shuffle of a load can be folded into a single element load.
14788/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14789/// shuffles have been customed lowered so we need to handle those here.
14790static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14791 TargetLowering::DAGCombinerInfo &DCI) {
14792 if (DCI.isBeforeLegalizeOps())
14793 return SDValue();
14794
14795 SDValue InVec = N->getOperand(0);
14796 SDValue EltNo = N->getOperand(1);
14797
14798 if (!isa<ConstantSDNode>(EltNo))
14799 return SDValue();
14800
14801 EVT VT = InVec.getValueType();
14802
14803 bool HasShuffleIntoBitcast = false;
14804 if (InVec.getOpcode() == ISD::BITCAST) {
14805 // Don't duplicate a load with other uses.
14806 if (!InVec.hasOneUse())
14807 return SDValue();
14808 EVT BCVT = InVec.getOperand(0).getValueType();
14809 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14810 return SDValue();
14811 InVec = InVec.getOperand(0);
14812 HasShuffleIntoBitcast = true;
14813 }
14814
14815 if (!isTargetShuffle(InVec.getOpcode()))
14816 return SDValue();
14817
14818 // Don't duplicate a load with other uses.
14819 if (!InVec.hasOneUse())
14820 return SDValue();
14821
14822 SmallVector<int, 16> ShuffleMask;
14823 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014824 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14825 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014826 return SDValue();
14827
14828 // Select the input vector, guarding against out of range extract vector.
14829 unsigned NumElems = VT.getVectorNumElements();
14830 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14831 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14832 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14833 : InVec.getOperand(1);
14834
14835 // If inputs to shuffle are the same for both ops, then allow 2 uses
14836 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14837
14838 if (LdNode.getOpcode() == ISD::BITCAST) {
14839 // Don't duplicate a load with other uses.
14840 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14841 return SDValue();
14842
14843 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14844 LdNode = LdNode.getOperand(0);
14845 }
14846
14847 if (!ISD::isNormalLoad(LdNode.getNode()))
14848 return SDValue();
14849
14850 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14851
14852 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14853 return SDValue();
14854
14855 if (HasShuffleIntoBitcast) {
14856 // If there's a bitcast before the shuffle, check if the load type and
14857 // alignment is valid.
14858 unsigned Align = LN0->getAlignment();
14859 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014860 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014861 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14862
14863 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14864 return SDValue();
14865 }
14866
14867 // All checks match so transform back to vector_shuffle so that DAG combiner
14868 // can finish the job
14869 DebugLoc dl = N->getDebugLoc();
14870
14871 // Create shuffle node taking into account the case that its a unary shuffle
14872 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14873 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14874 InVec.getOperand(0), Shuffle,
14875 &ShuffleMask[0]);
14876 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14877 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14878 EltNo);
14879}
14880
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014881/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14882/// generation and convert it from being a bunch of shuffles and extracts
14883/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014884static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014885 TargetLowering::DAGCombinerInfo &DCI) {
14886 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14887 if (NewOp.getNode())
14888 return NewOp;
14889
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014890 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014891 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14892 // from mmx to v2i32 has a single usage.
14893 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14894 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14895 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14896 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14897 N->getValueType(0),
14898 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014899
14900 // Only operate on vectors of 4 elements, where the alternative shuffling
14901 // gets to be more expensive.
14902 if (InputVector.getValueType() != MVT::v4i32)
14903 return SDValue();
14904
14905 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14906 // single use which is a sign-extend or zero-extend, and all elements are
14907 // used.
14908 SmallVector<SDNode *, 4> Uses;
14909 unsigned ExtractedElements = 0;
14910 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14911 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14912 if (UI.getUse().getResNo() != InputVector.getResNo())
14913 return SDValue();
14914
14915 SDNode *Extract = *UI;
14916 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14917 return SDValue();
14918
14919 if (Extract->getValueType(0) != MVT::i32)
14920 return SDValue();
14921 if (!Extract->hasOneUse())
14922 return SDValue();
14923 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14924 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14925 return SDValue();
14926 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14927 return SDValue();
14928
14929 // Record which element was extracted.
14930 ExtractedElements |=
14931 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14932
14933 Uses.push_back(Extract);
14934 }
14935
14936 // If not all the elements were used, this may not be worthwhile.
14937 if (ExtractedElements != 15)
14938 return SDValue();
14939
14940 // Ok, we've now decided to do the transformation.
14941 DebugLoc dl = InputVector.getDebugLoc();
14942
14943 // Store the value to a temporary stack slot.
14944 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014945 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14946 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014947
14948 // Replace each use (extract) with a load of the appropriate element.
14949 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14950 UE = Uses.end(); UI != UE; ++UI) {
14951 SDNode *Extract = *UI;
14952
Nadav Rotem86694292011-05-17 08:31:57 +000014953 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014954 SDValue Idx = Extract->getOperand(1);
14955 unsigned EltSize =
14956 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14957 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014958 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014959 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14960
Nadav Rotem86694292011-05-17 08:31:57 +000014961 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014962 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014963
14964 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014965 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014966 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014967 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014968
14969 // Replace the exact with the load.
14970 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14971 }
14972
14973 // The replacement was made in place; don't return anything.
14974 return SDValue();
14975}
14976
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014977/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14978static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14979 SDValue RHS, SelectionDAG &DAG,
14980 const X86Subtarget *Subtarget) {
14981 if (!VT.isVector())
14982 return 0;
14983
14984 switch (VT.getSimpleVT().SimpleTy) {
14985 default: return 0;
14986 case MVT::v32i8:
14987 case MVT::v16i16:
14988 case MVT::v8i32:
14989 if (!Subtarget->hasAVX2())
14990 return 0;
14991 case MVT::v16i8:
14992 case MVT::v8i16:
14993 case MVT::v4i32:
14994 if (!Subtarget->hasSSE2())
14995 return 0;
14996 }
14997
14998 // SSE2 has only a small subset of the operations.
14999 bool hasUnsigned = Subtarget->hasSSE41() ||
15000 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15001 bool hasSigned = Subtarget->hasSSE41() ||
15002 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15003
15004 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15005
15006 // Check for x CC y ? x : y.
15007 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15008 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15009 switch (CC) {
15010 default: break;
15011 case ISD::SETULT:
15012 case ISD::SETULE:
15013 return hasUnsigned ? X86ISD::UMIN : 0;
15014 case ISD::SETUGT:
15015 case ISD::SETUGE:
15016 return hasUnsigned ? X86ISD::UMAX : 0;
15017 case ISD::SETLT:
15018 case ISD::SETLE:
15019 return hasSigned ? X86ISD::SMIN : 0;
15020 case ISD::SETGT:
15021 case ISD::SETGE:
15022 return hasSigned ? X86ISD::SMAX : 0;
15023 }
15024 // Check for x CC y ? y : x -- a min/max with reversed arms.
15025 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15026 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15027 switch (CC) {
15028 default: break;
15029 case ISD::SETULT:
15030 case ISD::SETULE:
15031 return hasUnsigned ? X86ISD::UMAX : 0;
15032 case ISD::SETUGT:
15033 case ISD::SETUGE:
15034 return hasUnsigned ? X86ISD::UMIN : 0;
15035 case ISD::SETLT:
15036 case ISD::SETLE:
15037 return hasSigned ? X86ISD::SMAX : 0;
15038 case ISD::SETGT:
15039 case ISD::SETGE:
15040 return hasSigned ? X86ISD::SMIN : 0;
15041 }
15042 }
15043
15044 return 0;
15045}
15046
Duncan Sands6bcd2192011-09-17 16:49:39 +000015047/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15048/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015049static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015050 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015051 const X86Subtarget *Subtarget) {
15052 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015053 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015054 // Get the LHS/RHS of the select.
15055 SDValue LHS = N->getOperand(1);
15056 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015057 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015058
Dan Gohman670e5392009-09-21 18:03:22 +000015059 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015060 // instructions match the semantics of the common C idiom x<y?x:y but not
15061 // x<=y?x:y, because of how they handle negative zero (which can be
15062 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015063 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15064 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015065 (Subtarget->hasSSE2() ||
15066 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015067 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015068
Chris Lattner47b4ce82009-03-11 05:48:52 +000015069 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015070 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015071 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15072 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015073 switch (CC) {
15074 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015075 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015076 // Converting this to a min would handle NaNs incorrectly, and swapping
15077 // the operands would cause it to handle comparisons between positive
15078 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015079 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015080 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015081 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15082 break;
15083 std::swap(LHS, RHS);
15084 }
Dan Gohman670e5392009-09-21 18:03:22 +000015085 Opcode = X86ISD::FMIN;
15086 break;
15087 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015088 // Converting this to a min would handle comparisons between positive
15089 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015090 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015091 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15092 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015093 Opcode = X86ISD::FMIN;
15094 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015095 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015096 // Converting this to a min would handle both negative zeros and NaNs
15097 // incorrectly, but we can swap the operands to fix both.
15098 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015099 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015100 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015101 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015102 Opcode = X86ISD::FMIN;
15103 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015104
Dan Gohman670e5392009-09-21 18:03:22 +000015105 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015106 // Converting this to a max would handle comparisons between positive
15107 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015108 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015109 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015110 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015111 Opcode = X86ISD::FMAX;
15112 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015113 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015114 // Converting this to a max would handle NaNs incorrectly, and swapping
15115 // the operands would cause it to handle comparisons between positive
15116 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015117 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015118 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015119 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15120 break;
15121 std::swap(LHS, RHS);
15122 }
Dan Gohman670e5392009-09-21 18:03:22 +000015123 Opcode = X86ISD::FMAX;
15124 break;
15125 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015126 // Converting this to a max would handle both negative zeros and NaNs
15127 // incorrectly, but we can swap the operands to fix both.
15128 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015129 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015130 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015131 case ISD::SETGE:
15132 Opcode = X86ISD::FMAX;
15133 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015134 }
Dan Gohman670e5392009-09-21 18:03:22 +000015135 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015136 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15137 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015138 switch (CC) {
15139 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015140 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015141 // Converting this to a min would handle comparisons between positive
15142 // and negative zero incorrectly, and swapping the operands would
15143 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015144 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015145 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015146 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015147 break;
15148 std::swap(LHS, RHS);
15149 }
Dan Gohman670e5392009-09-21 18:03:22 +000015150 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015151 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015152 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015153 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015154 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015155 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15156 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015157 Opcode = X86ISD::FMIN;
15158 break;
15159 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015160 // Converting this to a min would handle both negative zeros and NaNs
15161 // incorrectly, but we can swap the operands to fix both.
15162 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015163 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015164 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015165 case ISD::SETGE:
15166 Opcode = X86ISD::FMIN;
15167 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015168
Dan Gohman670e5392009-09-21 18:03:22 +000015169 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015170 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015171 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015172 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015173 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015174 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015175 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015176 // Converting this to a max would handle comparisons between positive
15177 // and negative zero incorrectly, and swapping the operands would
15178 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015179 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015180 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015181 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015182 break;
15183 std::swap(LHS, RHS);
15184 }
Dan Gohman670e5392009-09-21 18:03:22 +000015185 Opcode = X86ISD::FMAX;
15186 break;
15187 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015188 // Converting this to a max would handle both negative zeros and NaNs
15189 // incorrectly, but we can swap the operands to fix both.
15190 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015191 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015192 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015193 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015194 Opcode = X86ISD::FMAX;
15195 break;
15196 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015197 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015198
Chris Lattner47b4ce82009-03-11 05:48:52 +000015199 if (Opcode)
15200 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015201 }
Eric Christopherfd179292009-08-27 18:07:15 +000015202
Chris Lattnerd1980a52009-03-12 06:52:53 +000015203 // If this is a select between two integer constants, try to do some
15204 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015205 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15206 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015207 // Don't do this for crazy integer types.
15208 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15209 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015210 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015211 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015212
Chris Lattnercee56e72009-03-13 05:53:31 +000015213 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015214 // Efficiently invertible.
15215 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15216 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15217 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15218 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015219 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015220 }
Eric Christopherfd179292009-08-27 18:07:15 +000015221
Chris Lattnerd1980a52009-03-12 06:52:53 +000015222 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015223 if (FalseC->getAPIntValue() == 0 &&
15224 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015225 if (NeedsCondInvert) // Invert the condition if needed.
15226 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15227 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015228
Chris Lattnerd1980a52009-03-12 06:52:53 +000015229 // Zero extend the condition if needed.
15230 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015231
Chris Lattnercee56e72009-03-13 05:53:31 +000015232 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015233 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015234 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015235 }
Eric Christopherfd179292009-08-27 18:07:15 +000015236
Chris Lattner97a29a52009-03-13 05:22:11 +000015237 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015238 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015239 if (NeedsCondInvert) // Invert the condition if needed.
15240 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15241 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015242
Chris Lattner97a29a52009-03-13 05:22:11 +000015243 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015244 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15245 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015246 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015247 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015248 }
Eric Christopherfd179292009-08-27 18:07:15 +000015249
Chris Lattnercee56e72009-03-13 05:53:31 +000015250 // Optimize cases that will turn into an LEA instruction. This requires
15251 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015252 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015253 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015254 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015255
Chris Lattnercee56e72009-03-13 05:53:31 +000015256 bool isFastMultiplier = false;
15257 if (Diff < 10) {
15258 switch ((unsigned char)Diff) {
15259 default: break;
15260 case 1: // result = add base, cond
15261 case 2: // result = lea base( , cond*2)
15262 case 3: // result = lea base(cond, cond*2)
15263 case 4: // result = lea base( , cond*4)
15264 case 5: // result = lea base(cond, cond*4)
15265 case 8: // result = lea base( , cond*8)
15266 case 9: // result = lea base(cond, cond*8)
15267 isFastMultiplier = true;
15268 break;
15269 }
15270 }
Eric Christopherfd179292009-08-27 18:07:15 +000015271
Chris Lattnercee56e72009-03-13 05:53:31 +000015272 if (isFastMultiplier) {
15273 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15274 if (NeedsCondInvert) // Invert the condition if needed.
15275 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15276 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015277
Chris Lattnercee56e72009-03-13 05:53:31 +000015278 // Zero extend the condition if needed.
15279 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15280 Cond);
15281 // Scale the condition by the difference.
15282 if (Diff != 1)
15283 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15284 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015285
Chris Lattnercee56e72009-03-13 05:53:31 +000015286 // Add the base if non-zero.
15287 if (FalseC->getAPIntValue() != 0)
15288 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15289 SDValue(FalseC, 0));
15290 return Cond;
15291 }
Eric Christopherfd179292009-08-27 18:07:15 +000015292 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015293 }
15294 }
Eric Christopherfd179292009-08-27 18:07:15 +000015295
Evan Cheng56f582d2012-01-04 01:41:39 +000015296 // Canonicalize max and min:
15297 // (x > y) ? x : y -> (x >= y) ? x : y
15298 // (x < y) ? x : y -> (x <= y) ? x : y
15299 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15300 // the need for an extra compare
15301 // against zero. e.g.
15302 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15303 // subl %esi, %edi
15304 // testl %edi, %edi
15305 // movl $0, %eax
15306 // cmovgl %edi, %eax
15307 // =>
15308 // xorl %eax, %eax
15309 // subl %esi, $edi
15310 // cmovsl %eax, %edi
15311 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15312 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15313 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15314 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15315 switch (CC) {
15316 default: break;
15317 case ISD::SETLT:
15318 case ISD::SETGT: {
15319 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15320 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15321 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15322 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15323 }
15324 }
15325 }
15326
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015327 // Match VSELECTs into subs with unsigned saturation.
15328 if (!DCI.isBeforeLegalize() &&
15329 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15330 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15331 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15332 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15333 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15334
15335 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15336 // left side invert the predicate to simplify logic below.
15337 SDValue Other;
15338 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15339 Other = RHS;
15340 CC = ISD::getSetCCInverse(CC, true);
15341 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15342 Other = LHS;
15343 }
15344
15345 if (Other.getNode() && Other->getNumOperands() == 2 &&
15346 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15347 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15348 SDValue CondRHS = Cond->getOperand(1);
15349
15350 // Look for a general sub with unsigned saturation first.
15351 // x >= y ? x-y : 0 --> subus x, y
15352 // x > y ? x-y : 0 --> subus x, y
15353 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15354 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15355 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15356
15357 // If the RHS is a constant we have to reverse the const canonicalization.
15358 // x > C-1 ? x+-C : 0 --> subus x, C
15359 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15360 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15361 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015362 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015363 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015364 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015365 }
15366
15367 // Another special case: If C was a sign bit, the sub has been
15368 // canonicalized into a xor.
15369 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15370 // it's safe to decanonicalize the xor?
15371 // x s< 0 ? x^C : 0 --> subus x, C
15372 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15373 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15374 isSplatVector(OpRHS.getNode())) {
15375 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15376 if (A.isSignBit())
15377 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15378 }
15379 }
15380 }
15381
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015382 // Try to match a min/max vector operation.
15383 if (!DCI.isBeforeLegalize() &&
15384 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15385 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15386 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15387
Nadav Rotemcc616562012-01-15 19:27:55 +000015388 // If we know that this node is legal then we know that it is going to be
15389 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15390 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15391 // to simplify previous instructions.
15392 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15393 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015394 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015395 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015396
15397 // Don't optimize vector selects that map to mask-registers.
15398 if (BitWidth == 1)
15399 return SDValue();
15400
Nadav Rotemcc616562012-01-15 19:27:55 +000015401 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15402 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15403
15404 APInt KnownZero, KnownOne;
15405 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15406 DCI.isBeforeLegalizeOps());
15407 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15408 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15409 DCI.CommitTargetLoweringOpt(TLO);
15410 }
15411
Dan Gohman475871a2008-07-27 21:46:04 +000015412 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015413}
15414
Michael Liao2a33cec2012-08-10 19:58:13 +000015415// Check whether a boolean test is testing a boolean value generated by
15416// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15417// code.
15418//
15419// Simplify the following patterns:
15420// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15421// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15422// to (Op EFLAGS Cond)
15423//
15424// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15425// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15426// to (Op EFLAGS !Cond)
15427//
15428// where Op could be BRCOND or CMOV.
15429//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015430static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015431 // Quit if not CMP and SUB with its value result used.
15432 if (Cmp.getOpcode() != X86ISD::CMP &&
15433 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15434 return SDValue();
15435
15436 // Quit if not used as a boolean value.
15437 if (CC != X86::COND_E && CC != X86::COND_NE)
15438 return SDValue();
15439
15440 // Check CMP operands. One of them should be 0 or 1 and the other should be
15441 // an SetCC or extended from it.
15442 SDValue Op1 = Cmp.getOperand(0);
15443 SDValue Op2 = Cmp.getOperand(1);
15444
15445 SDValue SetCC;
15446 const ConstantSDNode* C = 0;
15447 bool needOppositeCond = (CC == X86::COND_E);
15448
15449 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15450 SetCC = Op2;
15451 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15452 SetCC = Op1;
15453 else // Quit if all operands are not constants.
15454 return SDValue();
15455
15456 if (C->getZExtValue() == 1)
15457 needOppositeCond = !needOppositeCond;
15458 else if (C->getZExtValue() != 0)
15459 // Quit if the constant is neither 0 or 1.
15460 return SDValue();
15461
15462 // Skip 'zext' node.
15463 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15464 SetCC = SetCC.getOperand(0);
15465
Michael Liao7fdc66b2012-09-10 16:36:16 +000015466 switch (SetCC.getOpcode()) {
15467 case X86ISD::SETCC:
15468 // Set the condition code or opposite one if necessary.
15469 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15470 if (needOppositeCond)
15471 CC = X86::GetOppositeBranchCondition(CC);
15472 return SetCC.getOperand(1);
15473 case X86ISD::CMOV: {
15474 // Check whether false/true value has canonical one, i.e. 0 or 1.
15475 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15476 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15477 // Quit if true value is not a constant.
15478 if (!TVal)
15479 return SDValue();
15480 // Quit if false value is not a constant.
15481 if (!FVal) {
15482 // A special case for rdrand, where 0 is set if false cond is found.
15483 SDValue Op = SetCC.getOperand(0);
15484 if (Op.getOpcode() != X86ISD::RDRAND)
15485 return SDValue();
15486 }
15487 // Quit if false value is not the constant 0 or 1.
15488 bool FValIsFalse = true;
15489 if (FVal && FVal->getZExtValue() != 0) {
15490 if (FVal->getZExtValue() != 1)
15491 return SDValue();
15492 // If FVal is 1, opposite cond is needed.
15493 needOppositeCond = !needOppositeCond;
15494 FValIsFalse = false;
15495 }
15496 // Quit if TVal is not the constant opposite of FVal.
15497 if (FValIsFalse && TVal->getZExtValue() != 1)
15498 return SDValue();
15499 if (!FValIsFalse && TVal->getZExtValue() != 0)
15500 return SDValue();
15501 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15502 if (needOppositeCond)
15503 CC = X86::GetOppositeBranchCondition(CC);
15504 return SetCC.getOperand(3);
15505 }
15506 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015507
Michael Liao7fdc66b2012-09-10 16:36:16 +000015508 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015509}
15510
Chris Lattnerd1980a52009-03-12 06:52:53 +000015511/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15512static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015513 TargetLowering::DAGCombinerInfo &DCI,
15514 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015515 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015516
Chris Lattnerd1980a52009-03-12 06:52:53 +000015517 // If the flag operand isn't dead, don't touch this CMOV.
15518 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15519 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015520
Evan Chengb5a55d92011-05-24 01:48:22 +000015521 SDValue FalseOp = N->getOperand(0);
15522 SDValue TrueOp = N->getOperand(1);
15523 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15524 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015525
Evan Chengb5a55d92011-05-24 01:48:22 +000015526 if (CC == X86::COND_E || CC == X86::COND_NE) {
15527 switch (Cond.getOpcode()) {
15528 default: break;
15529 case X86ISD::BSR:
15530 case X86ISD::BSF:
15531 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15532 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15533 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15534 }
15535 }
15536
Michael Liao2a33cec2012-08-10 19:58:13 +000015537 SDValue Flags;
15538
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015539 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015540 if (Flags.getNode() &&
15541 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015542 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015543 SDValue Ops[] = { FalseOp, TrueOp,
15544 DAG.getConstant(CC, MVT::i8), Flags };
15545 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15546 Ops, array_lengthof(Ops));
15547 }
15548
Chris Lattnerd1980a52009-03-12 06:52:53 +000015549 // If this is a select between two integer constants, try to do some
15550 // optimizations. Note that the operands are ordered the opposite of SELECT
15551 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015552 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15553 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015554 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15555 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015556 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15557 CC = X86::GetOppositeBranchCondition(CC);
15558 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015559 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015560 }
Eric Christopherfd179292009-08-27 18:07:15 +000015561
Chris Lattnerd1980a52009-03-12 06:52:53 +000015562 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015563 // This is efficient for any integer data type (including i8/i16) and
15564 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015565 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015566 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15567 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015568
Chris Lattnerd1980a52009-03-12 06:52:53 +000015569 // Zero extend the condition if needed.
15570 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015571
Chris Lattnerd1980a52009-03-12 06:52:53 +000015572 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15573 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015574 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015575 if (N->getNumValues() == 2) // Dead flag value?
15576 return DCI.CombineTo(N, Cond, SDValue());
15577 return Cond;
15578 }
Eric Christopherfd179292009-08-27 18:07:15 +000015579
Chris Lattnercee56e72009-03-13 05:53:31 +000015580 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15581 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015582 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015583 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15584 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015585
Chris Lattner97a29a52009-03-13 05:22:11 +000015586 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015587 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15588 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015589 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15590 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015591
Chris Lattner97a29a52009-03-13 05:22:11 +000015592 if (N->getNumValues() == 2) // Dead flag value?
15593 return DCI.CombineTo(N, Cond, SDValue());
15594 return Cond;
15595 }
Eric Christopherfd179292009-08-27 18:07:15 +000015596
Chris Lattnercee56e72009-03-13 05:53:31 +000015597 // Optimize cases that will turn into an LEA instruction. This requires
15598 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015599 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015600 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015601 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015602
Chris Lattnercee56e72009-03-13 05:53:31 +000015603 bool isFastMultiplier = false;
15604 if (Diff < 10) {
15605 switch ((unsigned char)Diff) {
15606 default: break;
15607 case 1: // result = add base, cond
15608 case 2: // result = lea base( , cond*2)
15609 case 3: // result = lea base(cond, cond*2)
15610 case 4: // result = lea base( , cond*4)
15611 case 5: // result = lea base(cond, cond*4)
15612 case 8: // result = lea base( , cond*8)
15613 case 9: // result = lea base(cond, cond*8)
15614 isFastMultiplier = true;
15615 break;
15616 }
15617 }
Eric Christopherfd179292009-08-27 18:07:15 +000015618
Chris Lattnercee56e72009-03-13 05:53:31 +000015619 if (isFastMultiplier) {
15620 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015621 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15622 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015623 // Zero extend the condition if needed.
15624 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15625 Cond);
15626 // Scale the condition by the difference.
15627 if (Diff != 1)
15628 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15629 DAG.getConstant(Diff, Cond.getValueType()));
15630
15631 // Add the base if non-zero.
15632 if (FalseC->getAPIntValue() != 0)
15633 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15634 SDValue(FalseC, 0));
15635 if (N->getNumValues() == 2) // Dead flag value?
15636 return DCI.CombineTo(N, Cond, SDValue());
15637 return Cond;
15638 }
Eric Christopherfd179292009-08-27 18:07:15 +000015639 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015640 }
15641 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015642
15643 // Handle these cases:
15644 // (select (x != c), e, c) -> select (x != c), e, x),
15645 // (select (x == c), c, e) -> select (x == c), x, e)
15646 // where the c is an integer constant, and the "select" is the combination
15647 // of CMOV and CMP.
15648 //
15649 // The rationale for this change is that the conditional-move from a constant
15650 // needs two instructions, however, conditional-move from a register needs
15651 // only one instruction.
15652 //
15653 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15654 // some instruction-combining opportunities. This opt needs to be
15655 // postponed as late as possible.
15656 //
15657 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15658 // the DCI.xxxx conditions are provided to postpone the optimization as
15659 // late as possible.
15660
15661 ConstantSDNode *CmpAgainst = 0;
15662 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15663 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15664 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15665
15666 if (CC == X86::COND_NE &&
15667 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15668 CC = X86::GetOppositeBranchCondition(CC);
15669 std::swap(TrueOp, FalseOp);
15670 }
15671
15672 if (CC == X86::COND_E &&
15673 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15674 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15675 DAG.getConstant(CC, MVT::i8), Cond };
15676 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15677 array_lengthof(Ops));
15678 }
15679 }
15680 }
15681
Chris Lattnerd1980a52009-03-12 06:52:53 +000015682 return SDValue();
15683}
15684
Evan Cheng0b0cd912009-03-28 05:57:29 +000015685/// PerformMulCombine - Optimize a single multiply with constant into two
15686/// in order to implement it with two cheaper instructions, e.g.
15687/// LEA + SHL, LEA + LEA.
15688static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15689 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015690 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15691 return SDValue();
15692
Owen Andersone50ed302009-08-10 22:56:29 +000015693 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015694 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015695 return SDValue();
15696
15697 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15698 if (!C)
15699 return SDValue();
15700 uint64_t MulAmt = C->getZExtValue();
15701 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15702 return SDValue();
15703
15704 uint64_t MulAmt1 = 0;
15705 uint64_t MulAmt2 = 0;
15706 if ((MulAmt % 9) == 0) {
15707 MulAmt1 = 9;
15708 MulAmt2 = MulAmt / 9;
15709 } else if ((MulAmt % 5) == 0) {
15710 MulAmt1 = 5;
15711 MulAmt2 = MulAmt / 5;
15712 } else if ((MulAmt % 3) == 0) {
15713 MulAmt1 = 3;
15714 MulAmt2 = MulAmt / 3;
15715 }
15716 if (MulAmt2 &&
15717 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15718 DebugLoc DL = N->getDebugLoc();
15719
15720 if (isPowerOf2_64(MulAmt2) &&
15721 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15722 // If second multiplifer is pow2, issue it first. We want the multiply by
15723 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15724 // is an add.
15725 std::swap(MulAmt1, MulAmt2);
15726
15727 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015728 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015729 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015730 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015731 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015732 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015733 DAG.getConstant(MulAmt1, VT));
15734
Eric Christopherfd179292009-08-27 18:07:15 +000015735 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015736 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015737 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015738 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015739 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015740 DAG.getConstant(MulAmt2, VT));
15741
15742 // Do not add new nodes to DAG combiner worklist.
15743 DCI.CombineTo(N, NewMul, false);
15744 }
15745 return SDValue();
15746}
15747
Evan Chengad9c0a32009-12-15 00:53:42 +000015748static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15749 SDValue N0 = N->getOperand(0);
15750 SDValue N1 = N->getOperand(1);
15751 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15752 EVT VT = N0.getValueType();
15753
15754 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15755 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015756 if (VT.isInteger() && !VT.isVector() &&
15757 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015758 N0.getOperand(1).getOpcode() == ISD::Constant) {
15759 SDValue N00 = N0.getOperand(0);
15760 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15761 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15762 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15763 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15764 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15765 APInt ShAmt = N1C->getAPIntValue();
15766 Mask = Mask.shl(ShAmt);
15767 if (Mask != 0)
15768 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15769 N00, DAG.getConstant(Mask, VT));
15770 }
15771 }
15772
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015773 // Hardware support for vector shifts is sparse which makes us scalarize the
15774 // vector operations in many cases. Also, on sandybridge ADD is faster than
15775 // shl.
15776 // (shl V, 1) -> add V,V
15777 if (isSplatVector(N1.getNode())) {
15778 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15779 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15780 // We shift all of the values by one. In many cases we do not have
15781 // hardware support for this operation. This is better expressed as an ADD
15782 // of two values.
15783 if (N1C && (1 == N1C->getZExtValue())) {
15784 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15785 }
15786 }
15787
Evan Chengad9c0a32009-12-15 00:53:42 +000015788 return SDValue();
15789}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015790
Nate Begeman740ab032009-01-26 00:52:55 +000015791/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15792/// when possible.
15793static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015794 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015795 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015796 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015797 if (N->getOpcode() == ISD::SHL) {
15798 SDValue V = PerformSHLCombine(N, DAG);
15799 if (V.getNode()) return V;
15800 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015801
Nate Begeman740ab032009-01-26 00:52:55 +000015802 // On X86 with SSE2 support, we can transform this to a vector shift if
15803 // all elements are shifted by the same amount. We can't do this in legalize
15804 // because the a constant vector is typically transformed to a constant pool
15805 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015806 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015807 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015808
Craig Topper7be5dfd2011-11-12 09:58:49 +000015809 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015810 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015811 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015812 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015813
Mon P Wang3becd092009-01-28 08:12:05 +000015814 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015815 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015816 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015817 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015818 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15819 unsigned NumElts = VT.getVectorNumElements();
15820 unsigned i = 0;
15821 for (; i != NumElts; ++i) {
15822 SDValue Arg = ShAmtOp.getOperand(i);
15823 if (Arg.getOpcode() == ISD::UNDEF) continue;
15824 BaseShAmt = Arg;
15825 break;
15826 }
Craig Topper37c26772012-01-17 04:44:50 +000015827 // Handle the case where the build_vector is all undef
15828 // FIXME: Should DAG allow this?
15829 if (i == NumElts)
15830 return SDValue();
15831
Mon P Wang3becd092009-01-28 08:12:05 +000015832 for (; i != NumElts; ++i) {
15833 SDValue Arg = ShAmtOp.getOperand(i);
15834 if (Arg.getOpcode() == ISD::UNDEF) continue;
15835 if (Arg != BaseShAmt) {
15836 return SDValue();
15837 }
15838 }
15839 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015840 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015841 SDValue InVec = ShAmtOp.getOperand(0);
15842 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15843 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15844 unsigned i = 0;
15845 for (; i != NumElts; ++i) {
15846 SDValue Arg = InVec.getOperand(i);
15847 if (Arg.getOpcode() == ISD::UNDEF) continue;
15848 BaseShAmt = Arg;
15849 break;
15850 }
15851 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015853 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015854 if (C->getZExtValue() == SplatIdx)
15855 BaseShAmt = InVec.getOperand(1);
15856 }
15857 }
Mon P Wang845b1892012-02-01 22:15:20 +000015858 if (BaseShAmt.getNode() == 0) {
15859 // Don't create instructions with illegal types after legalize
15860 // types has run.
15861 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15862 !DCI.isBeforeLegalize())
15863 return SDValue();
15864
Mon P Wangefa42202009-09-03 19:56:25 +000015865 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15866 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015867 }
Mon P Wang3becd092009-01-28 08:12:05 +000015868 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015869 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015870
Mon P Wangefa42202009-09-03 19:56:25 +000015871 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015872 if (EltVT.bitsGT(MVT::i32))
15873 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15874 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015875 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015876
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015877 // The shift amount is identical so we can do a vector shift.
15878 SDValue ValOp = N->getOperand(0);
15879 switch (N->getOpcode()) {
15880 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015881 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015882 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015883 switch (VT.getSimpleVT().SimpleTy) {
15884 default: return SDValue();
15885 case MVT::v2i64:
15886 case MVT::v4i32:
15887 case MVT::v8i16:
15888 case MVT::v4i64:
15889 case MVT::v8i32:
15890 case MVT::v16i16:
15891 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15892 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015893 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015894 switch (VT.getSimpleVT().SimpleTy) {
15895 default: return SDValue();
15896 case MVT::v4i32:
15897 case MVT::v8i16:
15898 case MVT::v8i32:
15899 case MVT::v16i16:
15900 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15901 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015902 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015903 switch (VT.getSimpleVT().SimpleTy) {
15904 default: return SDValue();
15905 case MVT::v2i64:
15906 case MVT::v4i32:
15907 case MVT::v8i16:
15908 case MVT::v4i64:
15909 case MVT::v8i32:
15910 case MVT::v16i16:
15911 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15912 }
Nate Begeman740ab032009-01-26 00:52:55 +000015913 }
Nate Begeman740ab032009-01-26 00:52:55 +000015914}
15915
Stuart Hastings865f0932011-06-03 23:53:54 +000015916// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15917// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15918// and friends. Likewise for OR -> CMPNEQSS.
15919static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15920 TargetLowering::DAGCombinerInfo &DCI,
15921 const X86Subtarget *Subtarget) {
15922 unsigned opcode;
15923
15924 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15925 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015926 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015927 SDValue N0 = N->getOperand(0);
15928 SDValue N1 = N->getOperand(1);
15929 SDValue CMP0 = N0->getOperand(1);
15930 SDValue CMP1 = N1->getOperand(1);
15931 DebugLoc DL = N->getDebugLoc();
15932
15933 // The SETCCs should both refer to the same CMP.
15934 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15935 return SDValue();
15936
15937 SDValue CMP00 = CMP0->getOperand(0);
15938 SDValue CMP01 = CMP0->getOperand(1);
15939 EVT VT = CMP00.getValueType();
15940
15941 if (VT == MVT::f32 || VT == MVT::f64) {
15942 bool ExpectingFlags = false;
15943 // Check for any users that want flags:
15944 for (SDNode::use_iterator UI = N->use_begin(),
15945 UE = N->use_end();
15946 !ExpectingFlags && UI != UE; ++UI)
15947 switch (UI->getOpcode()) {
15948 default:
15949 case ISD::BR_CC:
15950 case ISD::BRCOND:
15951 case ISD::SELECT:
15952 ExpectingFlags = true;
15953 break;
15954 case ISD::CopyToReg:
15955 case ISD::SIGN_EXTEND:
15956 case ISD::ZERO_EXTEND:
15957 case ISD::ANY_EXTEND:
15958 break;
15959 }
15960
15961 if (!ExpectingFlags) {
15962 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15963 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15964
15965 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15966 X86::CondCode tmp = cc0;
15967 cc0 = cc1;
15968 cc1 = tmp;
15969 }
15970
15971 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15972 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15973 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15974 X86ISD::NodeType NTOperator = is64BitFP ?
15975 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15976 // FIXME: need symbolic constants for these magic numbers.
15977 // See X86ATTInstPrinter.cpp:printSSECC().
15978 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15979 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15980 DAG.getConstant(x86cc, MVT::i8));
15981 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15982 OnesOrZeroesF);
15983 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15984 DAG.getConstant(1, MVT::i32));
15985 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15986 return OneBitOfTruth;
15987 }
15988 }
15989 }
15990 }
15991 return SDValue();
15992}
15993
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015994/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15995/// so it can be folded inside ANDNP.
15996static bool CanFoldXORWithAllOnes(const SDNode *N) {
15997 EVT VT = N->getValueType(0);
15998
15999 // Match direct AllOnes for 128 and 256-bit vectors
16000 if (ISD::isBuildVectorAllOnes(N))
16001 return true;
16002
16003 // Look through a bit convert.
16004 if (N->getOpcode() == ISD::BITCAST)
16005 N = N->getOperand(0).getNode();
16006
16007 // Sometimes the operand may come from a insert_subvector building a 256-bit
16008 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016009 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016010 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16011 SDValue V1 = N->getOperand(0);
16012 SDValue V2 = N->getOperand(1);
16013
16014 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16015 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16016 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16017 ISD::isBuildVectorAllOnes(V2.getNode()))
16018 return true;
16019 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016020
16021 return false;
16022}
16023
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016024// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16025// register. In most cases we actually compare or select YMM-sized registers
16026// and mixing the two types creates horrible code. This method optimizes
16027// some of the transition sequences.
16028static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16029 TargetLowering::DAGCombinerInfo &DCI,
16030 const X86Subtarget *Subtarget) {
16031 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016032 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016033 return SDValue();
16034
16035 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16036 N->getOpcode() == ISD::ZERO_EXTEND ||
16037 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16038
16039 SDValue Narrow = N->getOperand(0);
16040 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016041 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016042 return SDValue();
16043
16044 if (Narrow->getOpcode() != ISD::XOR &&
16045 Narrow->getOpcode() != ISD::AND &&
16046 Narrow->getOpcode() != ISD::OR)
16047 return SDValue();
16048
16049 SDValue N0 = Narrow->getOperand(0);
16050 SDValue N1 = Narrow->getOperand(1);
16051 DebugLoc DL = Narrow->getDebugLoc();
16052
16053 // The Left side has to be a trunc.
16054 if (N0.getOpcode() != ISD::TRUNCATE)
16055 return SDValue();
16056
16057 // The type of the truncated inputs.
16058 EVT WideVT = N0->getOperand(0)->getValueType(0);
16059 if (WideVT != VT)
16060 return SDValue();
16061
16062 // The right side has to be a 'trunc' or a constant vector.
16063 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16064 bool RHSConst = (isSplatVector(N1.getNode()) &&
16065 isa<ConstantSDNode>(N1->getOperand(0)));
16066 if (!RHSTrunc && !RHSConst)
16067 return SDValue();
16068
16069 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16070
16071 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16072 return SDValue();
16073
16074 // Set N0 and N1 to hold the inputs to the new wide operation.
16075 N0 = N0->getOperand(0);
16076 if (RHSConst) {
16077 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16078 N1->getOperand(0));
16079 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16080 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16081 } else if (RHSTrunc) {
16082 N1 = N1->getOperand(0);
16083 }
16084
16085 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016086 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016087 unsigned Opcode = N->getOpcode();
16088 switch (Opcode) {
16089 case ISD::ANY_EXTEND:
16090 return Op;
16091 case ISD::ZERO_EXTEND: {
16092 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16093 APInt Mask = APInt::getAllOnesValue(InBits);
16094 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16095 return DAG.getNode(ISD::AND, DL, VT,
16096 Op, DAG.getConstant(Mask, VT));
16097 }
16098 case ISD::SIGN_EXTEND:
16099 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16100 Op, DAG.getValueType(NarrowVT));
16101 default:
16102 llvm_unreachable("Unexpected opcode");
16103 }
16104}
16105
Nate Begemanb65c1752010-12-17 22:55:37 +000016106static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16107 TargetLowering::DAGCombinerInfo &DCI,
16108 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016109 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016110 if (DCI.isBeforeLegalizeOps())
16111 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016112
Stuart Hastings865f0932011-06-03 23:53:54 +000016113 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16114 if (R.getNode())
16115 return R;
16116
Craig Topperb926afc2012-12-17 05:12:30 +000016117 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016118 // BLSI is X & (-X)
16119 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016120 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16121 SDValue N0 = N->getOperand(0);
16122 SDValue N1 = N->getOperand(1);
16123 DebugLoc DL = N->getDebugLoc();
16124
Craig Topperb4c94572011-10-21 06:55:01 +000016125 // Check LHS for neg
16126 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16127 isZero(N0.getOperand(0)))
16128 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16129
16130 // Check RHS for neg
16131 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16132 isZero(N1.getOperand(0)))
16133 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16134
16135 // Check LHS for X-1
16136 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16137 isAllOnes(N0.getOperand(1)))
16138 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16139
16140 // Check RHS for X-1
16141 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16142 isAllOnes(N1.getOperand(1)))
16143 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16144
Craig Topper54a11172011-10-14 07:06:56 +000016145 return SDValue();
16146 }
16147
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016148 // Want to form ANDNP nodes:
16149 // 1) In the hopes of then easily combining them with OR and AND nodes
16150 // to form PBLEND/PSIGN.
16151 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016152 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016153 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016154
Nate Begemanb65c1752010-12-17 22:55:37 +000016155 SDValue N0 = N->getOperand(0);
16156 SDValue N1 = N->getOperand(1);
16157 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016158
Nate Begemanb65c1752010-12-17 22:55:37 +000016159 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016160 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016161 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16162 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016163 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016164
16165 // Check RHS for vnot
16166 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016167 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16168 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016169 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016170
Nate Begemanb65c1752010-12-17 22:55:37 +000016171 return SDValue();
16172}
16173
Evan Cheng760d1942010-01-04 21:22:48 +000016174static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016175 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016176 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016177 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016178 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016179 return SDValue();
16180
Stuart Hastings865f0932011-06-03 23:53:54 +000016181 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16182 if (R.getNode())
16183 return R;
16184
Evan Cheng760d1942010-01-04 21:22:48 +000016185 SDValue N0 = N->getOperand(0);
16186 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016187
Nate Begemanb65c1752010-12-17 22:55:37 +000016188 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016189 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016190 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016191 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016192 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016193
Craig Topper1666cb62011-11-19 07:07:26 +000016194 // Canonicalize pandn to RHS
16195 if (N0.getOpcode() == X86ISD::ANDNP)
16196 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016197 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016198 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16199 SDValue Mask = N1.getOperand(0);
16200 SDValue X = N1.getOperand(1);
16201 SDValue Y;
16202 if (N0.getOperand(0) == Mask)
16203 Y = N0.getOperand(1);
16204 if (N0.getOperand(1) == Mask)
16205 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016206
Craig Topper1666cb62011-11-19 07:07:26 +000016207 // Check to see if the mask appeared in both the AND and ANDNP and
16208 if (!Y.getNode())
16209 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016210
Craig Topper1666cb62011-11-19 07:07:26 +000016211 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016212 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016213 if (Mask.getOpcode() == ISD::BITCAST)
16214 Mask = Mask.getOperand(0);
16215 if (X.getOpcode() == ISD::BITCAST)
16216 X = X.getOperand(0);
16217 if (Y.getOpcode() == ISD::BITCAST)
16218 Y = Y.getOperand(0);
16219
Craig Topper1666cb62011-11-19 07:07:26 +000016220 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016221
Craig Toppered2e13d2012-01-22 19:15:14 +000016222 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016223 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16224 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016225 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016226 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016227
16228 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016229 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016230 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16231 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16232 if ((SraAmt + 1) != EltBits)
16233 return SDValue();
16234
16235 DebugLoc DL = N->getDebugLoc();
16236
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016237 // We are going to replace the AND, OR, NAND with either BLEND
16238 // or PSIGN, which only look at the MSB. The VSRAI instruction
16239 // does not affect the highest bit, so we can get rid of it.
16240 Mask = Mask.getOperand(0);
16241
Craig Topper1666cb62011-11-19 07:07:26 +000016242 // Now we know we at least have a plendvb with the mask val. See if
16243 // we can form a psignb/w/d.
16244 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016245 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16246 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016247 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16248 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16249 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016250 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016251 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016252 }
16253 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016254 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016255 return SDValue();
16256
16257 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16258
16259 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16260 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16261 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016262 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016263 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016264 }
16265 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016266
Craig Topper1666cb62011-11-19 07:07:26 +000016267 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16268 return SDValue();
16269
Nate Begemanb65c1752010-12-17 22:55:37 +000016270 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016271 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16272 std::swap(N0, N1);
16273 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16274 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016275 if (!N0.hasOneUse() || !N1.hasOneUse())
16276 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016277
16278 SDValue ShAmt0 = N0.getOperand(1);
16279 if (ShAmt0.getValueType() != MVT::i8)
16280 return SDValue();
16281 SDValue ShAmt1 = N1.getOperand(1);
16282 if (ShAmt1.getValueType() != MVT::i8)
16283 return SDValue();
16284 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16285 ShAmt0 = ShAmt0.getOperand(0);
16286 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16287 ShAmt1 = ShAmt1.getOperand(0);
16288
16289 DebugLoc DL = N->getDebugLoc();
16290 unsigned Opc = X86ISD::SHLD;
16291 SDValue Op0 = N0.getOperand(0);
16292 SDValue Op1 = N1.getOperand(0);
16293 if (ShAmt0.getOpcode() == ISD::SUB) {
16294 Opc = X86ISD::SHRD;
16295 std::swap(Op0, Op1);
16296 std::swap(ShAmt0, ShAmt1);
16297 }
16298
Evan Cheng8b1190a2010-04-28 01:18:01 +000016299 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016300 if (ShAmt1.getOpcode() == ISD::SUB) {
16301 SDValue Sum = ShAmt1.getOperand(0);
16302 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016303 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16304 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16305 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16306 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016307 return DAG.getNode(Opc, DL, VT,
16308 Op0, Op1,
16309 DAG.getNode(ISD::TRUNCATE, DL,
16310 MVT::i8, ShAmt0));
16311 }
16312 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16313 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16314 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016315 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016316 return DAG.getNode(Opc, DL, VT,
16317 N0.getOperand(0), N1.getOperand(0),
16318 DAG.getNode(ISD::TRUNCATE, DL,
16319 MVT::i8, ShAmt0));
16320 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016321
Evan Cheng760d1942010-01-04 21:22:48 +000016322 return SDValue();
16323}
16324
Manman Ren92363622012-06-07 22:39:10 +000016325// Generate NEG and CMOV for integer abs.
16326static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16327 EVT VT = N->getValueType(0);
16328
16329 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16330 // 8-bit integer abs to NEG and CMOV.
16331 if (VT.isInteger() && VT.getSizeInBits() == 8)
16332 return SDValue();
16333
16334 SDValue N0 = N->getOperand(0);
16335 SDValue N1 = N->getOperand(1);
16336 DebugLoc DL = N->getDebugLoc();
16337
16338 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16339 // and change it to SUB and CMOV.
16340 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16341 N0.getOpcode() == ISD::ADD &&
16342 N0.getOperand(1) == N1 &&
16343 N1.getOpcode() == ISD::SRA &&
16344 N1.getOperand(0) == N0.getOperand(0))
16345 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16346 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16347 // Generate SUB & CMOV.
16348 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16349 DAG.getConstant(0, VT), N0.getOperand(0));
16350
16351 SDValue Ops[] = { N0.getOperand(0), Neg,
16352 DAG.getConstant(X86::COND_GE, MVT::i8),
16353 SDValue(Neg.getNode(), 1) };
16354 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16355 Ops, array_lengthof(Ops));
16356 }
16357 return SDValue();
16358}
16359
Craig Topper3738ccd2011-12-27 06:27:23 +000016360// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016361static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16362 TargetLowering::DAGCombinerInfo &DCI,
16363 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016364 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016365 if (DCI.isBeforeLegalizeOps())
16366 return SDValue();
16367
Manman Ren45d53b82012-06-08 18:58:26 +000016368 if (Subtarget->hasCMov()) {
16369 SDValue RV = performIntegerAbsCombine(N, DAG);
16370 if (RV.getNode())
16371 return RV;
16372 }
Manman Ren92363622012-06-07 22:39:10 +000016373
16374 // Try forming BMI if it is available.
16375 if (!Subtarget->hasBMI())
16376 return SDValue();
16377
Craig Topperb4c94572011-10-21 06:55:01 +000016378 if (VT != MVT::i32 && VT != MVT::i64)
16379 return SDValue();
16380
Craig Topper3738ccd2011-12-27 06:27:23 +000016381 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16382
Craig Topperb4c94572011-10-21 06:55:01 +000016383 // Create BLSMSK instructions by finding X ^ (X-1)
16384 SDValue N0 = N->getOperand(0);
16385 SDValue N1 = N->getOperand(1);
16386 DebugLoc DL = N->getDebugLoc();
16387
16388 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16389 isAllOnes(N0.getOperand(1)))
16390 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16391
16392 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16393 isAllOnes(N1.getOperand(1)))
16394 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16395
16396 return SDValue();
16397}
16398
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016399/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16400static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016401 TargetLowering::DAGCombinerInfo &DCI,
16402 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016403 LoadSDNode *Ld = cast<LoadSDNode>(N);
16404 EVT RegVT = Ld->getValueType(0);
16405 EVT MemVT = Ld->getMemoryVT();
16406 DebugLoc dl = Ld->getDebugLoc();
16407 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016408 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016409
16410 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016411 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016412 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016413
16414 // On Sandybridge unaligned 256bit loads are inefficient.
16415 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016416 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016417 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016418 if (NumElems < 2)
16419 return SDValue();
16420
Nadav Rotem48177ac2013-01-18 23:10:30 +000016421 SDValue Ptr = Ld->getBasePtr();
16422 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16423
16424 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16425 NumElems/2);
16426 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16427 Ld->getPointerInfo(), Ld->isVolatile(),
16428 Ld->isNonTemporal(), Ld->isInvariant(),
16429 Alignment);
16430 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16431 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16432 Ld->getPointerInfo(), Ld->isVolatile(),
16433 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016434 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016435 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16436 Load1.getValue(1),
16437 Load2.getValue(1));
16438
16439 SDValue NewVec = DAG.getUNDEF(RegVT);
16440 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16441 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16442 return DCI.CombineTo(N, NewVec, TF, true);
16443 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016444
Nadav Rotemca6f2962011-09-18 19:00:23 +000016445 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016446 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16447 // expansion is still better than scalar code.
16448 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16449 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016450 // TODO: It is possible to support ZExt by zeroing the undef values
16451 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016452 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16453 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016454 assert(MemVT != RegVT && "Cannot extend to the same type");
16455 assert(MemVT.isVector() && "Must load a vector from memory");
16456
16457 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016458 unsigned MemSz = MemVT.getSizeInBits();
16459 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016460
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016461 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16462 return SDValue();
16463
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016464 // All sizes must be a power of two.
16465 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16466 return SDValue();
16467
16468 // Attempt to load the original value using scalar loads.
16469 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016470 MVT SclrLoadTy = MVT::i8;
16471 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16472 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16473 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016474 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016475 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016476 }
16477 }
16478
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016479 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16480 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16481 (64 <= MemSz))
16482 SclrLoadTy = MVT::f64;
16483
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016484 // Calculate the number of scalar loads that we need to perform
16485 // in order to load our vector from memory.
16486 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016487 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16488 return SDValue();
16489
16490 unsigned loadRegZize = RegSz;
16491 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16492 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016493
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016494 // Represent our vector as a sequence of elements which are the
16495 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016496 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016497 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016498
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016499 // Represent the data using the same element type that is stored in
16500 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016501 EVT WideVecVT =
16502 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016503 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016504
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016505 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16506 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016507
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016508 // We can't shuffle using an illegal type.
16509 if (!TLI.isTypeLegal(WideVecVT))
16510 return SDValue();
16511
16512 SmallVector<SDValue, 8> Chains;
16513 SDValue Ptr = Ld->getBasePtr();
16514 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16515 TLI.getPointerTy());
16516 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16517
16518 for (unsigned i = 0; i < NumLoads; ++i) {
16519 // Perform a single load.
16520 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16521 Ptr, Ld->getPointerInfo(),
16522 Ld->isVolatile(), Ld->isNonTemporal(),
16523 Ld->isInvariant(), Ld->getAlignment());
16524 Chains.push_back(ScalarLoad.getValue(1));
16525 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16526 // another round of DAGCombining.
16527 if (i == 0)
16528 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16529 else
16530 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16531 ScalarLoad, DAG.getIntPtrConstant(i));
16532
16533 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16534 }
16535
16536 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16537 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016538
16539 // Bitcast the loaded value to a vector of the original element type, in
16540 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016541 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016542 unsigned SizeRatio = RegSz/MemSz;
16543
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016544 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016545 // If we have SSE4.1 we can directly emit a VSEXT node.
16546 if (Subtarget->hasSSE41()) {
16547 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16548 return DCI.CombineTo(N, Sext, TF, true);
16549 }
16550
16551 // Otherwise we'll shuffle the small elements in the high bits of the
16552 // larger type and perform an arithmetic shift. If the shift is not legal
16553 // it's better to scalarize.
16554 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16555 return SDValue();
16556
16557 // Redistribute the loaded elements into the different locations.
16558 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16559 for (unsigned i = 0; i != NumElems; ++i)
16560 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16561
16562 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16563 DAG.getUNDEF(WideVecVT),
16564 &ShuffleVec[0]);
16565
16566 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16567
16568 // Build the arithmetic shift.
16569 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16570 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016571 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16572 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016573
16574 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016575 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016576
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016577 // Redistribute the loaded elements into the different locations.
16578 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016579 for (unsigned i = 0; i != NumElems; ++i)
16580 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016581
16582 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016583 DAG.getUNDEF(WideVecVT),
16584 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016585
16586 // Bitcast to the requested type.
16587 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16588 // Replace the original load with the new sequence
16589 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016590 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016591 }
16592
16593 return SDValue();
16594}
16595
Chris Lattner149a4e52008-02-22 02:09:43 +000016596/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016597static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016598 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016599 StoreSDNode *St = cast<StoreSDNode>(N);
16600 EVT VT = St->getValue().getValueType();
16601 EVT StVT = St->getMemoryVT();
16602 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016603 SDValue StoredVal = St->getOperand(1);
16604 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016605 unsigned Alignment = St->getAlignment();
16606 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016607
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016608 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016609 // On Sandy Bridge, 256-bit memory operations are executed by two
16610 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16611 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016612 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016613 StVT == VT && !IsAligned) {
16614 unsigned NumElems = VT.getVectorNumElements();
16615 if (NumElems < 2)
16616 return SDValue();
16617
16618 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16619 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016620
16621 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16622 SDValue Ptr0 = St->getBasePtr();
16623 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16624
16625 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16626 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016627 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016628 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16629 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016630 St->isNonTemporal(),
16631 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016632 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16633 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016634
16635 // Optimize trunc store (of multiple scalars) to shuffle and store.
16636 // First, pack all of the elements in one place. Next, store to memory
16637 // in fewer chunks.
16638 if (St->isTruncatingStore() && VT.isVector()) {
16639 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16640 unsigned NumElems = VT.getVectorNumElements();
16641 assert(StVT != VT && "Cannot truncate to the same type");
16642 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16643 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16644
16645 // From, To sizes and ElemCount must be pow of two
16646 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016647 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016648 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016649 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016650
Nadav Rotem614061b2011-08-10 19:30:14 +000016651 unsigned SizeRatio = FromSz / ToSz;
16652
16653 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16654
16655 // Create a type on which we perform the shuffle
16656 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16657 StVT.getScalarType(), NumElems*SizeRatio);
16658
16659 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16660
16661 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16662 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016663 for (unsigned i = 0; i != NumElems; ++i)
16664 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016665
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016666 // Can't shuffle using an illegal type.
16667 if (!TLI.isTypeLegal(WideVecVT))
16668 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016669
16670 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016671 DAG.getUNDEF(WideVecVT),
16672 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016673 // At this point all of the data is stored at the bottom of the
16674 // register. We now need to save it to mem.
16675
16676 // Find the largest store unit
16677 MVT StoreType = MVT::i8;
16678 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16679 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16680 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016681 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016682 StoreType = Tp;
16683 }
16684
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016685 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16686 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16687 (64 <= NumElems * ToSz))
16688 StoreType = MVT::f64;
16689
Nadav Rotem614061b2011-08-10 19:30:14 +000016690 // Bitcast the original vector into a vector of store-size units
16691 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016692 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016693 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16694 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16695 SmallVector<SDValue, 8> Chains;
16696 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16697 TLI.getPointerTy());
16698 SDValue Ptr = St->getBasePtr();
16699
16700 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016701 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016702 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16703 StoreType, ShuffWide,
16704 DAG.getIntPtrConstant(i));
16705 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16706 St->getPointerInfo(), St->isVolatile(),
16707 St->isNonTemporal(), St->getAlignment());
16708 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16709 Chains.push_back(Ch);
16710 }
16711
16712 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16713 Chains.size());
16714 }
16715
Chris Lattner149a4e52008-02-22 02:09:43 +000016716 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16717 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016718 // A preferable solution to the general problem is to figure out the right
16719 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016720
16721 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016722 if (VT.getSizeInBits() != 64)
16723 return SDValue();
16724
Devang Patel578efa92009-06-05 21:57:13 +000016725 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016726 bool NoImplicitFloatOps = F->getAttributes().
16727 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016728 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016729 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016730 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016731 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016732 isa<LoadSDNode>(St->getValue()) &&
16733 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16734 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016735 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016736 LoadSDNode *Ld = 0;
16737 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016738 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016739 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016740 // Must be a store of a load. We currently handle two cases: the load
16741 // is a direct child, and it's under an intervening TokenFactor. It is
16742 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016743 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016744 Ld = cast<LoadSDNode>(St->getChain());
16745 else if (St->getValue().hasOneUse() &&
16746 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016747 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016748 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016749 TokenFactorIndex = i;
16750 Ld = cast<LoadSDNode>(St->getValue());
16751 } else
16752 Ops.push_back(ChainVal->getOperand(i));
16753 }
16754 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016755
Evan Cheng536e6672009-03-12 05:59:15 +000016756 if (!Ld || !ISD::isNormalLoad(Ld))
16757 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016758
Evan Cheng536e6672009-03-12 05:59:15 +000016759 // If this is not the MMX case, i.e. we are just turning i64 load/store
16760 // into f64 load/store, avoid the transformation if there are multiple
16761 // uses of the loaded value.
16762 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16763 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016764
Evan Cheng536e6672009-03-12 05:59:15 +000016765 DebugLoc LdDL = Ld->getDebugLoc();
16766 DebugLoc StDL = N->getDebugLoc();
16767 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16768 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16769 // pair instead.
16770 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016771 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016772 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16773 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016774 Ld->isNonTemporal(), Ld->isInvariant(),
16775 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016776 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016777 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016778 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016779 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016780 Ops.size());
16781 }
Evan Cheng536e6672009-03-12 05:59:15 +000016782 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016783 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016784 St->isVolatile(), St->isNonTemporal(),
16785 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016786 }
Evan Cheng536e6672009-03-12 05:59:15 +000016787
16788 // Otherwise, lower to two pairs of 32-bit loads / stores.
16789 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016790 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16791 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016792
Owen Anderson825b72b2009-08-11 20:47:22 +000016793 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016794 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016795 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016796 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016797 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016798 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016799 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016800 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016801 MinAlign(Ld->getAlignment(), 4));
16802
16803 SDValue NewChain = LoLd.getValue(1);
16804 if (TokenFactorIndex != -1) {
16805 Ops.push_back(LoLd);
16806 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016807 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016808 Ops.size());
16809 }
16810
16811 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016812 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16813 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016814
16815 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016816 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016817 St->isVolatile(), St->isNonTemporal(),
16818 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016819 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016820 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016821 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016822 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016823 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016824 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016825 }
Dan Gohman475871a2008-07-27 21:46:04 +000016826 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016827}
16828
Duncan Sands17470be2011-09-22 20:15:48 +000016829/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16830/// and return the operands for the horizontal operation in LHS and RHS. A
16831/// horizontal operation performs the binary operation on successive elements
16832/// of its first operand, then on successive elements of its second operand,
16833/// returning the resulting values in a vector. For example, if
16834/// A = < float a0, float a1, float a2, float a3 >
16835/// and
16836/// B = < float b0, float b1, float b2, float b3 >
16837/// then the result of doing a horizontal operation on A and B is
16838/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16839/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16840/// A horizontal-op B, for some already available A and B, and if so then LHS is
16841/// set to A, RHS to B, and the routine returns 'true'.
16842/// Note that the binary operation should have the property that if one of the
16843/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016844static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016845 // Look for the following pattern: if
16846 // A = < float a0, float a1, float a2, float a3 >
16847 // B = < float b0, float b1, float b2, float b3 >
16848 // and
16849 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16850 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16851 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16852 // which is A horizontal-op B.
16853
16854 // At least one of the operands should be a vector shuffle.
16855 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16856 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16857 return false;
16858
16859 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016860
16861 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16862 "Unsupported vector type for horizontal add/sub");
16863
16864 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16865 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016866 unsigned NumElts = VT.getVectorNumElements();
16867 unsigned NumLanes = VT.getSizeInBits()/128;
16868 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016869 assert((NumLaneElts % 2 == 0) &&
16870 "Vector type should have an even number of elements in each lane");
16871 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016872
16873 // View LHS in the form
16874 // LHS = VECTOR_SHUFFLE A, B, LMask
16875 // If LHS is not a shuffle then pretend it is the shuffle
16876 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16877 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16878 // type VT.
16879 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016880 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016881 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16882 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16883 A = LHS.getOperand(0);
16884 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16885 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016886 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16887 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016888 } else {
16889 if (LHS.getOpcode() != ISD::UNDEF)
16890 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016891 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016892 LMask[i] = i;
16893 }
16894
16895 // Likewise, view RHS in the form
16896 // RHS = VECTOR_SHUFFLE C, D, RMask
16897 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016898 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016899 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16900 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16901 C = RHS.getOperand(0);
16902 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16903 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016904 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16905 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016906 } else {
16907 if (RHS.getOpcode() != ISD::UNDEF)
16908 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016909 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016910 RMask[i] = i;
16911 }
16912
16913 // Check that the shuffles are both shuffling the same vectors.
16914 if (!(A == C && B == D) && !(A == D && B == C))
16915 return false;
16916
16917 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16918 if (!A.getNode() && !B.getNode())
16919 return false;
16920
16921 // If A and B occur in reverse order in RHS, then "swap" them (which means
16922 // rewriting the mask).
16923 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016924 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016925
16926 // At this point LHS and RHS are equivalent to
16927 // LHS = VECTOR_SHUFFLE A, B, LMask
16928 // RHS = VECTOR_SHUFFLE A, B, RMask
16929 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016930 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016931 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016932
Craig Topperf8363302011-12-02 08:18:41 +000016933 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016934 if (LIdx < 0 || RIdx < 0 ||
16935 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16936 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016937 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016938
Craig Topperf8363302011-12-02 08:18:41 +000016939 // Check that successive elements are being operated on. If not, this is
16940 // not a horizontal operation.
16941 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16942 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016943 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016944 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016945 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016946 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016947 }
16948
16949 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16950 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16951 return true;
16952}
16953
16954/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16955static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16956 const X86Subtarget *Subtarget) {
16957 EVT VT = N->getValueType(0);
16958 SDValue LHS = N->getOperand(0);
16959 SDValue RHS = N->getOperand(1);
16960
16961 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016962 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016963 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016964 isHorizontalBinOp(LHS, RHS, true))
16965 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16966 return SDValue();
16967}
16968
16969/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16970static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16971 const X86Subtarget *Subtarget) {
16972 EVT VT = N->getValueType(0);
16973 SDValue LHS = N->getOperand(0);
16974 SDValue RHS = N->getOperand(1);
16975
16976 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016977 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016978 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016979 isHorizontalBinOp(LHS, RHS, false))
16980 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16981 return SDValue();
16982}
16983
Chris Lattner6cf73262008-01-25 06:14:17 +000016984/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16985/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016986static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016987 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16988 // F[X]OR(0.0, x) -> x
16989 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016990 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16991 if (C->getValueAPF().isPosZero())
16992 return N->getOperand(1);
16993 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16994 if (C->getValueAPF().isPosZero())
16995 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016996 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016997}
16998
Nadav Rotemd60cb112012-08-19 13:06:16 +000016999/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17000/// X86ISD::FMAX nodes.
17001static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17002 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17003
17004 // Only perform optimizations if UnsafeMath is used.
17005 if (!DAG.getTarget().Options.UnsafeFPMath)
17006 return SDValue();
17007
17008 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017009 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017010 unsigned NewOp = 0;
17011 switch (N->getOpcode()) {
17012 default: llvm_unreachable("unknown opcode");
17013 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17014 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17015 }
17016
17017 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17018 N->getOperand(0), N->getOperand(1));
17019}
17020
Chris Lattneraf723b92008-01-25 05:46:26 +000017021/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017022static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017023 // FAND(0.0, x) -> 0.0
17024 // FAND(x, 0.0) -> 0.0
17025 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17026 if (C->getValueAPF().isPosZero())
17027 return N->getOperand(0);
17028 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17029 if (C->getValueAPF().isPosZero())
17030 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017031 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017032}
17033
Dan Gohmane5af2d32009-01-29 01:59:02 +000017034static SDValue PerformBTCombine(SDNode *N,
17035 SelectionDAG &DAG,
17036 TargetLowering::DAGCombinerInfo &DCI) {
17037 // BT ignores high bits in the bit index operand.
17038 SDValue Op1 = N->getOperand(1);
17039 if (Op1.hasOneUse()) {
17040 unsigned BitWidth = Op1.getValueSizeInBits();
17041 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17042 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017043 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17044 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017045 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017046 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17047 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17048 DCI.CommitTargetLoweringOpt(TLO);
17049 }
17050 return SDValue();
17051}
Chris Lattner83e6c992006-10-04 06:57:07 +000017052
Eli Friedman7a5e5552009-06-07 06:52:44 +000017053static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17054 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017055 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017056 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017057 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017058 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017059 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017060 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017061 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017062 }
17063 return SDValue();
17064}
17065
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017066static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17067 TargetLowering::DAGCombinerInfo &DCI,
17068 const X86Subtarget *Subtarget) {
17069 if (!DCI.isBeforeLegalizeOps())
17070 return SDValue();
17071
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017072 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017073 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017074
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017075 EVT VT = N->getValueType(0);
17076 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017077 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17078 if (R.getNode())
17079 return R;
17080 }
17081
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017082 return SDValue();
17083}
17084
Michael Liaof6c24ee2012-08-10 14:39:24 +000017085static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017086 const X86Subtarget* Subtarget) {
17087 DebugLoc dl = N->getDebugLoc();
17088 EVT VT = N->getValueType(0);
17089
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017090 // Let legalize expand this if it isn't a legal type yet.
17091 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17092 return SDValue();
17093
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017094 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017095 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17096 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017097 return SDValue();
17098
17099 SDValue A = N->getOperand(0);
17100 SDValue B = N->getOperand(1);
17101 SDValue C = N->getOperand(2);
17102
17103 bool NegA = (A.getOpcode() == ISD::FNEG);
17104 bool NegB = (B.getOpcode() == ISD::FNEG);
17105 bool NegC = (C.getOpcode() == ISD::FNEG);
17106
Michael Liaof6c24ee2012-08-10 14:39:24 +000017107 // Negative multiplication when NegA xor NegB
17108 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017109 if (NegA)
17110 A = A.getOperand(0);
17111 if (NegB)
17112 B = B.getOperand(0);
17113 if (NegC)
17114 C = C.getOperand(0);
17115
17116 unsigned Opcode;
17117 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017118 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017119 else
Craig Topperbf404372012-08-31 15:40:30 +000017120 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17121
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017122 return DAG.getNode(Opcode, dl, VT, A, B, C);
17123}
17124
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017125static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017126 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017127 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017128 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17129 // (and (i32 x86isd::setcc_carry), 1)
17130 // This eliminates the zext. This transformation is necessary because
17131 // ISD::SETCC is always legalized to i8.
17132 DebugLoc dl = N->getDebugLoc();
17133 SDValue N0 = N->getOperand(0);
17134 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017135
Evan Cheng2e489c42009-12-16 00:53:11 +000017136 if (N0.getOpcode() == ISD::AND &&
17137 N0.hasOneUse() &&
17138 N0.getOperand(0).hasOneUse()) {
17139 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017140 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17141 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17142 if (!C || C->getZExtValue() != 1)
17143 return SDValue();
17144 return DAG.getNode(ISD::AND, dl, VT,
17145 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17146 N00.getOperand(0), N00.getOperand(1)),
17147 DAG.getConstant(1, VT));
17148 }
17149 }
17150
Craig Topper5a529e42013-01-18 06:44:29 +000017151 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017152 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17153 if (R.getNode())
17154 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017155 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017156
Evan Cheng2e489c42009-12-16 00:53:11 +000017157 return SDValue();
17158}
17159
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017160// Optimize x == -y --> x+y == 0
17161// x != -y --> x+y != 0
17162static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17163 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17164 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017165 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017166
17167 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17169 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17170 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17171 LHS.getValueType(), RHS, LHS.getOperand(1));
17172 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17173 addV, DAG.getConstant(0, addV.getValueType()), CC);
17174 }
17175 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17177 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17178 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17179 RHS.getValueType(), LHS, RHS.getOperand(1));
17180 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17181 addV, DAG.getConstant(0, addV.getValueType()), CC);
17182 }
17183 return SDValue();
17184}
17185
Eric Christophere187e252013-01-31 00:50:48 +000017186// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17187// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017188// an all-ones bit which is more useful than 0/1 in some cases.
17189static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17190 return DAG.getNode(ISD::AND, DL, MVT::i8,
17191 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17192 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17193 DAG.getConstant(1, MVT::i8));
17194}
17195
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017196// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017197static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17198 TargetLowering::DAGCombinerInfo &DCI,
17199 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017200 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017201 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17202 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017203
Shuxin Yanga5526a92012-10-31 23:11:48 +000017204 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017205 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017206 // materializing "setb reg".
17207 //
17208 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17209 // cannot take an immediate as its first operand.
17210 //
Eric Christophere187e252013-01-31 00:50:48 +000017211 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017212 EFLAGS.getValueType().isInteger() &&
17213 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17214 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17215 EFLAGS.getNode()->getVTList(),
17216 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17217 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17218 return MaterializeSETB(DL, NewEFLAGS, DAG);
17219 }
17220 }
17221
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017222 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17223 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17224 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017225 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017226 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017227
Michael Liao2a33cec2012-08-10 19:58:13 +000017228 SDValue Flags;
17229
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017230 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17231 if (Flags.getNode()) {
17232 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17233 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17234 }
17235
Michael Liao2a33cec2012-08-10 19:58:13 +000017236 return SDValue();
17237}
17238
17239// Optimize branch condition evaluation.
17240//
17241static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17242 TargetLowering::DAGCombinerInfo &DCI,
17243 const X86Subtarget *Subtarget) {
17244 DebugLoc DL = N->getDebugLoc();
17245 SDValue Chain = N->getOperand(0);
17246 SDValue Dest = N->getOperand(1);
17247 SDValue EFLAGS = N->getOperand(3);
17248 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17249
17250 SDValue Flags;
17251
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017252 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17253 if (Flags.getNode()) {
17254 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17255 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17256 Flags);
17257 }
17258
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017259 return SDValue();
17260}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017261
Benjamin Kramer1396c402011-06-18 11:09:41 +000017262static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17263 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017264 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017265 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017266
17267 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017268 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017269 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017270 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017271 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17272 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17273 }
17274
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017275 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17276 // a 32-bit target where SSE doesn't support i64->FP operations.
17277 if (Op0.getOpcode() == ISD::LOAD) {
17278 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17279 EVT VT = Ld->getValueType(0);
17280 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17281 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17282 !XTLI->getSubtarget()->is64Bit() &&
17283 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017284 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17285 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017286 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17287 return FILDChain;
17288 }
17289 }
17290 return SDValue();
17291}
17292
Chris Lattner23a01992010-12-20 01:37:09 +000017293// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17294static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17295 X86TargetLowering::DAGCombinerInfo &DCI) {
17296 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17297 // the result is either zero or one (depending on the input carry bit).
17298 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17299 if (X86::isZeroNode(N->getOperand(0)) &&
17300 X86::isZeroNode(N->getOperand(1)) &&
17301 // We don't have a good way to replace an EFLAGS use, so only do this when
17302 // dead right now.
17303 SDValue(N, 1).use_empty()) {
17304 DebugLoc DL = N->getDebugLoc();
17305 EVT VT = N->getValueType(0);
17306 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17307 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17308 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17309 DAG.getConstant(X86::COND_B,MVT::i8),
17310 N->getOperand(2)),
17311 DAG.getConstant(1, VT));
17312 return DCI.CombineTo(N, Res1, CarryOut);
17313 }
17314
17315 return SDValue();
17316}
17317
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017318// fold (add Y, (sete X, 0)) -> adc 0, Y
17319// (add Y, (setne X, 0)) -> sbb -1, Y
17320// (sub (sete X, 0), Y) -> sbb 0, Y
17321// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017322static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017323 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017324
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017325 // Look through ZExts.
17326 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17327 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17328 return SDValue();
17329
17330 SDValue SetCC = Ext.getOperand(0);
17331 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17332 return SDValue();
17333
17334 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17335 if (CC != X86::COND_E && CC != X86::COND_NE)
17336 return SDValue();
17337
17338 SDValue Cmp = SetCC.getOperand(1);
17339 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017340 !X86::isZeroNode(Cmp.getOperand(1)) ||
17341 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017342 return SDValue();
17343
17344 SDValue CmpOp0 = Cmp.getOperand(0);
17345 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17346 DAG.getConstant(1, CmpOp0.getValueType()));
17347
17348 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17349 if (CC == X86::COND_NE)
17350 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17351 DL, OtherVal.getValueType(), OtherVal,
17352 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17353 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17354 DL, OtherVal.getValueType(), OtherVal,
17355 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17356}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017357
Craig Topper54f952a2011-11-19 09:02:40 +000017358/// PerformADDCombine - Do target-specific dag combines on integer adds.
17359static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17360 const X86Subtarget *Subtarget) {
17361 EVT VT = N->getValueType(0);
17362 SDValue Op0 = N->getOperand(0);
17363 SDValue Op1 = N->getOperand(1);
17364
17365 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017366 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017367 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017368 isHorizontalBinOp(Op0, Op1, true))
17369 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17370
17371 return OptimizeConditionalInDecrement(N, DAG);
17372}
17373
17374static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17375 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017376 SDValue Op0 = N->getOperand(0);
17377 SDValue Op1 = N->getOperand(1);
17378
17379 // X86 can't encode an immediate LHS of a sub. See if we can push the
17380 // negation into a preceding instruction.
17381 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017382 // If the RHS of the sub is a XOR with one use and a constant, invert the
17383 // immediate. Then add one to the LHS of the sub so we can turn
17384 // X-Y -> X+~Y+1, saving one register.
17385 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17386 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017387 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017388 EVT VT = Op0.getValueType();
17389 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17390 Op1.getOperand(0),
17391 DAG.getConstant(~XorC, VT));
17392 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017393 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017394 }
17395 }
17396
Craig Topper54f952a2011-11-19 09:02:40 +000017397 // Try to synthesize horizontal adds from adds of shuffles.
17398 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017399 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017400 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017401 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017402 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17403
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017404 return OptimizeConditionalInDecrement(N, DAG);
17405}
17406
Michael Liaod9d09602012-10-23 17:34:00 +000017407/// performVZEXTCombine - Performs build vector combines
17408static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17409 TargetLowering::DAGCombinerInfo &DCI,
17410 const X86Subtarget *Subtarget) {
17411 // (vzext (bitcast (vzext (x)) -> (vzext x)
17412 SDValue In = N->getOperand(0);
17413 while (In.getOpcode() == ISD::BITCAST)
17414 In = In.getOperand(0);
17415
17416 if (In.getOpcode() != X86ISD::VZEXT)
17417 return SDValue();
17418
17419 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17420}
17421
Dan Gohman475871a2008-07-27 21:46:04 +000017422SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017423 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017424 SelectionDAG &DAG = DCI.DAG;
17425 switch (N->getOpcode()) {
17426 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017427 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017428 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017429 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017430 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017431 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017432 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17433 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017434 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017435 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017436 case ISD::SHL:
17437 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017438 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017439 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017440 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017441 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017442 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017443 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017444 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017445 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17446 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017447 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017448 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017449 case X86ISD::FMIN:
17450 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017451 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017452 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017453 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017454 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017455 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017456 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017457 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017458 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017459 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017460 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017461 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017462 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017463 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017464 case X86ISD::UNPCKH:
17465 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017466 case X86ISD::MOVHLPS:
17467 case X86ISD::MOVLHPS:
17468 case X86ISD::PSHUFD:
17469 case X86ISD::PSHUFHW:
17470 case X86ISD::PSHUFLW:
17471 case X86ISD::MOVSS:
17472 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017473 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017474 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017475 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017476 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017477 }
17478
Dan Gohman475871a2008-07-27 21:46:04 +000017479 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017480}
17481
Evan Chenge5b51ac2010-04-17 06:13:15 +000017482/// isTypeDesirableForOp - Return true if the target has native support for
17483/// the specified value type and it is 'desirable' to use the type for the
17484/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17485/// instruction encodings are longer and some i16 instructions are slow.
17486bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17487 if (!isTypeLegal(VT))
17488 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017489 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017490 return true;
17491
17492 switch (Opc) {
17493 default:
17494 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017495 case ISD::LOAD:
17496 case ISD::SIGN_EXTEND:
17497 case ISD::ZERO_EXTEND:
17498 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017499 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017500 case ISD::SRL:
17501 case ISD::SUB:
17502 case ISD::ADD:
17503 case ISD::MUL:
17504 case ISD::AND:
17505 case ISD::OR:
17506 case ISD::XOR:
17507 return false;
17508 }
17509}
17510
17511/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017512/// beneficial for dag combiner to promote the specified node. If true, it
17513/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017514bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017515 EVT VT = Op.getValueType();
17516 if (VT != MVT::i16)
17517 return false;
17518
Evan Cheng4c26e932010-04-19 19:29:22 +000017519 bool Promote = false;
17520 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017521 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017522 default: break;
17523 case ISD::LOAD: {
17524 LoadSDNode *LD = cast<LoadSDNode>(Op);
17525 // If the non-extending load has a single use and it's not live out, then it
17526 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017527 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17528 Op.hasOneUse()*/) {
17529 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17530 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17531 // The only case where we'd want to promote LOAD (rather then it being
17532 // promoted as an operand is when it's only use is liveout.
17533 if (UI->getOpcode() != ISD::CopyToReg)
17534 return false;
17535 }
17536 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017537 Promote = true;
17538 break;
17539 }
17540 case ISD::SIGN_EXTEND:
17541 case ISD::ZERO_EXTEND:
17542 case ISD::ANY_EXTEND:
17543 Promote = true;
17544 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017545 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017546 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017547 SDValue N0 = Op.getOperand(0);
17548 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017549 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017550 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017551 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017552 break;
17553 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017554 case ISD::ADD:
17555 case ISD::MUL:
17556 case ISD::AND:
17557 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017558 case ISD::XOR:
17559 Commute = true;
17560 // fallthrough
17561 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017562 SDValue N0 = Op.getOperand(0);
17563 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017564 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017565 return false;
17566 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017567 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017568 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017569 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017570 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017571 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017572 }
17573 }
17574
17575 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017576 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017577}
17578
Evan Cheng60c07e12006-07-05 22:17:51 +000017579//===----------------------------------------------------------------------===//
17580// X86 Inline Assembly Support
17581//===----------------------------------------------------------------------===//
17582
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017583namespace {
17584 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017585 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017586 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017587
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017588 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017589 StringRef piece(*args[i]);
17590 if (!s.startswith(piece)) // Check if the piece matches.
17591 return false;
17592
17593 s = s.substr(piece.size());
17594 StringRef::size_type pos = s.find_first_not_of(" \t");
17595 if (pos == 0) // We matched a prefix.
17596 return false;
17597
17598 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017599 }
17600
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017601 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017602 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017603 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017604}
17605
Chris Lattnerb8105652009-07-20 17:51:36 +000017606bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17607 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017608
17609 std::string AsmStr = IA->getAsmString();
17610
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017611 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17612 if (!Ty || Ty->getBitWidth() % 16 != 0)
17613 return false;
17614
Chris Lattnerb8105652009-07-20 17:51:36 +000017615 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017616 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017617 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017618
17619 switch (AsmPieces.size()) {
17620 default: return false;
17621 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017622 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017623 // we will turn this bswap into something that will be lowered to logical
17624 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17625 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017626 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017627 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17628 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17629 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17630 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17631 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17632 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017633 // No need to check constraints, nothing other than the equivalent of
17634 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017635 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017636 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017637
Chris Lattnerb8105652009-07-20 17:51:36 +000017638 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017639 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017640 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017641 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17642 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017643 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017644 const std::string &ConstraintsStr = IA->getConstraintString();
17645 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017646 std::sort(AsmPieces.begin(), AsmPieces.end());
17647 if (AsmPieces.size() == 4 &&
17648 AsmPieces[0] == "~{cc}" &&
17649 AsmPieces[1] == "~{dirflag}" &&
17650 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017651 AsmPieces[3] == "~{fpsr}")
17652 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017653 }
17654 break;
17655 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017656 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017657 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017658 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17659 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17660 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017661 AsmPieces.clear();
17662 const std::string &ConstraintsStr = IA->getConstraintString();
17663 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17664 std::sort(AsmPieces.begin(), AsmPieces.end());
17665 if (AsmPieces.size() == 4 &&
17666 AsmPieces[0] == "~{cc}" &&
17667 AsmPieces[1] == "~{dirflag}" &&
17668 AsmPieces[2] == "~{flags}" &&
17669 AsmPieces[3] == "~{fpsr}")
17670 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017671 }
Evan Cheng55d42002011-01-08 01:24:27 +000017672
17673 if (CI->getType()->isIntegerTy(64)) {
17674 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17675 if (Constraints.size() >= 2 &&
17676 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17677 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17678 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017679 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17680 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17681 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017682 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017683 }
17684 }
17685 break;
17686 }
17687 return false;
17688}
17689
Chris Lattnerf4dff842006-07-11 02:54:03 +000017690/// getConstraintType - Given a constraint letter, return the type of
17691/// constraint it is for this target.
17692X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017693X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17694 if (Constraint.size() == 1) {
17695 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017696 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017697 case 'q':
17698 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017699 case 'f':
17700 case 't':
17701 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017702 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017703 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017704 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017705 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017706 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017707 case 'a':
17708 case 'b':
17709 case 'c':
17710 case 'd':
17711 case 'S':
17712 case 'D':
17713 case 'A':
17714 return C_Register;
17715 case 'I':
17716 case 'J':
17717 case 'K':
17718 case 'L':
17719 case 'M':
17720 case 'N':
17721 case 'G':
17722 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017723 case 'e':
17724 case 'Z':
17725 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017726 default:
17727 break;
17728 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017729 }
Chris Lattner4234f572007-03-25 02:14:49 +000017730 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017731}
17732
John Thompson44ab89e2010-10-29 17:29:13 +000017733/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017734/// This object must already have been set up with the operand type
17735/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017736TargetLowering::ConstraintWeight
17737 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017738 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017739 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017740 Value *CallOperandVal = info.CallOperandVal;
17741 // If we don't have a value, we can't do a match,
17742 // but allow it at the lowest weight.
17743 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017744 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017745 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017746 // Look at the constraint type.
17747 switch (*constraint) {
17748 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017749 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17750 case 'R':
17751 case 'q':
17752 case 'Q':
17753 case 'a':
17754 case 'b':
17755 case 'c':
17756 case 'd':
17757 case 'S':
17758 case 'D':
17759 case 'A':
17760 if (CallOperandVal->getType()->isIntegerTy())
17761 weight = CW_SpecificReg;
17762 break;
17763 case 'f':
17764 case 't':
17765 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017766 if (type->isFloatingPointTy())
17767 weight = CW_SpecificReg;
17768 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017769 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017770 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17771 weight = CW_SpecificReg;
17772 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017773 case 'x':
17774 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017775 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017776 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017777 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017778 break;
17779 case 'I':
17780 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17781 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017782 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017783 }
17784 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017785 case 'J':
17786 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17787 if (C->getZExtValue() <= 63)
17788 weight = CW_Constant;
17789 }
17790 break;
17791 case 'K':
17792 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17793 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17794 weight = CW_Constant;
17795 }
17796 break;
17797 case 'L':
17798 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17799 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17800 weight = CW_Constant;
17801 }
17802 break;
17803 case 'M':
17804 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17805 if (C->getZExtValue() <= 3)
17806 weight = CW_Constant;
17807 }
17808 break;
17809 case 'N':
17810 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17811 if (C->getZExtValue() <= 0xff)
17812 weight = CW_Constant;
17813 }
17814 break;
17815 case 'G':
17816 case 'C':
17817 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17818 weight = CW_Constant;
17819 }
17820 break;
17821 case 'e':
17822 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17823 if ((C->getSExtValue() >= -0x80000000LL) &&
17824 (C->getSExtValue() <= 0x7fffffffLL))
17825 weight = CW_Constant;
17826 }
17827 break;
17828 case 'Z':
17829 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17830 if (C->getZExtValue() <= 0xffffffff)
17831 weight = CW_Constant;
17832 }
17833 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017834 }
17835 return weight;
17836}
17837
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017838/// LowerXConstraint - try to replace an X constraint, which matches anything,
17839/// with another that has more specific requirements based on the type of the
17840/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017841const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017842LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017843 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17844 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017845 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017846 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017847 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017848 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017849 return "x";
17850 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017851
Chris Lattner5e764232008-04-26 23:02:14 +000017852 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017853}
17854
Chris Lattner48884cd2007-08-25 00:47:38 +000017855/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17856/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017857void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017858 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017859 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017860 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017861 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017862
Eric Christopher100c8332011-06-02 23:16:42 +000017863 // Only support length 1 constraints for now.
17864 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017865
Eric Christopher100c8332011-06-02 23:16:42 +000017866 char ConstraintLetter = Constraint[0];
17867 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017868 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017869 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017870 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017871 if (C->getZExtValue() <= 31) {
17872 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017873 break;
17874 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017875 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017876 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017877 case 'J':
17878 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017879 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017880 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17881 break;
17882 }
17883 }
17884 return;
17885 case 'K':
17886 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017887 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017888 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17889 break;
17890 }
17891 }
17892 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017893 case 'N':
17894 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017895 if (C->getZExtValue() <= 255) {
17896 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017897 break;
17898 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017899 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017900 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017901 case 'e': {
17902 // 32-bit signed value
17903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017904 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17905 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017906 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017907 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017908 break;
17909 }
17910 // FIXME gcc accepts some relocatable values here too, but only in certain
17911 // memory models; it's complicated.
17912 }
17913 return;
17914 }
17915 case 'Z': {
17916 // 32-bit unsigned value
17917 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017918 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17919 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017920 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17921 break;
17922 }
17923 }
17924 // FIXME gcc accepts some relocatable values here too, but only in certain
17925 // memory models; it's complicated.
17926 return;
17927 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017928 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017929 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017930 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017931 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017932 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017933 break;
17934 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017935
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017936 // In any sort of PIC mode addresses need to be computed at runtime by
17937 // adding in a register or some sort of table lookup. These can't
17938 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017939 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017940 return;
17941
Chris Lattnerdc43a882007-05-03 16:52:29 +000017942 // If we are in non-pic codegen mode, we allow the address of a global (with
17943 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017944 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017945 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017946
Chris Lattner49921962009-05-08 18:23:14 +000017947 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17948 while (1) {
17949 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17950 Offset += GA->getOffset();
17951 break;
17952 } else if (Op.getOpcode() == ISD::ADD) {
17953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17954 Offset += C->getZExtValue();
17955 Op = Op.getOperand(0);
17956 continue;
17957 }
17958 } else if (Op.getOpcode() == ISD::SUB) {
17959 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17960 Offset += -C->getZExtValue();
17961 Op = Op.getOperand(0);
17962 continue;
17963 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017964 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017965
Chris Lattner49921962009-05-08 18:23:14 +000017966 // Otherwise, this isn't something we can handle, reject it.
17967 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017968 }
Eric Christopherfd179292009-08-27 18:07:15 +000017969
Dan Gohman46510a72010-04-15 01:51:59 +000017970 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017971 // If we require an extra load to get this address, as in PIC mode, we
17972 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017973 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17974 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017975 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017976
Devang Patel0d881da2010-07-06 22:08:15 +000017977 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17978 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017979 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017980 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017981 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017982
Gabor Greifba36cb52008-08-28 21:40:38 +000017983 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017984 Ops.push_back(Result);
17985 return;
17986 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017987 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017988}
17989
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017990std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017991X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017992 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017993 // First, see if this is a constraint that directly corresponds to an LLVM
17994 // register class.
17995 if (Constraint.size() == 1) {
17996 // GCC Constraint Letters
17997 switch (Constraint[0]) {
17998 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017999 // TODO: Slight differences here in allocation order and leaving
18000 // RIP in the class. Do they matter any more here than they do
18001 // in the normal allocation?
18002 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18003 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018004 if (VT == MVT::i32 || VT == MVT::f32)
18005 return std::make_pair(0U, &X86::GR32RegClass);
18006 if (VT == MVT::i16)
18007 return std::make_pair(0U, &X86::GR16RegClass);
18008 if (VT == MVT::i8 || VT == MVT::i1)
18009 return std::make_pair(0U, &X86::GR8RegClass);
18010 if (VT == MVT::i64 || VT == MVT::f64)
18011 return std::make_pair(0U, &X86::GR64RegClass);
18012 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018013 }
18014 // 32-bit fallthrough
18015 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018016 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018017 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18018 if (VT == MVT::i16)
18019 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18020 if (VT == MVT::i8 || VT == MVT::i1)
18021 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18022 if (VT == MVT::i64)
18023 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018024 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018025 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018026 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018027 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018028 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018029 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018030 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018031 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018032 return std::make_pair(0U, &X86::GR32RegClass);
18033 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018034 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018035 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018036 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018037 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018038 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018039 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018040 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18041 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018042 case 'f': // FP Stack registers.
18043 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18044 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018045 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018046 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018047 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018048 return std::make_pair(0U, &X86::RFP64RegClass);
18049 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018050 case 'y': // MMX_REGS if MMX allowed.
18051 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018052 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018053 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018054 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018055 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018056 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018057 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018058
Owen Anderson825b72b2009-08-11 20:47:22 +000018059 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018060 default: break;
18061 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018062 case MVT::f32:
18063 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018064 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018065 case MVT::f64:
18066 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018067 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018068 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018069 case MVT::v16i8:
18070 case MVT::v8i16:
18071 case MVT::v4i32:
18072 case MVT::v2i64:
18073 case MVT::v4f32:
18074 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018075 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018076 // AVX types.
18077 case MVT::v32i8:
18078 case MVT::v16i16:
18079 case MVT::v8i32:
18080 case MVT::v4i64:
18081 case MVT::v8f32:
18082 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018083 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018084 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018085 break;
18086 }
18087 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018088
Chris Lattnerf76d1802006-07-31 23:26:50 +000018089 // Use the default implementation in TargetLowering to convert the register
18090 // constraint into a member of a register class.
18091 std::pair<unsigned, const TargetRegisterClass*> Res;
18092 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018093
18094 // Not found as a standard register?
18095 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018096 // Map st(0) -> st(7) -> ST0
18097 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18098 tolower(Constraint[1]) == 's' &&
18099 tolower(Constraint[2]) == 't' &&
18100 Constraint[3] == '(' &&
18101 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18102 Constraint[5] == ')' &&
18103 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018104
Chris Lattner56d77c72009-09-13 22:41:48 +000018105 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018106 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018107 return Res;
18108 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018109
Chris Lattner56d77c72009-09-13 22:41:48 +000018110 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018111 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018112 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018113 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018114 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018115 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018116
18117 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018118 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018119 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018120 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018121 return Res;
18122 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018123
Dale Johannesen330169f2008-11-13 21:52:36 +000018124 // 'A' means EAX + EDX.
18125 if (Constraint == "A") {
18126 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018127 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018128 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018129 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018130 return Res;
18131 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018132
Chris Lattnerf76d1802006-07-31 23:26:50 +000018133 // Otherwise, check to see if this is a register class of the wrong value
18134 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18135 // turn into {ax},{dx}.
18136 if (Res.second->hasType(VT))
18137 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018138
Chris Lattnerf76d1802006-07-31 23:26:50 +000018139 // All of the single-register GCC register classes map their values onto
18140 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18141 // really want an 8-bit or 32-bit register, map to the appropriate register
18142 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018143 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000018144 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018145 unsigned DestReg = 0;
18146 switch (Res.first) {
18147 default: break;
18148 case X86::AX: DestReg = X86::AL; break;
18149 case X86::DX: DestReg = X86::DL; break;
18150 case X86::CX: DestReg = X86::CL; break;
18151 case X86::BX: DestReg = X86::BL; break;
18152 }
18153 if (DestReg) {
18154 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018155 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018156 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018157 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018158 unsigned DestReg = 0;
18159 switch (Res.first) {
18160 default: break;
18161 case X86::AX: DestReg = X86::EAX; break;
18162 case X86::DX: DestReg = X86::EDX; break;
18163 case X86::CX: DestReg = X86::ECX; break;
18164 case X86::BX: DestReg = X86::EBX; break;
18165 case X86::SI: DestReg = X86::ESI; break;
18166 case X86::DI: DestReg = X86::EDI; break;
18167 case X86::BP: DestReg = X86::EBP; break;
18168 case X86::SP: DestReg = X86::ESP; break;
18169 }
18170 if (DestReg) {
18171 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018172 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018173 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018174 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018175 unsigned DestReg = 0;
18176 switch (Res.first) {
18177 default: break;
18178 case X86::AX: DestReg = X86::RAX; break;
18179 case X86::DX: DestReg = X86::RDX; break;
18180 case X86::CX: DestReg = X86::RCX; break;
18181 case X86::BX: DestReg = X86::RBX; break;
18182 case X86::SI: DestReg = X86::RSI; break;
18183 case X86::DI: DestReg = X86::RDI; break;
18184 case X86::BP: DestReg = X86::RBP; break;
18185 case X86::SP: DestReg = X86::RSP; break;
18186 }
18187 if (DestReg) {
18188 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018189 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018190 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018191 }
Craig Topperc9099502012-04-20 06:31:50 +000018192 } else if (Res.second == &X86::FR32RegClass ||
18193 Res.second == &X86::FR64RegClass ||
18194 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018195 // Handle references to XMM physical registers that got mapped into the
18196 // wrong class. This can happen with constraints like {xmm0} where the
18197 // target independent register mapper will just pick the first match it can
18198 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018199
18200 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018201 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018202 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018203 Res.second = &X86::FR64RegClass;
18204 else if (X86::VR128RegClass.hasType(VT))
18205 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018206 else if (X86::VR256RegClass.hasType(VT))
18207 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018208 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018209
Chris Lattnerf76d1802006-07-31 23:26:50 +000018210 return Res;
18211}