blob: 6daa9b65e6fb6d59725b6ef1b9a6505fd5feda64 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000019#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/VariadicFunction.h"
Evan Cheng55d42002011-01-08 01:24:27 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalAlias.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000044#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/MC/MCSymbol.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000161
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000162 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000163 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000164
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000166 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000169 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000170 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000172
Eric Christopherde5e1012011-03-11 01:05:58 +0000173 // For 64-bit since we have so many registers use the ILP scheduler, for
174 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000175 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000176 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000177 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000178 else if (Subtarget->is64Bit())
179 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000180 else
181 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000182 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000183
Preston Gurd2e2efd92012-09-04 18:22:17 +0000184 // Bypass i32 with i8 on Atom when compiling with O2
185 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000186 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000187
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000188 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000189 // Setup Windows compiler runtime calls.
190 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000191 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000192 setLibcallName(RTLIB::SREM_I64, "_allrem");
193 setLibcallName(RTLIB::UREM_I64, "_aullrem");
194 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
198 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000200
201 // The _ftol2 runtime function has an unusual calling conv, which
202 // is modeled by a special pseudo-instruction.
203 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
204 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
206 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000207 }
208
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000209 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000210 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 setUseUnderscoreSetJmp(false);
212 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 // MS runtime is weird: it exports _setjmp, but longjmp!
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(false);
217 } else {
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(true);
220 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000223 addRegisterClass(MVT::i8, &X86::GR8RegClass);
224 addRegisterClass(MVT::i16, &X86::GR16RegClass);
225 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000227 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000230
Scott Michelfdc40a02009-02-17 22:15:04 +0000231 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000233 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
237 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000238
239 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
248 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000252
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000256 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000257 // We have an algorithm for SSE2->double, and we turn this into a
258 // 64-bit FILD followed by conditional FADD for other targets.
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 // We have an algorithm for SSE2, and we turn this into a 64-bit
261 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
265 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
266 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
268 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000269
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000270 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // SSE has no i16 to fp conversion, only i32
272 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284
Dale Johannesen73328d12007-09-19 23:55:34 +0000285 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
286 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
288 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000289
Evan Cheng02568ff2006-01-30 22:13:22 +0000290 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
291 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
293 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000294
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000295 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000297 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000299 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 }
303
304 // Handle FP_TO_UINT by promoting the destination to a larger signed
305 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000313 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000314 // Since AVX is a superset of SSE3, only check for SSE here.
315 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000326 if (isTargetFTOL()) {
327 // Use the _ftol2 runtime function, which has a pseudo-instruction
328 // to handle its weird calling convention.
329 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
330 }
331
Chris Lattner399610a2006-12-05 18:22:22 +0000332 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000333 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
335 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000336 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000338 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000339 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000340 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000341 }
Chris Lattner21f66852005-12-23 05:15:23 +0000342
Dan Gohmanb00ee212008-02-18 19:34:53 +0000343 // Scalar integer divide and remainder are lowered to use operations that
344 // produce two results, to match the available instructions. This exposes
345 // the two-result form to trivial CSE, which is able to combine x/y and x%y
346 // into a single instruction.
347 //
348 // Scalar integer multiply-high is also lowered to use two-result
349 // operations, to match the available instructions. However, plain multiply
350 // (low) operations are left as Legal, as there are single-result
351 // instructions for this in x86. Using the two-result multiply instructions
352 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000353 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000354 MVT VT = IntVTs[i];
355 setOperationAction(ISD::MULHS, VT, Expand);
356 setOperationAction(ISD::MULHU, VT, Expand);
357 setOperationAction(ISD::SDIV, VT, Expand);
358 setOperationAction(ISD::UDIV, VT, Expand);
359 setOperationAction(ISD::SREM, VT, Expand);
360 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000362 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000363 setOperationAction(ISD::ADDC, VT, Custom);
364 setOperationAction(ISD::ADDE, VT, Custom);
365 setOperationAction(ISD::SUBC, VT, Custom);
366 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000368
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
370 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
371 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
372 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
378 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
379 setOperationAction(ISD::FREM , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f64 , Expand);
381 setOperationAction(ISD::FREM , MVT::f80 , Expand);
382 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chandler Carruth77821022011-12-24 12:12:34 +0000384 // Promote the i8 variants and force them on up to i32 which has a shorter
385 // encoding.
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
388 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
389 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000390 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000395 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000396 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
400 }
Craig Topper37f21672011-10-11 06:44:02 +0000401
402 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000403 // When promoting the i8 variants, force them to i32 for a shorter
404 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000405 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000406 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
408 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
411 if (Subtarget->is64Bit())
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000413 } else {
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
420 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
425
Benjamin Kramer1292c222010-12-04 20:32:23 +0000426 if (Subtarget->hasPOPCNT()) {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
428 } else {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
432 if (Subtarget->is64Bit())
433 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
434 }
435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
437 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000438
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000441 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000443 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000449 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000456 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000459 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
460 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000461 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // other SjLj exception interfaces are implemented and please don't build
463 // your own exception handling based on them.
464 // LLVM/Clang supports zero-cost DWARF exception handling.
465 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
466 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000467
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000473 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
475 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
479 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
480 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
481 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000482 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000483 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000484 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
486 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000488 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
490 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000492 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493
Craig Topper1accb7e2012-01-10 06:54:16 +0000494 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000496
Eric Christopher9a9d2752010-07-22 02:48:34 +0000497 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000498 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000499
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000500 // On X86 and X86-64, atomic operations are lowered to locked instructions.
501 // Locked instructions, in turn, have implicit fence semantics (all memory
502 // operations are flushed before issuing the locked instruction, and they
503 // are not buffered), so we can fold away the common pattern of
504 // fence-atomic-fence.
505 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000506
Mon P Wang63307c32008-05-05 19:05:59 +0000507 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000508 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000509 MVT VT = IntVTs[i];
510 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000512 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000513 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000514
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000515 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000516 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000524 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000528 }
529
Eli Friedman43f51ae2011-08-26 21:21:21 +0000530 if (Subtarget->hasCmpxchg16b()) {
531 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
532 }
533
Evan Cheng3c992d22006-03-07 02:02:57 +0000534 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000535 if (!Subtarget->isTargetDarwin() &&
536 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000537 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000539 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
542 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
543 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
544 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000545 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 setExceptionPointerRegister(X86::RAX);
547 setExceptionSelectorRegister(X86::RDX);
548 } else {
549 setExceptionPointerRegister(X86::EAX);
550 setExceptionSelectorRegister(X86::EDX);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000554
Duncan Sands4a544a72011-09-06 13:37:06 +0000555 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
556 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000557
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000559 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000728 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
729 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000730 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000731 setOperationAction(ISD::ADD , VT, Expand);
732 setOperationAction(ISD::SUB , VT, Expand);
733 setOperationAction(ISD::FADD, VT, Expand);
734 setOperationAction(ISD::FNEG, VT, Expand);
735 setOperationAction(ISD::FSUB, VT, Expand);
736 setOperationAction(ISD::MUL , VT, Expand);
737 setOperationAction(ISD::FMUL, VT, Expand);
738 setOperationAction(ISD::SDIV, VT, Expand);
739 setOperationAction(ISD::UDIV, VT, Expand);
740 setOperationAction(ISD::FDIV, VT, Expand);
741 setOperationAction(ISD::SREM, VT, Expand);
742 setOperationAction(ISD::UREM, VT, Expand);
743 setOperationAction(ISD::LOAD, VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
749 setOperationAction(ISD::FABS, VT, Expand);
750 setOperationAction(ISD::FSIN, VT, Expand);
751 setOperationAction(ISD::FCOS, VT, Expand);
752 setOperationAction(ISD::FREM, VT, Expand);
753 setOperationAction(ISD::FMA, VT, Expand);
754 setOperationAction(ISD::FPOWI, VT, Expand);
755 setOperationAction(ISD::FSQRT, VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
757 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000758 setOperationAction(ISD::FCEIL, VT, Expand);
759 setOperationAction(ISD::FTRUNC, VT, Expand);
760 setOperationAction(ISD::FRINT, VT, Expand);
761 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
763 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
764 setOperationAction(ISD::SDIVREM, VT, Expand);
765 setOperationAction(ISD::UDIVREM, VT, Expand);
766 setOperationAction(ISD::FPOW, VT, Expand);
767 setOperationAction(ISD::CTPOP, VT, Expand);
768 setOperationAction(ISD::CTTZ, VT, Expand);
769 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
770 setOperationAction(ISD::CTLZ, VT, Expand);
771 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
772 setOperationAction(ISD::SHL, VT, Expand);
773 setOperationAction(ISD::SRA, VT, Expand);
774 setOperationAction(ISD::SRL, VT, Expand);
775 setOperationAction(ISD::ROTL, VT, Expand);
776 setOperationAction(ISD::ROTR, VT, Expand);
777 setOperationAction(ISD::BSWAP, VT, Expand);
778 setOperationAction(ISD::SETCC, VT, Expand);
779 setOperationAction(ISD::FLOG, VT, Expand);
780 setOperationAction(ISD::FLOG2, VT, Expand);
781 setOperationAction(ISD::FLOG10, VT, Expand);
782 setOperationAction(ISD::FEXP, VT, Expand);
783 setOperationAction(ISD::FEXP2, VT, Expand);
784 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
785 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
786 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
787 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
788 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
789 setOperationAction(ISD::TRUNCATE, VT, Expand);
790 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
791 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
792 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
793 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000794 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
795 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000796 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000797 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000798 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
799 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
800 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000801 }
802
Evan Chengc7ce29b2009-02-13 22:36:38 +0000803 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
804 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000806 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000807 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Dale Johannesen0488fb62010-09-30 23:57:10 +0000810 // MMX-sized vectors (other than x86mmx) are expected to be expanded
811 // into smaller operations.
812 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
813 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
814 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
815 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
816 setOperationAction(ISD::AND, MVT::v8i8, Expand);
817 setOperationAction(ISD::AND, MVT::v4i16, Expand);
818 setOperationAction(ISD::AND, MVT::v2i32, Expand);
819 setOperationAction(ISD::AND, MVT::v1i64, Expand);
820 setOperationAction(ISD::OR, MVT::v8i8, Expand);
821 setOperationAction(ISD::OR, MVT::v4i16, Expand);
822 setOperationAction(ISD::OR, MVT::v2i32, Expand);
823 setOperationAction(ISD::OR, MVT::v1i64, Expand);
824 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
831 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
833 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
834 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
835 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
836 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000837 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
838 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
839 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
840 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
846 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
847 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
848 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
849 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
850 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000851 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
853 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
854 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
855 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
856 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000857 }
858
Craig Topper1accb7e2012-01-10 06:54:16 +0000859 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000861
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
863 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000864 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
865 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
866 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
867 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
870 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
871 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
872 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000873 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
875 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
876 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
877 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
878 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
879 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
880 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
881 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
882 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
883 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
884 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
885 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000886 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000887
Nadav Rotem354efd82011-09-18 14:57:03 +0000888 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000889 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
890 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
891 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
894 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000900 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000901 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000902 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000904 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000905 // Do not attempt to custom lower non-128-bit vectors
906 if (!VT.is128BitVector())
907 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000911 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
914 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
917 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
918 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000919
Nate Begemancdd1eec2008-02-12 22:51:28 +0000920 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000925 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000926 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000927 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000928
929 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000930 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000931 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000932
Craig Topper0d1f1762012-08-12 00:34:56 +0000933 setOperationAction(ISD::AND, VT, Promote);
934 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
935 setOperationAction(ISD::OR, VT, Promote);
936 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
937 setOperationAction(ISD::XOR, VT, Promote);
938 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
939 setOperationAction(ISD::LOAD, VT, Promote);
940 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
941 setOperationAction(ISD::SELECT, VT, Promote);
942 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000943 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000946
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
949 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
950 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
951 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
954 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000955
Michael Liaoa7554632012-10-23 17:36:08 +0000956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
957 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000958 // As there is no 64-bit GPR available, we need build a special custom
959 // sequence to convert from v2i32 to v2f32.
960 if (!Subtarget->is64Bit())
961 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000962
Michael Liao9d796db2012-10-10 16:32:15 +0000963 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000964 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000965
Michael Liaob8150d82012-09-10 18:33:51 +0000966 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000967 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000968
Craig Topperd0a31172012-01-10 06:37:29 +0000969 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000970 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
971 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
972 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
973 setOperationAction(ISD::FRINT, MVT::f32, Legal);
974 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
975 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
976 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
977 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
978 setOperationAction(ISD::FRINT, MVT::f64, Legal);
979 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
980
Craig Topper12fb5c62012-09-08 17:42:27 +0000981 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000982 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
983 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
984 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
985 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000986 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000987 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
988 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
989 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
990 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000991
Nate Begeman14d12ca2008-02-11 04:19:36 +0000992 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000994
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000995 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
999 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // i8 and i16 vectors are custom , because the source register and source
1002 // source memory operand types are not the same width. f32 vectors are
1003 // custom since the immediate controlling the insert encodes additional
1004 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1008 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1013 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001014
Pete Coopera77214a2011-11-14 19:38:42 +00001015 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001016 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001018 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001020 }
1021 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001022
Craig Topper1accb7e2012-01-10 06:54:16 +00001023 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001024 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001025 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001026
Nadav Rotem43012222011-05-11 08:12:09 +00001027 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001028 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001029
Nadav Rotem43012222011-05-11 08:12:09 +00001030 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001031 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001032
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001033 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1035 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1036
1037 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1038 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1039
1040 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1041 } else {
1042 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1043 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1044
1045 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1046 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1047
1048 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1049 }
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001050 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1051 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001052 }
1053
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001054 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001055 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1058 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1059 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1060 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001061
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1064 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001065
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1068 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1069 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1070 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001071 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001072 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1073 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1074 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1075 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001077 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001078
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1081 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1082 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1083 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001084 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001085 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1086 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1087 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1088 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001090 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001091
Michael Liaobedcbd42012-10-16 18:14:11 +00001092 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001093 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001094
1095 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1096
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001097 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1098 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001099 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001100
Michael Liaoa7554632012-10-23 17:36:08 +00001101 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1102 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1103 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1104
Michael Liaob8150d82012-09-10 18:33:51 +00001105 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1106
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001107 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1108 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1109
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001110 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1111 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1112
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001113 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001114 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001115
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001116 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1117
Duncan Sands28b77e92011-09-06 19:07:46 +00001118 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1119 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1120 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1121 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001122
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001123 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1124 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1125 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1126
Craig Topperaaa643c2011-11-09 07:28:55 +00001127 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1128 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1129 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1130 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001131
Nadav Rotem0509db22012-12-28 05:45:24 +00001132 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1133 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1134 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1135 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1136 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1137 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001138
Craig Topperbf404372012-08-31 15:40:30 +00001139 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001140 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1141 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1142 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1143 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1144 setOperationAction(ISD::FMA, MVT::f32, Legal);
1145 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001146 }
Craig Topper880ef452012-08-11 22:34:26 +00001147
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001148 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001149 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1150 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1151 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1152 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001153
Craig Topperaaa643c2011-11-09 07:28:55 +00001154 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1155 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1156 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1157 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001158
Craig Topperaaa643c2011-11-09 07:28:55 +00001159 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1160 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1161 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001162 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001163
1164 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001165
1166 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1167 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1168
1169 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1170 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1171
1172 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001173
1174 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001175 } else {
1176 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1177 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1178 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1179 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1180
1181 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1182 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1183 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1184 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1185
1186 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1187 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1188 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1189 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001190
1191 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1192 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1193
1194 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1195 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1196
1197 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001198 }
Craig Topper13894fa2011-08-24 06:14:18 +00001199
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001200 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001201 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1202 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001203 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001204
1205 // Extract subvector is special because the value type
1206 // (result) is 128-bit but the source is 256-bit wide.
1207 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001208 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001209
1210 // Do not attempt to custom lower other non-256-bit vectors
1211 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001212 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001213
Craig Topper0d1f1762012-08-12 00:34:56 +00001214 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1215 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1216 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1217 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1218 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1219 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1220 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001221 }
1222
David Greene54d8eba2011-01-27 22:38:56 +00001223 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001224 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001225 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001226
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001227 // Do not attempt to promote non-256-bit vectors
1228 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001229 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001230
Craig Topper0d1f1762012-08-12 00:34:56 +00001231 setOperationAction(ISD::AND, VT, Promote);
1232 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1233 setOperationAction(ISD::OR, VT, Promote);
1234 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1235 setOperationAction(ISD::XOR, VT, Promote);
1236 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1237 setOperationAction(ISD::LOAD, VT, Promote);
1238 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1239 setOperationAction(ISD::SELECT, VT, Promote);
1240 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001241 }
David Greene9b9838d2009-06-29 16:47:10 +00001242 }
1243
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001244 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1245 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001246 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1247 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001248 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1249 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001250 }
1251
Evan Cheng6be2c582006-04-05 23:38:46 +00001252 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001254 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001255
Eli Friedman962f5492010-06-02 19:35:46 +00001256 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1257 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001258 //
Eli Friedman962f5492010-06-02 19:35:46 +00001259 // FIXME: We really should do custom legalization for addition and
1260 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1261 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001262 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1263 // Add/Sub/Mul with overflow operations are custom lowered.
1264 MVT VT = IntVTs[i];
1265 setOperationAction(ISD::SADDO, VT, Custom);
1266 setOperationAction(ISD::UADDO, VT, Custom);
1267 setOperationAction(ISD::SSUBO, VT, Custom);
1268 setOperationAction(ISD::USUBO, VT, Custom);
1269 setOperationAction(ISD::SMULO, VT, Custom);
1270 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001271 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001272
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001273 // There are no 8-bit 3-address imul/mul instructions
1274 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1275 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001276
Evan Chengd54f2d52009-03-31 19:38:51 +00001277 if (!Subtarget->is64Bit()) {
1278 // These libcalls are not available in 32-bit.
1279 setLibcallName(RTLIB::SHL_I128, 0);
1280 setLibcallName(RTLIB::SRL_I128, 0);
1281 setLibcallName(RTLIB::SRA_I128, 0);
1282 }
1283
Evan Cheng206ee9d2006-07-07 08:33:52 +00001284 // We have target-specific dag combine patterns for the following nodes:
1285 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001286 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001287 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001288 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001289 setTargetDAGCombine(ISD::SHL);
1290 setTargetDAGCombine(ISD::SRA);
1291 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001292 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001293 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001294 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001295 setTargetDAGCombine(ISD::FADD);
1296 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001297 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001298 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001299 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001300 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001301 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001302 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001303 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001304 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001305 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001306 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001307 if (Subtarget->is64Bit())
1308 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001309 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001310
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001311 computeRegisterProperties();
1312
Evan Cheng05219282011-01-06 06:52:41 +00001313 // On Darwin, -Os means optimize for size without hurting performance,
1314 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001315 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001316 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001317 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001318 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1319 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1320 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001321 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001322 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001323
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001324 // Predictable cmov don't hurt on atom because it's in-order.
1325 predictableSelectIsExpensive = !Subtarget->isAtom();
1326
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001327 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001328}
1329
Duncan Sands28b77e92011-09-06 19:07:46 +00001330EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1331 if (!VT.isVector()) return MVT::i8;
1332 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001333}
1334
Evan Cheng29286502008-01-23 23:17:41 +00001335/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1336/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001337static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001338 if (MaxAlign == 16)
1339 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001340 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001341 if (VTy->getBitWidth() == 128)
1342 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001343 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001344 unsigned EltAlign = 0;
1345 getMaxByValAlign(ATy->getElementType(), EltAlign);
1346 if (EltAlign > MaxAlign)
1347 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001348 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001349 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1350 unsigned EltAlign = 0;
1351 getMaxByValAlign(STy->getElementType(i), EltAlign);
1352 if (EltAlign > MaxAlign)
1353 MaxAlign = EltAlign;
1354 if (MaxAlign == 16)
1355 break;
1356 }
1357 }
Evan Cheng29286502008-01-23 23:17:41 +00001358}
1359
1360/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1361/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001362/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1363/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001364unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001365 if (Subtarget->is64Bit()) {
1366 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001367 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001368 if (TyAlign > 8)
1369 return TyAlign;
1370 return 8;
1371 }
1372
Evan Cheng29286502008-01-23 23:17:41 +00001373 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001374 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001375 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001376 return Align;
1377}
Chris Lattner2b02a442007-02-25 08:29:00 +00001378
Evan Chengf0df0312008-05-15 08:39:06 +00001379/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001380/// and store operations as a result of memset, memcpy, and memmove
1381/// lowering. If DstAlign is zero that means it's safe to destination
1382/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1383/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001384/// probably because the source does not need to be loaded. If 'IsMemset' is
1385/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1386/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1387/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001388/// It returns EVT::Other if the type should be determined using generic
1389/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001390EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001391X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1392 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001393 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001394 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001395 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001396 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001397 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001398 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1399 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001400 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001401 (Subtarget->isUnalignedMemAccessFast() ||
1402 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001403 (SrcAlign == 0 || SrcAlign >= 16)))) {
1404 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001405 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001406 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001407 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001408 return MVT::v8f32;
1409 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001410 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001411 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001412 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001413 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001414 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001415 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001416 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001417 // Do not use f64 to lower memcpy if source is string constant. It's
1418 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001419 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001420 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001421 }
Evan Chengf0df0312008-05-15 08:39:06 +00001422 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 return MVT::i64;
1424 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001425}
1426
Evan Cheng7d342672012-12-12 01:32:07 +00001427bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001428 if (VT == MVT::f32)
1429 return X86ScalarSSEf32;
1430 else if (VT == MVT::f64)
1431 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001432 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001433}
1434
Evan Cheng376642e2012-12-10 23:21:26 +00001435bool
1436X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1437 if (Fast)
1438 *Fast = Subtarget->isUnalignedMemAccessFast();
1439 return true;
1440}
1441
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001442/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1443/// current function. The returned value is a member of the
1444/// MachineJumpTableInfo::JTEntryKind enum.
1445unsigned X86TargetLowering::getJumpTableEncoding() const {
1446 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1447 // symbol.
1448 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1449 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001450 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001451
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001452 // Otherwise, use the normal jump table encoding heuristics.
1453 return TargetLowering::getJumpTableEncoding();
1454}
1455
Chris Lattnerc64daab2010-01-26 05:02:42 +00001456const MCExpr *
1457X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1458 const MachineBasicBlock *MBB,
1459 unsigned uid,MCContext &Ctx) const{
1460 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1461 Subtarget->isPICStyleGOT());
1462 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1463 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001464 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1465 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001466}
1467
Evan Chengcc415862007-11-09 01:32:10 +00001468/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1469/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001470SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001471 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001472 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001473 // This doesn't have DebugLoc associated with it, but is not really the
1474 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001475 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001476 return Table;
1477}
1478
Chris Lattner589c6f62010-01-26 06:28:43 +00001479/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1480/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1481/// MCExpr.
1482const MCExpr *X86TargetLowering::
1483getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1484 MCContext &Ctx) const {
1485 // X86-64 uses RIP relative addressing based on the jump table label.
1486 if (Subtarget->isPICStyleRIPRel())
1487 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1488
1489 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001490 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001491}
1492
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001493// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001494std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001495X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001496 const TargetRegisterClass *RRC = 0;
1497 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001498 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001499 default:
1500 return TargetLowering::findRepresentativeClass(VT);
1501 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001502 RRC = Subtarget->is64Bit() ?
1503 (const TargetRegisterClass*)&X86::GR64RegClass :
1504 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001505 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001506 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001507 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001508 break;
1509 case MVT::f32: case MVT::f64:
1510 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1511 case MVT::v4f32: case MVT::v2f64:
1512 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1513 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001514 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001515 break;
1516 }
1517 return std::make_pair(RRC, Cost);
1518}
1519
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001520bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1521 unsigned &Offset) const {
1522 if (!Subtarget->isTargetLinux())
1523 return false;
1524
1525 if (Subtarget->is64Bit()) {
1526 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1527 Offset = 0x28;
1528 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1529 AddressSpace = 256;
1530 else
1531 AddressSpace = 257;
1532 } else {
1533 // %gs:0x14 on i386
1534 Offset = 0x14;
1535 AddressSpace = 256;
1536 }
1537 return true;
1538}
1539
Chris Lattner2b02a442007-02-25 08:29:00 +00001540//===----------------------------------------------------------------------===//
1541// Return Value Calling Convention Implementation
1542//===----------------------------------------------------------------------===//
1543
Chris Lattner59ed56b2007-02-28 04:55:35 +00001544#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001545
Michael J. Spencerec38de22010-10-10 22:04:20 +00001546bool
Eric Christopher471e4222011-06-08 23:55:35 +00001547X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001548 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001549 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001550 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001551 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001552 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001553 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001554 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001555}
1556
Dan Gohman98ca4f22009-08-05 01:29:28 +00001557SDValue
1558X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001559 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001561 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001562 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001563 MachineFunction &MF = DAG.getMachineFunction();
1564 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner9774c912007-02-27 05:28:59 +00001566 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001567 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 RVLocs, *DAG.getContext());
1569 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Evan Chengdcea1632010-02-04 02:40:39 +00001571 // Add the regs to the liveout set for the function.
1572 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1573 for (unsigned i = 0; i != RVLocs.size(); ++i)
1574 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1575 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Dan Gohman475871a2008-07-27 21:46:04 +00001577 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001578
Dan Gohman475871a2008-07-27 21:46:04 +00001579 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001580 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1581 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001582 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1583 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001585 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001586 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1587 CCValAssign &VA = RVLocs[i];
1588 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001589 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001590 EVT ValVT = ValToCopy.getValueType();
1591
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001592 // Promote values to the appropriate types
1593 if (VA.getLocInfo() == CCValAssign::SExt)
1594 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1595 else if (VA.getLocInfo() == CCValAssign::ZExt)
1596 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1597 else if (VA.getLocInfo() == CCValAssign::AExt)
1598 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1599 else if (VA.getLocInfo() == CCValAssign::BCvt)
1600 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1601
Dale Johannesenc4510512010-09-24 19:05:48 +00001602 // If this is x86-64, and we disabled SSE, we can't return FP values,
1603 // or SSE or MMX vectors.
1604 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1605 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001606 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001607 report_fatal_error("SSE register return with SSE disabled");
1608 }
1609 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1610 // llvm-gcc has never done it right and no one has noticed, so this
1611 // should be OK for now.
1612 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001613 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001614 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001615
Chris Lattner447ff682008-03-11 03:23:40 +00001616 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1617 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001618 if (VA.getLocReg() == X86::ST0 ||
1619 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001620 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1621 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001622 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001623 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001624 RetOps.push_back(ValToCopy);
1625 // Don't emit a copytoreg.
1626 continue;
1627 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001628
Evan Cheng242b38b2009-02-23 09:03:22 +00001629 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1630 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001631 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001632 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001633 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001635 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1636 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001637 // If we don't have SSE2 available, convert to v4f32 so the generated
1638 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001639 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001641 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001642 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001643 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001644
Dale Johannesendd64c412009-02-04 00:33:20 +00001645 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001646 Flag = Chain.getValue(1);
1647 }
Dan Gohman61a92132008-04-21 23:59:07 +00001648
Eli Benderskya5597f02013-01-25 22:07:43 +00001649 // The x86-64 ABIs require that for returning structs by value we copy
1650 // the sret argument into %rax/%eax (depending on ABI) for the return.
1651 // We saved the argument into a virtual register in the entry block,
1652 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001653 if (Subtarget->is64Bit() &&
1654 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1655 MachineFunction &MF = DAG.getMachineFunction();
1656 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1657 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001658 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001659 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001660 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001661
Eli Benderskya5597f02013-01-25 22:07:43 +00001662 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1663 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001664 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001665
Eli Benderskya5597f02013-01-25 22:07:43 +00001666 // RAX/EAX now acts like a return value.
1667 MRI.addLiveOut(RetValReg);
Dan Gohman61a92132008-04-21 23:59:07 +00001668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001669
Chris Lattner447ff682008-03-11 03:23:40 +00001670 RetOps[0] = Chain; // Update chain.
1671
1672 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001673 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001674 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001675
1676 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001678}
1679
Evan Chengbf010eb2012-04-10 01:51:00 +00001680bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001681 if (N->getNumValues() != 1)
1682 return false;
1683 if (!N->hasNUsesOfValue(1, 0))
1684 return false;
1685
Evan Chengbf010eb2012-04-10 01:51:00 +00001686 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001687 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001688 if (Copy->getOpcode() == ISD::CopyToReg) {
1689 // If the copy has a glue operand, we conservatively assume it isn't safe to
1690 // perform a tail call.
1691 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1692 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001693 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001694 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001695 return false;
1696
Evan Cheng1bf891a2010-12-01 22:59:46 +00001697 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001698 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001699 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001700 if (UI->getOpcode() != X86ISD::RET_FLAG)
1701 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001702 HasRet = true;
1703 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001704
Evan Chengbf010eb2012-04-10 01:51:00 +00001705 if (!HasRet)
1706 return false;
1707
1708 Chain = TCChain;
1709 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001710}
1711
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001712MVT
1713X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001714 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001715 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001716 // TODO: Is this also valid on 32-bit?
1717 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001718 ReturnMVT = MVT::i8;
1719 else
1720 ReturnMVT = MVT::i32;
1721
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001722 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001723 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001724}
1725
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726/// LowerCallResult - Lower the result values of a call into the
1727/// appropriate copies out of appropriate physical registers.
1728///
1729SDValue
1730X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001731 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 const SmallVectorImpl<ISD::InputArg> &Ins,
1733 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001734 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001735
Chris Lattnere32bbf62007-02-28 07:09:55 +00001736 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001737 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001738 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001739 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001740 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001742
Chris Lattner3085e152007-02-25 08:59:22 +00001743 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001744 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001745 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001747
Torok Edwin3f142c32009-02-01 18:15:56 +00001748 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001750 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001751 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001752 }
1753
Evan Cheng79fb3b42009-02-20 20:43:02 +00001754 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001755
1756 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001757 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001758 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001759 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001760 // instead.
1761 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1762 // If we prefer to use the value in xmm registers, copy it out as f80 and
1763 // use a truncate to move it from fp stack reg to xmm reg.
1764 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001765 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001766 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1767 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001768 Val = Chain.getValue(0);
1769
1770 // Round the f80 to the right size, which also moves it to the appropriate
1771 // xmm register.
1772 if (CopyVT != VA.getValVT())
1773 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1774 // This truncation won't change the value.
1775 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001776 } else {
1777 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1778 CopyVT, InFlag).getValue(1);
1779 Val = Chain.getValue(0);
1780 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001781 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001783 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001784
Dan Gohman98ca4f22009-08-05 01:29:28 +00001785 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001786}
1787
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001788//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001789// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001790//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001791// StdCall calling convention seems to be standard for many Windows' API
1792// routines and around. It differs from C calling convention just a little:
1793// callee should clean up the stack, not caller. Symbols should be also
1794// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001795// For info on fast calling convention see Fast Calling Convention (tail call)
1796// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001797
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001799/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001800enum StructReturnType {
1801 NotStructReturn,
1802 RegStructReturn,
1803 StackStructReturn
1804};
1805static StructReturnType
1806callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001808 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001809
Rafael Espindola1cee7102012-07-25 13:41:10 +00001810 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1811 if (!Flags.isSRet())
1812 return NotStructReturn;
1813 if (Flags.isInReg())
1814 return RegStructReturn;
1815 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001816}
1817
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001818/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001819/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001820static StructReturnType
1821argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001823 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001824
Rafael Espindola1cee7102012-07-25 13:41:10 +00001825 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1826 if (!Flags.isSRet())
1827 return NotStructReturn;
1828 if (Flags.isInReg())
1829 return RegStructReturn;
1830 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001831}
1832
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001833/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1834/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001835/// the specific parameter attribute. The copy will be passed as a byval
1836/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001837static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001838CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001839 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1840 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001841 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001842
Dale Johannesendd64c412009-02-04 00:33:20 +00001843 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001844 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001845 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001846}
1847
Chris Lattner29689432010-03-11 00:22:57 +00001848/// IsTailCallConvention - Return true if the calling convention is one that
1849/// supports tail call optimization.
1850static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001851 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1852 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001853}
1854
Evan Cheng485fafc2011-03-21 01:19:09 +00001855bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001856 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001857 return false;
1858
1859 CallSite CS(CI);
1860 CallingConv::ID CalleeCC = CS.getCallingConv();
1861 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1862 return false;
1863
1864 return true;
1865}
1866
Evan Cheng0c439eb2010-01-27 00:07:07 +00001867/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1868/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001869static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1870 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001871 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001872}
1873
Dan Gohman98ca4f22009-08-05 01:29:28 +00001874SDValue
1875X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001876 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 const SmallVectorImpl<ISD::InputArg> &Ins,
1878 DebugLoc dl, SelectionDAG &DAG,
1879 const CCValAssign &VA,
1880 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001881 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001882 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001883 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001884 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1885 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001886 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001887 EVT ValVT;
1888
1889 // If value is passed by pointer we have address passed instead of the value
1890 // itself.
1891 if (VA.getLocInfo() == CCValAssign::Indirect)
1892 ValVT = VA.getLocVT();
1893 else
1894 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001895
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001896 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001897 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001898 // In case of tail call optimization mark all arguments mutable. Since they
1899 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001900 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001901 unsigned Bytes = Flags.getByValSize();
1902 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1903 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001904 return DAG.getFrameIndex(FI, getPointerTy());
1905 } else {
1906 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001907 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001908 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1909 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001910 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001911 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001912 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001913}
1914
Dan Gohman475871a2008-07-27 21:46:04 +00001915SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001917 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 bool isVarArg,
1919 const SmallVectorImpl<ISD::InputArg> &Ins,
1920 DebugLoc dl,
1921 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001922 SmallVectorImpl<SDValue> &InVals)
1923 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001924 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001925 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001926
Gordon Henriksen86737662008-01-05 16:56:59 +00001927 const Function* Fn = MF.getFunction();
1928 if (Fn->hasExternalLinkage() &&
1929 Subtarget->isTargetCygMing() &&
1930 Fn->getName() == "main")
1931 FuncInfo->setForceFramePointer(true);
1932
Evan Cheng1bc78042006-04-26 01:20:17 +00001933 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001935 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001936 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001937
Chris Lattner29689432010-03-11 00:22:57 +00001938 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001939 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001940
Chris Lattner638402b2007-02-28 07:00:42 +00001941 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001942 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001943 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001944 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001945
1946 // Allocate shadow area for Win64
1947 if (IsWin64) {
1948 CCInfo.AllocateStack(32, 8);
1949 }
1950
Duncan Sands45907662010-10-31 13:21:44 +00001951 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001952
Chris Lattnerf39f7712007-02-28 05:46:49 +00001953 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001954 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001955 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1956 CCValAssign &VA = ArgLocs[i];
1957 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1958 // places.
1959 assert(VA.getValNo() != LastVal &&
1960 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001961 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001962 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001963
Chris Lattnerf39f7712007-02-28 05:46:49 +00001964 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001965 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001966 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001967 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001968 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001970 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001972 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001974 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001975 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001976 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001977 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001978 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001979 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001980 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001981 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001982 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001983
Devang Patel68e6bee2011-02-21 23:21:26 +00001984 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001986
Chris Lattnerf39f7712007-02-28 05:46:49 +00001987 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1988 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1989 // right size.
1990 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001991 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001992 DAG.getValueType(VA.getValVT()));
1993 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001994 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001995 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001996 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001997 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001998
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001999 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002000 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002001 if (RegVT.isVector())
2002 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2003 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002004 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002005 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002006 } else {
2007 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002008 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002009 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002010
2011 // If value is passed via pointer - do a load.
2012 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002013 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002014 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002015
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002017 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002018
Eli Benderskya5597f02013-01-25 22:07:43 +00002019 // The x86-64 ABIs require that for returning structs by value we copy
2020 // the sret argument into %rax/%eax (depending on ABI) for the return.
2021 // Save the argument into a virtual register so that we can access it
2022 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002023 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002024 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2025 unsigned Reg = FuncInfo->getSRetReturnReg();
2026 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002027 MVT PtrTy = getPointerTy();
2028 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002029 FuncInfo->setSRetReturnReg(Reg);
2030 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002031 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002033 }
2034
Chris Lattnerf39f7712007-02-28 05:46:49 +00002035 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002036 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002037 if (FuncIsMadeTailCallSafe(CallConv,
2038 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002039 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002040
Evan Cheng1bc78042006-04-26 01:20:17 +00002041 // If the function takes variable number of arguments, make a frame index for
2042 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002043 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002044 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2045 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002046 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002047 }
2048 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002049 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2050
2051 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002052 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002053 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002054 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002055 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002056 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2057 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002058 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002059 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2060 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2061 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002062 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002063 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002064
2065 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002066 // The XMM registers which might contain var arg parameters are shadowed
2067 // in their paired GPR. So we only need to save the GPR to their home
2068 // slots.
2069 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002070 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071 } else {
2072 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2073 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002074
Chad Rosier30450e82011-12-22 22:35:21 +00002075 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2076 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002077 }
2078 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2079 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002080
Bill Wendling831737d2012-12-30 10:32:01 +00002081 bool NoImplicitFloatOps = Fn->getAttributes().
2082 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002083 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002084 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002085 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2086 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002087 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002088 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002089 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002090 // Kernel mode asks for SSE to be disabled, so don't push them
2091 // on the stack.
2092 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002093
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002094 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002095 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002096 // Get to the caller-allocated home save location. Add 8 to account
2097 // for the return address.
2098 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002099 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002100 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002101 // Fixup to set vararg frame on shadow area (4 x i64).
2102 if (NumIntRegs < 4)
2103 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002104 } else {
2105 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002106 // registers, then we must store them to their spots on the stack so
2107 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002108 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2109 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2110 FuncInfo->setRegSaveFrameIndex(
2111 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002112 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002113 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002114
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002117 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2118 getPointerTy());
2119 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002120 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002121 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2122 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002123 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002124 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002127 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002128 MachinePointerInfo::getFixedStack(
2129 FuncInfo->getRegSaveFrameIndex(), Offset),
2130 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002132 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002134
Dan Gohmanface41a2009-08-16 21:24:25 +00002135 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2136 // Now store the XMM (fp + vector) parameter registers.
2137 SmallVector<SDValue, 11> SaveXMMOps;
2138 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002139
Craig Topperc9099502012-04-20 06:31:50 +00002140 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002141 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2142 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002143
Dan Gohman1e93df62010-04-17 14:41:14 +00002144 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2145 FuncInfo->getRegSaveFrameIndex()));
2146 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2147 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002148
Dan Gohmanface41a2009-08-16 21:24:25 +00002149 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002150 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002151 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002152 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2153 SaveXMMOps.push_back(Val);
2154 }
2155 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2156 MVT::Other,
2157 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002159
2160 if (!MemOps.empty())
2161 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2162 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002167 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2168 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002169 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002170 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002171 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002172 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002173 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002174 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002175 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002176 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002177
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002179 // RegSaveFrameIndex is X86-64 only.
2180 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002181 if (CallConv == CallingConv::X86_FastCall ||
2182 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002183 // fastcc functions can't have varargs.
2184 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 }
Evan Cheng25caf632006-05-23 21:06:34 +00002186
Rafael Espindola76927d752011-08-30 19:39:58 +00002187 FuncInfo->setArgumentStackSize(StackSize);
2188
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2194 SDValue StackPtr, SDValue Arg,
2195 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002196 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002197 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002198 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002199 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002200 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002201 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002202 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002203
2204 return DAG.getStore(Chain, dl, Arg, PtrOff,
2205 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002206 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002207}
2208
Bill Wendling64e87322009-01-16 19:25:27 +00002209/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002210/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002211SDValue
2212X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002213 SDValue &OutRetAddr, SDValue Chain,
2214 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002215 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002216 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002219
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002220 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002221 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002222 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002223 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002224}
2225
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002226/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002227/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002228static SDValue
2229EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002230 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2231 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232 // Store the return address to the appropriate stack slot.
2233 if (!FPDiff) return Chain;
2234 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002236 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002237 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002238 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002239 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002240 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002241 return Chain;
2242}
2243
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002245X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002246 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002247 SelectionDAG &DAG = CLI.DAG;
2248 DebugLoc &dl = CLI.DL;
2249 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2250 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2251 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2252 SDValue Chain = CLI.Chain;
2253 SDValue Callee = CLI.Callee;
2254 CallingConv::ID CallConv = CLI.CallConv;
2255 bool &isTailCall = CLI.IsTailCall;
2256 bool isVarArg = CLI.IsVarArg;
2257
Dan Gohman98ca4f22009-08-05 01:29:28 +00002258 MachineFunction &MF = DAG.getMachineFunction();
2259 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002260 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002261 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002262 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002263 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002264
Nick Lewycky22de16d2012-01-19 00:34:10 +00002265 if (MF.getTarget().Options.DisableTailCalls)
2266 isTailCall = false;
2267
Evan Cheng5f941932010-02-05 02:21:12 +00002268 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002269 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002270 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002271 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002272 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002273 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002274
2275 // Sibcalls are automatically detected tailcalls which do not require
2276 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002277 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002278 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002279
2280 if (isTailCall)
2281 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002282 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002283
Chris Lattner29689432010-03-11 00:22:57 +00002284 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002285 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002286
Chris Lattner638402b2007-02-28 07:00:42 +00002287 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002288 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002289 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002290 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002291
2292 // Allocate shadow area for Win64
2293 if (IsWin64) {
2294 CCInfo.AllocateStack(32, 8);
2295 }
2296
Duncan Sands45907662010-10-31 13:21:44 +00002297 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002298
Chris Lattner423c5f42007-02-28 05:31:48 +00002299 // Get a count of how many bytes are to be pushed on the stack.
2300 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002301 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002302 // This is a sibcall. The memory operands are available in caller's
2303 // own caller's stack.
2304 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002305 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2306 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002307 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002308
Gordon Henriksen86737662008-01-05 16:56:59 +00002309 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002310 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002311 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002312 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2313 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2314
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 FPDiff = NumBytesCallerPushed - NumBytes;
2316
2317 // Set the delta of movement of the returnaddr stackslot.
2318 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002319 if (FPDiff < X86Info->getTCReturnAddrDelta())
2320 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002321 }
2322
Evan Chengf22f9b32010-02-06 03:28:46 +00002323 if (!IsSibcall)
2324 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002325
Dan Gohman475871a2008-07-27 21:46:04 +00002326 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002327 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002328 if (isTailCall && FPDiff)
2329 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2330 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002331
Dan Gohman475871a2008-07-27 21:46:04 +00002332 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2333 SmallVector<SDValue, 8> MemOpChains;
2334 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002335
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002336 // Walk the register/memloc assignments, inserting copies/loads. In the case
2337 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002338 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2339 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002340 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002341 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002343 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002344
Chris Lattner423c5f42007-02-28 05:31:48 +00002345 // Promote the value if needed.
2346 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002347 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002348 case CCValAssign::Full: break;
2349 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002350 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002351 break;
2352 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002353 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002354 break;
2355 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002356 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002357 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002358 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002359 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2360 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002361 } else
2362 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2363 break;
2364 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002365 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002366 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002367 case CCValAssign::Indirect: {
2368 // Store the argument.
2369 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002370 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002371 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002372 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002373 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002374 Arg = SpillSlot;
2375 break;
2376 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002377 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002378
Chris Lattner423c5f42007-02-28 05:31:48 +00002379 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002380 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2381 if (isVarArg && IsWin64) {
2382 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2383 // shadow reg if callee is a varargs function.
2384 unsigned ShadowReg = 0;
2385 switch (VA.getLocReg()) {
2386 case X86::XMM0: ShadowReg = X86::RCX; break;
2387 case X86::XMM1: ShadowReg = X86::RDX; break;
2388 case X86::XMM2: ShadowReg = X86::R8; break;
2389 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002390 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002391 if (ShadowReg)
2392 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002393 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002394 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002395 assert(VA.isMemLoc());
2396 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002397 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2398 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002399 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2400 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002401 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002403
Evan Cheng32fe1032006-05-25 00:59:30 +00002404 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002406 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002407
Chris Lattner88e1fd52009-07-09 04:24:46 +00002408 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002409 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2410 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002412 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2413 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002414 } else {
2415 // If we are tail calling and generating PIC/GOT style code load the
2416 // address of the callee into ECX. The value in ecx is used as target of
2417 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2418 // for tail calls on PIC/GOT architectures. Normally we would just put the
2419 // address of GOT into ebx and then call target@PLT. But for tail calls
2420 // ebx would be restored (since ebx is callee saved) before jumping to the
2421 // target@PLT.
2422
2423 // Note: The actual moving to ECX is done further down.
2424 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2425 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2426 !G->getGlobal()->hasProtectedVisibility())
2427 Callee = LowerGlobalAddress(Callee, DAG);
2428 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002429 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002430 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002431 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002432
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002433 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002434 // From AMD64 ABI document:
2435 // For calls that may call functions that use varargs or stdargs
2436 // (prototype-less calls or calls to functions containing ellipsis (...) in
2437 // the declaration) %al is used as hidden argument to specify the number
2438 // of SSE registers used. The contents of %al do not need to match exactly
2439 // the number of registers, but must be an ubound on the number of SSE
2440 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002441
Gordon Henriksen86737662008-01-05 16:56:59 +00002442 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002443 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002444 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2445 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2446 };
2447 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002448 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002449 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002450
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002451 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2452 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002453 }
2454
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002455 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002456 if (isTailCall) {
2457 // Force all the incoming stack arguments to be loaded from the stack
2458 // before any new outgoing arguments are stored to the stack, because the
2459 // outgoing stack slots may alias the incoming argument stack slots, and
2460 // the alias isn't otherwise explicit. This is slightly more conservative
2461 // than necessary, because it means that each store effectively depends
2462 // on every argument instead of just those arguments it would clobber.
2463 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2464
Dan Gohman475871a2008-07-27 21:46:04 +00002465 SmallVector<SDValue, 8> MemOpChains2;
2466 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002468 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2470 CCValAssign &VA = ArgLocs[i];
2471 if (VA.isRegLoc())
2472 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002473 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002474 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002476 // Create frame index.
2477 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002478 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002479 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002480 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002481
Duncan Sands276dcbd2008-03-21 09:14:45 +00002482 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002483 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002484 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002485 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002486 StackPtr = DAG.getCopyFromReg(Chain, dl,
2487 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002488 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002489 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002490
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2492 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002493 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002495 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002496 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002497 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002498 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002499 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002500 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002501 }
2502 }
2503
2504 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002506 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002507
2508 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002509 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2510 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002511 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002512 }
2513
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002514 // Build a sequence of copy-to-reg nodes chained together with token chain
2515 // and flag operands which copy the outgoing args into registers.
2516 SDValue InFlag;
2517 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2518 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2519 RegsToPass[i].second, InFlag);
2520 InFlag = Chain.getValue(1);
2521 }
2522
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002523 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2524 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2525 // In the 64-bit large code model, we have to make all calls
2526 // through a register, since the call instruction's 32-bit
2527 // pc-relative offset may not be large enough to hold the whole
2528 // address.
2529 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002530 // If the callee is a GlobalAddress node (quite common, every direct call
2531 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2532 // it.
2533
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002534 // We should use extra load for direct calls to dllimported functions in
2535 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002536 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002537 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002538 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002539 bool ExtraLoad = false;
2540 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002541
Chris Lattner48a7d022009-07-09 05:02:21 +00002542 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2543 // external symbols most go through the PLT in PIC mode. If the symbol
2544 // has hidden or protected visibility, or if it is static or local, then
2545 // we don't need to use the PLT - we can directly call it.
2546 if (Subtarget->isTargetELF() &&
2547 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002548 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002549 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002550 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002551 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002552 (!Subtarget->getTargetTriple().isMacOSX() ||
2553 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002554 // PC-relative references to external symbols should go through $stub,
2555 // unless we're building with the leopard linker or later, which
2556 // automatically synthesizes these stubs.
2557 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002558 } else if (Subtarget->isPICStyleRIPRel() &&
2559 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002560 cast<Function>(GV)->getAttributes().
2561 hasAttribute(AttributeSet::FunctionIndex,
2562 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002563 // If the function is marked as non-lazy, generate an indirect call
2564 // which loads from the GOT directly. This avoids runtime overhead
2565 // at the cost of eager binding (and one extra byte of encoding).
2566 OpFlags = X86II::MO_GOTPCREL;
2567 WrapperKind = X86ISD::WrapperRIP;
2568 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002569 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002570
Devang Patel0d881da2010-07-06 22:08:15 +00002571 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002572 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002573
2574 // Add a wrapper if needed.
2575 if (WrapperKind != ISD::DELETED_NODE)
2576 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2577 // Add extra indirection if needed.
2578 if (ExtraLoad)
2579 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2580 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002581 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002582 }
Bill Wendling056292f2008-09-16 21:48:12 +00002583 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002584 unsigned char OpFlags = 0;
2585
Evan Cheng1bf891a2010-12-01 22:59:46 +00002586 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2587 // external symbols should go through the PLT.
2588 if (Subtarget->isTargetELF() &&
2589 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2590 OpFlags = X86II::MO_PLT;
2591 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002592 (!Subtarget->getTargetTriple().isMacOSX() ||
2593 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002594 // PC-relative references to external symbols should go through $stub,
2595 // unless we're building with the leopard linker or later, which
2596 // automatically synthesizes these stubs.
2597 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002598 }
Eric Christopherfd179292009-08-27 18:07:15 +00002599
Chris Lattner48a7d022009-07-09 05:02:21 +00002600 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2601 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002602 }
2603
Chris Lattnerd96d0722007-02-25 06:40:16 +00002604 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002605 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002606 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002607
Evan Chengf22f9b32010-02-06 03:28:46 +00002608 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002609 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2610 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002611 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002612 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002613
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002614 Ops.push_back(Chain);
2615 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002616
Dan Gohman98ca4f22009-08-05 01:29:28 +00002617 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002618 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002619
Gordon Henriksen86737662008-01-05 16:56:59 +00002620 // Add argument registers to the end of the list so that they are known live
2621 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002622 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2623 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2624 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002625
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002626 // Add a register mask operand representing the call-preserved registers.
2627 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2628 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2629 assert(Mask && "Missing call preserved mask for calling convention");
2630 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002631
Gabor Greifba36cb52008-08-28 21:40:38 +00002632 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002633 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002634
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002636 // We used to do:
2637 //// If this is the first return lowered for this function, add the regs
2638 //// to the liveout set for the function.
2639 // This isn't right, although it's probably harmless on x86; liveouts
2640 // should be computed from returns not tail calls. Consider a void
2641 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002642 return DAG.getNode(X86ISD::TC_RETURN, dl,
2643 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002644 }
2645
Dale Johannesenace16102009-02-03 19:33:06 +00002646 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002647 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002648
Chris Lattner2d297092006-05-23 18:50:38 +00002649 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002650 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002651 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2652 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002653 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002654 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002655 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002656 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002657 // pops the hidden struct pointer, so we have to push it back.
2658 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002659 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002660 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002661 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002662 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Gordon Henriksenae636f82008-01-03 16:47:34 +00002664 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002665 if (!IsSibcall) {
2666 Chain = DAG.getCALLSEQ_END(Chain,
2667 DAG.getIntPtrConstant(NumBytes, true),
2668 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2669 true),
2670 InFlag);
2671 InFlag = Chain.getValue(1);
2672 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002673
Chris Lattner3085e152007-02-25 08:59:22 +00002674 // Handle result values, copying them out of physregs into vregs that we
2675 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002676 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2677 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002678}
2679
Evan Cheng25ab6902006-09-08 06:48:29 +00002680//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002681// Fast Calling Convention (tail call) implementation
2682//===----------------------------------------------------------------------===//
2683
2684// Like std call, callee cleans arguments, convention except that ECX is
2685// reserved for storing the tail called function address. Only 2 registers are
2686// free for argument passing (inreg). Tail call optimization is performed
2687// provided:
2688// * tailcallopt is enabled
2689// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002690// On X86_64 architecture with GOT-style position independent code only local
2691// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002692// To keep the stack aligned according to platform abi the function
2693// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2694// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002695// If a tail called function callee has more arguments than the caller the
2696// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002697// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002698// original REtADDR, but before the saved framepointer or the spilled registers
2699// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2700// stack layout:
2701// arg1
2702// arg2
2703// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002704// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002705// move area ]
2706// (possible EBP)
2707// ESI
2708// EDI
2709// local1 ..
2710
2711/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2712/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002713unsigned
2714X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2715 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002716 MachineFunction &MF = DAG.getMachineFunction();
2717 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002718 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002719 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002720 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002721 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002722 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002723 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2724 // Number smaller than 12 so just add the difference.
2725 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2726 } else {
2727 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002728 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002729 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002730 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002731 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002732}
2733
Evan Cheng5f941932010-02-05 02:21:12 +00002734/// MatchingStackOffset - Return true if the given stack call argument is
2735/// already available in the same position (relatively) of the caller's
2736/// incoming argument stack.
2737static
2738bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2739 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2740 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002741 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2742 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002743 if (Arg.getOpcode() == ISD::CopyFromReg) {
2744 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002745 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002746 return false;
2747 MachineInstr *Def = MRI->getVRegDef(VR);
2748 if (!Def)
2749 return false;
2750 if (!Flags.isByVal()) {
2751 if (!TII->isLoadFromStackSlot(Def, FI))
2752 return false;
2753 } else {
2754 unsigned Opcode = Def->getOpcode();
2755 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2756 Def->getOperand(1).isFI()) {
2757 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002758 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002759 } else
2760 return false;
2761 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002762 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2763 if (Flags.isByVal())
2764 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002765 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002766 // define @foo(%struct.X* %A) {
2767 // tail call @bar(%struct.X* byval %A)
2768 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002769 return false;
2770 SDValue Ptr = Ld->getBasePtr();
2771 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2772 if (!FINode)
2773 return false;
2774 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002775 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002776 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002777 FI = FINode->getIndex();
2778 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002779 } else
2780 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002781
Evan Cheng4cae1332010-03-05 08:38:04 +00002782 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002783 if (!MFI->isFixedObjectIndex(FI))
2784 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002785 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002786}
2787
Dan Gohman98ca4f22009-08-05 01:29:28 +00002788/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2789/// for tail call optimization. Targets which want to do tail call
2790/// optimization should implement this function.
2791bool
2792X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002793 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002794 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002795 bool isCalleeStructRet,
2796 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002797 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002798 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002799 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002800 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002801 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002802 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002803 CalleeCC != CallingConv::C)
2804 return false;
2805
Evan Cheng7096ae42010-01-29 06:45:59 +00002806 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002807 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002808 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002809
2810 // If the function return type is x86_fp80 and the callee return type is not,
2811 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2812 // perform a tailcall optimization here.
2813 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2814 return false;
2815
Evan Cheng13617962010-04-30 01:12:32 +00002816 CallingConv::ID CallerCC = CallerF->getCallingConv();
2817 bool CCMatch = CallerCC == CalleeCC;
2818
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002819 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002820 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002821 return true;
2822 return false;
2823 }
2824
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002825 // Look for obvious safe cases to perform tail call optimization that do not
2826 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002827
Evan Cheng2c12cb42010-03-26 16:26:03 +00002828 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2829 // emit a special epilogue.
2830 if (RegInfo->needsStackRealignment(MF))
2831 return false;
2832
Evan Chenga375d472010-03-15 18:54:48 +00002833 // Also avoid sibcall optimization if either caller or callee uses struct
2834 // return semantics.
2835 if (isCalleeStructRet || isCallerStructRet)
2836 return false;
2837
Chad Rosier2416da32011-06-24 21:15:36 +00002838 // An stdcall caller is expected to clean up its arguments; the callee
2839 // isn't going to do that.
2840 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2841 return false;
2842
Chad Rosier871f6642011-05-18 19:59:50 +00002843 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002844 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002845 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002846
2847 // Optimizing for varargs on Win64 is unlikely to be safe without
2848 // additional testing.
2849 if (Subtarget->isTargetWin64())
2850 return false;
2851
Chad Rosier871f6642011-05-18 19:59:50 +00002852 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002853 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002854 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002855
Chad Rosier871f6642011-05-18 19:59:50 +00002856 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2857 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2858 if (!ArgLocs[i].isRegLoc())
2859 return false;
2860 }
2861
Chad Rosier30450e82011-12-22 22:35:21 +00002862 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2863 // stack. Therefore, if it's not used by the call it is not safe to optimize
2864 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002865 bool Unused = false;
2866 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2867 if (!Ins[i].Used) {
2868 Unused = true;
2869 break;
2870 }
2871 }
2872 if (Unused) {
2873 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002874 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002875 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002876 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002877 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002878 CCValAssign &VA = RVLocs[i];
2879 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2880 return false;
2881 }
2882 }
2883
Evan Cheng13617962010-04-30 01:12:32 +00002884 // If the calling conventions do not match, then we'd better make sure the
2885 // results are returned in the same way as what the caller expects.
2886 if (!CCMatch) {
2887 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002888 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002889 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002890 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2891
2892 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002893 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002894 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002895 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2896
2897 if (RVLocs1.size() != RVLocs2.size())
2898 return false;
2899 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2900 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2901 return false;
2902 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2903 return false;
2904 if (RVLocs1[i].isRegLoc()) {
2905 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2906 return false;
2907 } else {
2908 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2909 return false;
2910 }
2911 }
2912 }
2913
Evan Chenga6bff982010-01-30 01:22:00 +00002914 // If the callee takes no arguments then go on to check the results of the
2915 // call.
2916 if (!Outs.empty()) {
2917 // Check if stack adjustment is needed. For now, do not do this if any
2918 // argument is passed on the stack.
2919 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002920 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002921 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002922
2923 // Allocate shadow area for Win64
2924 if (Subtarget->isTargetWin64()) {
2925 CCInfo.AllocateStack(32, 8);
2926 }
2927
Duncan Sands45907662010-10-31 13:21:44 +00002928 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002929 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002930 MachineFunction &MF = DAG.getMachineFunction();
2931 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2932 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002933
2934 // Check if the arguments are already laid out in the right way as
2935 // the caller's fixed stack objects.
2936 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002937 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2938 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002939 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002940 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2941 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002942 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002943 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002944 if (VA.getLocInfo() == CCValAssign::Indirect)
2945 return false;
2946 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002947 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2948 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002949 return false;
2950 }
2951 }
2952 }
Evan Cheng9c044672010-05-29 01:35:22 +00002953
2954 // If the tailcall address may be in a register, then make sure it's
2955 // possible to register allocate for it. In 32-bit, the call address can
2956 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002957 // callee-saved registers are restored. These happen to be the same
2958 // registers used to pass 'inreg' arguments so watch out for those.
2959 if (!Subtarget->is64Bit() &&
2960 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002961 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002962 unsigned NumInRegs = 0;
2963 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2964 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002965 if (!VA.isRegLoc())
2966 continue;
2967 unsigned Reg = VA.getLocReg();
2968 switch (Reg) {
2969 default: break;
2970 case X86::EAX: case X86::EDX: case X86::ECX:
2971 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002972 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002973 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002974 }
2975 }
2976 }
Evan Chenga6bff982010-01-30 01:22:00 +00002977 }
Evan Chengb1712452010-01-27 06:25:16 +00002978
Evan Cheng86809cc2010-02-03 03:28:02 +00002979 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002980}
2981
Dan Gohman3df24e62008-09-03 23:12:08 +00002982FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002983X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2984 const TargetLibraryInfo *libInfo) const {
2985 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002986}
2987
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002988//===----------------------------------------------------------------------===//
2989// Other Lowering Hooks
2990//===----------------------------------------------------------------------===//
2991
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002992static bool MayFoldLoad(SDValue Op) {
2993 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2994}
2995
2996static bool MayFoldIntoStore(SDValue Op) {
2997 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2998}
2999
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003000static bool isTargetShuffle(unsigned Opcode) {
3001 switch(Opcode) {
3002 default: return false;
3003 case X86ISD::PSHUFD:
3004 case X86ISD::PSHUFHW:
3005 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003006 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003007 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003008 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003009 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003010 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003011 case X86ISD::MOVLPS:
3012 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003013 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003014 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003015 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003016 case X86ISD::MOVSS:
3017 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003018 case X86ISD::UNPCKL:
3019 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003020 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003021 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003022 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003023 return true;
3024 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003025}
3026
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003027static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003028 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003029 switch(Opc) {
3030 default: llvm_unreachable("Unknown x86 shuffle node");
3031 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003032 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003033 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003034 return DAG.getNode(Opc, dl, VT, V1);
3035 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003036}
3037
3038static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003039 SDValue V1, unsigned TargetMask,
3040 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003041 switch(Opc) {
3042 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003043 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003044 case X86ISD::PSHUFHW:
3045 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003046 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003047 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003048 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3049 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003050}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003051
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003052static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003053 SDValue V1, SDValue V2, unsigned TargetMask,
3054 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003055 switch(Opc) {
3056 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003057 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003058 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003059 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003060 return DAG.getNode(Opc, dl, VT, V1, V2,
3061 DAG.getConstant(TargetMask, MVT::i8));
3062 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003063}
3064
3065static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3066 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3067 switch(Opc) {
3068 default: llvm_unreachable("Unknown x86 shuffle node");
3069 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003070 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003071 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003072 case X86ISD::MOVLPS:
3073 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003074 case X86ISD::MOVSS:
3075 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003076 case X86ISD::UNPCKL:
3077 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003078 return DAG.getNode(Opc, dl, VT, V1, V2);
3079 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003080}
3081
Dan Gohmand858e902010-04-17 15:26:15 +00003082SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003083 MachineFunction &MF = DAG.getMachineFunction();
3084 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3085 int ReturnAddrIndex = FuncInfo->getRAIndex();
3086
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003087 if (ReturnAddrIndex == 0) {
3088 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003089 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003090 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003091 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003092 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003093 }
3094
Evan Cheng25ab6902006-09-08 06:48:29 +00003095 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003096}
3097
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003098bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3099 bool hasSymbolicDisplacement) {
3100 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003101 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003102 return false;
3103
3104 // If we don't have a symbolic displacement - we don't have any extra
3105 // restrictions.
3106 if (!hasSymbolicDisplacement)
3107 return true;
3108
3109 // FIXME: Some tweaks might be needed for medium code model.
3110 if (M != CodeModel::Small && M != CodeModel::Kernel)
3111 return false;
3112
3113 // For small code model we assume that latest object is 16MB before end of 31
3114 // bits boundary. We may also accept pretty large negative constants knowing
3115 // that all objects are in the positive half of address space.
3116 if (M == CodeModel::Small && Offset < 16*1024*1024)
3117 return true;
3118
3119 // For kernel code model we know that all object resist in the negative half
3120 // of 32bits address space. We may not accept negative offsets, since they may
3121 // be just off and we may accept pretty large positive ones.
3122 if (M == CodeModel::Kernel && Offset > 0)
3123 return true;
3124
3125 return false;
3126}
3127
Evan Chengef41ff62011-06-23 17:54:54 +00003128/// isCalleePop - Determines whether the callee is required to pop its
3129/// own arguments. Callee pop is necessary to support tail calls.
3130bool X86::isCalleePop(CallingConv::ID CallingConv,
3131 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3132 if (IsVarArg)
3133 return false;
3134
3135 switch (CallingConv) {
3136 default:
3137 return false;
3138 case CallingConv::X86_StdCall:
3139 return !is64Bit;
3140 case CallingConv::X86_FastCall:
3141 return !is64Bit;
3142 case CallingConv::X86_ThisCall:
3143 return !is64Bit;
3144 case CallingConv::Fast:
3145 return TailCallOpt;
3146 case CallingConv::GHC:
3147 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003148 case CallingConv::HiPE:
3149 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003150 }
3151}
3152
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003153/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3154/// specific condition code, returning the condition code and the LHS/RHS of the
3155/// comparison to make.
3156static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3157 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003158 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003159 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3160 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3161 // X > -1 -> X == 0, jump !sign.
3162 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003163 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003164 }
3165 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003166 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003167 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003168 }
3169 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003170 // X < 1 -> X <= 0
3171 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003172 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003173 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003174 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003175
Evan Chengd9558e02006-01-06 00:43:03 +00003176 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003177 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003178 case ISD::SETEQ: return X86::COND_E;
3179 case ISD::SETGT: return X86::COND_G;
3180 case ISD::SETGE: return X86::COND_GE;
3181 case ISD::SETLT: return X86::COND_L;
3182 case ISD::SETLE: return X86::COND_LE;
3183 case ISD::SETNE: return X86::COND_NE;
3184 case ISD::SETULT: return X86::COND_B;
3185 case ISD::SETUGT: return X86::COND_A;
3186 case ISD::SETULE: return X86::COND_BE;
3187 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003188 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003189 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003190
Chris Lattner4c78e022008-12-23 23:42:27 +00003191 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003192
Chris Lattner4c78e022008-12-23 23:42:27 +00003193 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003194 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3195 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003196 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3197 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003198 }
3199
Chris Lattner4c78e022008-12-23 23:42:27 +00003200 switch (SetCCOpcode) {
3201 default: break;
3202 case ISD::SETOLT:
3203 case ISD::SETOLE:
3204 case ISD::SETUGT:
3205 case ISD::SETUGE:
3206 std::swap(LHS, RHS);
3207 break;
3208 }
3209
3210 // On a floating point condition, the flags are set as follows:
3211 // ZF PF CF op
3212 // 0 | 0 | 0 | X > Y
3213 // 0 | 0 | 1 | X < Y
3214 // 1 | 0 | 0 | X == Y
3215 // 1 | 1 | 1 | unordered
3216 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003217 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003218 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003219 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003220 case ISD::SETOLT: // flipped
3221 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003222 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003223 case ISD::SETOLE: // flipped
3224 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003225 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003226 case ISD::SETUGT: // flipped
3227 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003228 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003229 case ISD::SETUGE: // flipped
3230 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003231 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003232 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003233 case ISD::SETNE: return X86::COND_NE;
3234 case ISD::SETUO: return X86::COND_P;
3235 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003236 case ISD::SETOEQ:
3237 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003238 }
Evan Chengd9558e02006-01-06 00:43:03 +00003239}
3240
Evan Cheng4a460802006-01-11 00:33:36 +00003241/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3242/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003243/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003244static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003245 switch (X86CC) {
3246 default:
3247 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003248 case X86::COND_B:
3249 case X86::COND_BE:
3250 case X86::COND_E:
3251 case X86::COND_P:
3252 case X86::COND_A:
3253 case X86::COND_AE:
3254 case X86::COND_NE:
3255 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003256 return true;
3257 }
3258}
3259
Evan Chengeb2f9692009-10-27 19:56:55 +00003260/// isFPImmLegal - Returns true if the target can instruction select the
3261/// specified FP immediate natively. If false, the legalizer will
3262/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003263bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003264 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3265 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3266 return true;
3267 }
3268 return false;
3269}
3270
Nate Begeman9008ca62009-04-27 18:41:29 +00003271/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3272/// the specified range (L, H].
3273static bool isUndefOrInRange(int Val, int Low, int Hi) {
3274 return (Val < 0) || (Val >= Low && Val < Hi);
3275}
3276
3277/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3278/// specified value.
3279static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003280 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003281}
3282
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003283/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003284/// from position Pos and ending in Pos+Size, falls within the specified
3285/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003286static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003287 unsigned Pos, unsigned Size, int Low) {
3288 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003289 if (!isUndefOrEqual(Mask[i], Low))
3290 return false;
3291 return true;
3292}
3293
Nate Begeman9008ca62009-04-27 18:41:29 +00003294/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3295/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3296/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003297static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003298 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003299 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 return (Mask[0] < 2 && Mask[1] < 2);
3302 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003303}
3304
Nate Begeman9008ca62009-04-27 18:41:29 +00003305/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3306/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003307static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3308 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003309 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003310
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003312 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3313 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003314
Evan Cheng506d3df2006-03-29 23:07:14 +00003315 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003316 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003317 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003318 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003319
Craig Toppera9a568a2012-05-02 08:03:44 +00003320 if (VT == MVT::v16i16) {
3321 // Lower quadword copied in order or undef.
3322 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3323 return false;
3324
3325 // Upper quadword shuffled.
3326 for (unsigned i = 12; i != 16; ++i)
3327 if (!isUndefOrInRange(Mask[i], 12, 16))
3328 return false;
3329 }
3330
Evan Cheng506d3df2006-03-29 23:07:14 +00003331 return true;
3332}
3333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3335/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003336static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3337 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003338 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Rafael Espindola15684b22009-04-24 12:40:33 +00003340 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003341 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3342 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003343
Rafael Espindola15684b22009-04-24 12:40:33 +00003344 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003345 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003346 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003347 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003348
Craig Toppera9a568a2012-05-02 08:03:44 +00003349 if (VT == MVT::v16i16) {
3350 // Upper quadword copied in order.
3351 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3352 return false;
3353
3354 // Lower quadword shuffled.
3355 for (unsigned i = 8; i != 12; ++i)
3356 if (!isUndefOrInRange(Mask[i], 8, 12))
3357 return false;
3358 }
3359
Rafael Espindola15684b22009-04-24 12:40:33 +00003360 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003361}
3362
Nate Begemana09008b2009-10-19 02:17:23 +00003363/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3364/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003365static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3366 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003367 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3368 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003369 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003370
Craig Topper0e2037b2012-01-20 05:53:00 +00003371 unsigned NumElts = VT.getVectorNumElements();
3372 unsigned NumLanes = VT.getSizeInBits()/128;
3373 unsigned NumLaneElts = NumElts/NumLanes;
3374
3375 // Do not handle 64-bit element shuffles with palignr.
3376 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003377 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003378
Craig Topper0e2037b2012-01-20 05:53:00 +00003379 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3380 unsigned i;
3381 for (i = 0; i != NumLaneElts; ++i) {
3382 if (Mask[i+l] >= 0)
3383 break;
3384 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003385
Craig Topper0e2037b2012-01-20 05:53:00 +00003386 // Lane is all undef, go to next lane
3387 if (i == NumLaneElts)
3388 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003389
Craig Topper0e2037b2012-01-20 05:53:00 +00003390 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003391
Craig Topper0e2037b2012-01-20 05:53:00 +00003392 // Make sure its in this lane in one of the sources
3393 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3394 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003395 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003396
3397 // If not lane 0, then we must match lane 0
3398 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3399 return false;
3400
3401 // Correct second source to be contiguous with first source
3402 if (Start >= (int)NumElts)
3403 Start -= NumElts - NumLaneElts;
3404
3405 // Make sure we're shifting in the right direction.
3406 if (Start <= (int)(i+l))
3407 return false;
3408
3409 Start -= i;
3410
3411 // Check the rest of the elements to see if they are consecutive.
3412 for (++i; i != NumLaneElts; ++i) {
3413 int Idx = Mask[i+l];
3414
3415 // Make sure its in this lane
3416 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3417 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3418 return false;
3419
3420 // If not lane 0, then we must match lane 0
3421 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3422 return false;
3423
3424 if (Idx >= (int)NumElts)
3425 Idx -= NumElts - NumLaneElts;
3426
3427 if (!isUndefOrEqual(Idx, Start+i))
3428 return false;
3429
3430 }
Nate Begemana09008b2009-10-19 02:17:23 +00003431 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003432
Nate Begemana09008b2009-10-19 02:17:23 +00003433 return true;
3434}
3435
Craig Topper1a7700a2012-01-19 08:19:12 +00003436/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3437/// the two vector operands have swapped position.
3438static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3439 unsigned NumElems) {
3440 for (unsigned i = 0; i != NumElems; ++i) {
3441 int idx = Mask[i];
3442 if (idx < 0)
3443 continue;
3444 else if (idx < (int)NumElems)
3445 Mask[i] = idx + NumElems;
3446 else
3447 Mask[i] = idx - NumElems;
3448 }
3449}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003450
Craig Topper1a7700a2012-01-19 08:19:12 +00003451/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3452/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3453/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3454/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003455static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003456 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003457 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003458 return false;
3459
Craig Topper1a7700a2012-01-19 08:19:12 +00003460 unsigned NumElems = VT.getVectorNumElements();
3461 unsigned NumLanes = VT.getSizeInBits()/128;
3462 unsigned NumLaneElems = NumElems/NumLanes;
3463
3464 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003465 return false;
3466
3467 // VSHUFPSY divides the resulting vector into 4 chunks.
3468 // The sources are also splitted into 4 chunks, and each destination
3469 // chunk must come from a different source chunk.
3470 //
3471 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3472 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3473 //
3474 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3475 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3476 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003477 // VSHUFPDY divides the resulting vector into 4 chunks.
3478 // The sources are also splitted into 4 chunks, and each destination
3479 // chunk must come from a different source chunk.
3480 //
3481 // SRC1 => X3 X2 X1 X0
3482 // SRC2 => Y3 Y2 Y1 Y0
3483 //
3484 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3485 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003486 unsigned HalfLaneElems = NumLaneElems/2;
3487 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3488 for (unsigned i = 0; i != NumLaneElems; ++i) {
3489 int Idx = Mask[i+l];
3490 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3491 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3492 return false;
3493 // For VSHUFPSY, the mask of the second half must be the same as the
3494 // first but with the appropriate offsets. This works in the same way as
3495 // VPERMILPS works with masks.
3496 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3497 continue;
3498 if (!isUndefOrEqual(Idx, Mask[i]+l))
3499 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003500 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003501 }
3502
3503 return true;
3504}
3505
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003506/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3507/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003508static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003509 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003510 return false;
3511
Craig Topper7a9a28b2012-08-12 02:23:29 +00003512 unsigned NumElems = VT.getVectorNumElements();
3513
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003514 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003515 return false;
3516
Evan Cheng2064a2b2006-03-28 06:50:32 +00003517 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003518 return isUndefOrEqual(Mask[0], 6) &&
3519 isUndefOrEqual(Mask[1], 7) &&
3520 isUndefOrEqual(Mask[2], 2) &&
3521 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003522}
3523
Nate Begeman0b10b912009-11-07 23:17:15 +00003524/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3525/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3526/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003527static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003528 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003529 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003530
Craig Topper7a9a28b2012-08-12 02:23:29 +00003531 unsigned NumElems = VT.getVectorNumElements();
3532
Nate Begeman0b10b912009-11-07 23:17:15 +00003533 if (NumElems != 4)
3534 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003535
Craig Topperdd637ae2012-02-19 05:41:45 +00003536 return isUndefOrEqual(Mask[0], 2) &&
3537 isUndefOrEqual(Mask[1], 3) &&
3538 isUndefOrEqual(Mask[2], 2) &&
3539 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003540}
3541
Evan Cheng5ced1d82006-04-06 23:23:56 +00003542/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3543/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003544static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003545 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003546 return false;
3547
Craig Topperdd637ae2012-02-19 05:41:45 +00003548 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003549
Evan Cheng5ced1d82006-04-06 23:23:56 +00003550 if (NumElems != 2 && NumElems != 4)
3551 return false;
3552
Chad Rosier238ae312012-04-30 17:47:15 +00003553 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003554 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003555 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003556
Chad Rosier238ae312012-04-30 17:47:15 +00003557 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003558 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003559 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003560
3561 return true;
3562}
3563
Nate Begeman0b10b912009-11-07 23:17:15 +00003564/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3565/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003566static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003567 if (!VT.is128BitVector())
3568 return false;
3569
Craig Topperdd637ae2012-02-19 05:41:45 +00003570 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003571
Craig Topper7a9a28b2012-08-12 02:23:29 +00003572 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003573 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))
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 = 0, e = NumElems/2; i != e; ++i)
3580 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003581 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003582
3583 return true;
3584}
3585
Elena Demikhovsky15963732012-06-26 08:04:10 +00003586//
3587// Some special combinations that can be optimized.
3588//
3589static
3590SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3591 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003592 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003593 DebugLoc dl = SVOp->getDebugLoc();
3594
3595 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3596 return SDValue();
3597
3598 ArrayRef<int> Mask = SVOp->getMask();
3599
3600 // These are the special masks that may be optimized.
3601 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3602 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3603 bool MatchEvenMask = true;
3604 bool MatchOddMask = true;
3605 for (int i=0; i<8; ++i) {
3606 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3607 MatchEvenMask = false;
3608 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3609 MatchOddMask = false;
3610 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003611
Elena Demikhovsky32510202012-09-04 12:49:02 +00003612 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003613 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003614
Elena Demikhovsky15963732012-06-26 08:04:10 +00003615 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3616
Elena Demikhovsky32510202012-09-04 12:49:02 +00003617 SDValue Op0 = SVOp->getOperand(0);
3618 SDValue Op1 = SVOp->getOperand(1);
3619
3620 if (MatchEvenMask) {
3621 // Shift the second operand right to 32 bits.
3622 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3623 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3624 } else {
3625 // Shift the first operand left to 32 bits.
3626 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3627 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3628 }
3629 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3630 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003631}
3632
Evan Cheng0038e592006-03-28 00:39:58 +00003633/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3634/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003635static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003636 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003637 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003638
3639 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3640 "Unsupported vector type for unpckh");
3641
Craig Topper5a529e42013-01-18 06:44:29 +00003642 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003643 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003644 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003645
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003646 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3647 // independently on 128-bit lanes.
3648 unsigned NumLanes = VT.getSizeInBits()/128;
3649 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003650
Craig Topper94438ba2011-12-16 08:06:31 +00003651 for (unsigned l = 0; l != NumLanes; ++l) {
3652 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3653 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003654 i += 2, ++j) {
3655 int BitI = Mask[i];
3656 int BitI1 = Mask[i+1];
3657 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003658 return false;
David Greenea20244d2011-03-02 17:23:43 +00003659 if (V2IsSplat) {
3660 if (!isUndefOrEqual(BitI1, NumElts))
3661 return false;
3662 } else {
3663 if (!isUndefOrEqual(BitI1, j + NumElts))
3664 return false;
3665 }
Evan Cheng39623da2006-04-20 08:58:49 +00003666 }
Evan Cheng0038e592006-03-28 00:39:58 +00003667 }
David Greenea20244d2011-03-02 17:23:43 +00003668
Evan Cheng0038e592006-03-28 00:39:58 +00003669 return true;
3670}
3671
Evan Cheng4fcb9222006-03-28 02:43:26 +00003672/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3673/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003674static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003675 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003676 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003677
3678 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3679 "Unsupported vector type for unpckh");
3680
Craig Topper5a529e42013-01-18 06:44:29 +00003681 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003682 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003683 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003684
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003685 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3686 // independently on 128-bit lanes.
3687 unsigned NumLanes = VT.getSizeInBits()/128;
3688 unsigned NumLaneElts = NumElts/NumLanes;
3689
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003690 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003691 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3692 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003693 int BitI = Mask[i];
3694 int BitI1 = Mask[i+1];
3695 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003696 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003697 if (V2IsSplat) {
3698 if (isUndefOrEqual(BitI1, NumElts))
3699 return false;
3700 } else {
3701 if (!isUndefOrEqual(BitI1, j+NumElts))
3702 return false;
3703 }
Evan Cheng39623da2006-04-20 08:58:49 +00003704 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003705 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003706 return true;
3707}
3708
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003709/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3710/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3711/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003712static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003713 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003714 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003715
3716 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3717 "Unsupported vector type for unpckh");
3718
Craig Topper5a529e42013-01-18 06:44:29 +00003719 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003720 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003721 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003722
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003723 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3724 // FIXME: Need a better way to get rid of this, there's no latency difference
3725 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3726 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003727 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003728 return false;
3729
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003730 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3731 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003732 unsigned NumLanes = VT.getSizeInBits()/128;
3733 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003734
Craig Topper94438ba2011-12-16 08:06:31 +00003735 for (unsigned l = 0; l != NumLanes; ++l) {
3736 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3737 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003738 i += 2, ++j) {
3739 int BitI = Mask[i];
3740 int BitI1 = Mask[i+1];
3741
3742 if (!isUndefOrEqual(BitI, j))
3743 return false;
3744 if (!isUndefOrEqual(BitI1, j))
3745 return false;
3746 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003747 }
David Greenea20244d2011-03-02 17:23:43 +00003748
Rafael Espindola15684b22009-04-24 12:40:33 +00003749 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003750}
3751
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003752/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3753/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3754/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003755static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003756 unsigned NumElts = VT.getVectorNumElements();
3757
3758 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3759 "Unsupported vector type for unpckh");
3760
Craig Topper5a529e42013-01-18 06:44:29 +00003761 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003762 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003763 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003764
Craig Topper94438ba2011-12-16 08:06:31 +00003765 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3766 // independently on 128-bit lanes.
3767 unsigned NumLanes = VT.getSizeInBits()/128;
3768 unsigned NumLaneElts = NumElts/NumLanes;
3769
3770 for (unsigned l = 0; l != NumLanes; ++l) {
3771 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3772 i != (l+1)*NumLaneElts; i += 2, ++j) {
3773 int BitI = Mask[i];
3774 int BitI1 = Mask[i+1];
3775 if (!isUndefOrEqual(BitI, j))
3776 return false;
3777 if (!isUndefOrEqual(BitI1, j))
3778 return false;
3779 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003780 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003781 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003782}
3783
Evan Cheng017dcc62006-04-21 01:05:10 +00003784/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3785/// specifies a shuffle of elements that is suitable for input to MOVSS,
3786/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003787static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003788 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003789 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003790 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003791 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003792
Craig Topperc612d792012-01-02 09:17:37 +00003793 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003794
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003796 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003797
Craig Topperc612d792012-01-02 09:17:37 +00003798 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003800 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003801
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003802 return true;
3803}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003804
Craig Topper70b883b2011-11-28 10:14:51 +00003805/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003806/// as permutations between 128-bit chunks or halves. As an example: this
3807/// shuffle bellow:
3808/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3809/// The first half comes from the second half of V1 and the second half from the
3810/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003811static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3812 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003813 return false;
3814
3815 // The shuffle result is divided into half A and half B. In total the two
3816 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3817 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003818 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003819 bool MatchA = false, MatchB = false;
3820
3821 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003822 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003823 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3824 MatchA = true;
3825 break;
3826 }
3827 }
3828
3829 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003830 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003831 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3832 MatchB = true;
3833 break;
3834 }
3835 }
3836
3837 return MatchA && MatchB;
3838}
3839
Craig Topper70b883b2011-11-28 10:14:51 +00003840/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3841/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003842static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003843 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003844
Craig Topperc612d792012-01-02 09:17:37 +00003845 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003846
Craig Topperc612d792012-01-02 09:17:37 +00003847 unsigned FstHalf = 0, SndHalf = 0;
3848 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003849 if (SVOp->getMaskElt(i) > 0) {
3850 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3851 break;
3852 }
3853 }
Craig Topperc612d792012-01-02 09:17:37 +00003854 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003855 if (SVOp->getMaskElt(i) > 0) {
3856 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3857 break;
3858 }
3859 }
3860
3861 return (FstHalf | (SndHalf << 4));
3862}
3863
Craig Topper70b883b2011-11-28 10:14:51 +00003864/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003865/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3866/// Note that VPERMIL mask matching is different depending whether theunderlying
3867/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3868/// to the same elements of the low, but to the higher half of the source.
3869/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003870/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003871static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3872 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003873 return false;
3874
Craig Topperc612d792012-01-02 09:17:37 +00003875 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003876 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003877 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003878 return false;
3879
Craig Topperc612d792012-01-02 09:17:37 +00003880 unsigned NumLanes = VT.getSizeInBits()/128;
3881 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003882 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003883 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003884 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003885 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003886 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003887 continue;
3888 // VPERMILPS handling
3889 if (Mask[i] < 0)
3890 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003891 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003892 return false;
3893 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003894 }
3895
3896 return true;
3897}
3898
Craig Topper5aaffa82012-02-19 02:53:47 +00003899/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003900/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003901/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003902static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003904 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003905 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003906
3907 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003908 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003909 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003910
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003912 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003913
Craig Topperc612d792012-01-02 09:17:37 +00003914 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003915 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3916 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3917 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003918 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003919
Evan Cheng39623da2006-04-20 08:58:49 +00003920 return true;
3921}
3922
Evan Chengd9539472006-04-14 21:59:03 +00003923/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3924/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003926static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003927 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003928 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003929 return false;
3930
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931 unsigned NumElems = VT.getVectorNumElements();
3932
Craig Topper5a529e42013-01-18 06:44:29 +00003933 if ((VT.is128BitVector() && NumElems != 4) ||
3934 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003935 return false;
3936
3937 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003938 for (unsigned i = 0; i != NumElems; i += 2)
3939 if (!isUndefOrEqual(Mask[i], i+1) ||
3940 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003942
3943 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003944}
3945
3946/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3947/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003948/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003949static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003950 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003951 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003952 return false;
3953
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003954 unsigned NumElems = VT.getVectorNumElements();
3955
Craig Topper5a529e42013-01-18 06:44:29 +00003956 if ((VT.is128BitVector() && NumElems != 4) ||
3957 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003958 return false;
3959
3960 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003961 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003962 if (!isUndefOrEqual(Mask[i], i) ||
3963 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003965
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003966 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003967}
3968
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003969/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3970/// specifies a shuffle of elements that is suitable for input to 256-bit
3971/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003972static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3973 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003974 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003975
Craig Topper7a9a28b2012-08-12 02:23:29 +00003976 unsigned NumElts = VT.getVectorNumElements();
3977 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003978 return false;
3979
Craig Topperc612d792012-01-02 09:17:37 +00003980 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003981 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003982 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003983 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003984 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003985 return false;
3986 return true;
3987}
3988
Evan Cheng0b457f02008-09-25 20:50:48 +00003989/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003990/// specifies a shuffle of elements that is suitable for input to 128-bit
3991/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003992static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003993 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003994 return false;
3995
Craig Topperc612d792012-01-02 09:17:37 +00003996 unsigned e = VT.getVectorNumElements() / 2;
3997 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003998 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003999 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004000 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004001 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004002 return false;
4003 return true;
4004}
4005
David Greenec38a03e2011-02-03 15:50:00 +00004006/// isVEXTRACTF128Index - Return true if the specified
4007/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4008/// suitable for input to VEXTRACTF128.
4009bool X86::isVEXTRACTF128Index(SDNode *N) {
4010 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4011 return false;
4012
4013 // The index should be aligned on a 128-bit boundary.
4014 uint64_t Index =
4015 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4016
Craig Topper5141d972013-01-18 08:41:28 +00004017 MVT VT = N->getValueType(0).getSimpleVT();
4018 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004019 bool Result = (Index * ElSize) % 128 == 0;
4020
4021 return Result;
4022}
4023
David Greeneccacdc12011-02-04 16:08:29 +00004024/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4025/// operand specifies a subvector insert that is suitable for input to
4026/// VINSERTF128.
4027bool X86::isVINSERTF128Index(SDNode *N) {
4028 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4029 return false;
4030
4031 // The index should be aligned on a 128-bit boundary.
4032 uint64_t Index =
4033 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4034
Craig Topper5141d972013-01-18 08:41:28 +00004035 MVT VT = N->getValueType(0).getSimpleVT();
4036 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004037 bool Result = (Index * ElSize) % 128 == 0;
4038
4039 return Result;
4040}
4041
Evan Cheng63d33002006-03-22 08:01:21 +00004042/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004043/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004044/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004045static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004046 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004047
Craig Topper1a7700a2012-01-19 08:19:12 +00004048 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4049 "Unsupported vector type for PSHUF/SHUFP");
4050
4051 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4052 // independently on 128-bit lanes.
4053 unsigned NumElts = VT.getVectorNumElements();
4054 unsigned NumLanes = VT.getSizeInBits()/128;
4055 unsigned NumLaneElts = NumElts/NumLanes;
4056
4057 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4058 "Only supports 2 or 4 elements per lane");
4059
4060 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004061 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004062 for (unsigned i = 0; i != NumElts; ++i) {
4063 int Elt = N->getMaskElt(i);
4064 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004065 Elt &= NumLaneElts - 1;
4066 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004067 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004068 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004069
Evan Cheng63d33002006-03-22 08:01:21 +00004070 return Mask;
4071}
4072
Evan Cheng506d3df2006-03-29 23:07:14 +00004073/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004074/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004075static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004076 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004077
4078 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4079 "Unsupported vector type for PSHUFHW");
4080
4081 unsigned NumElts = VT.getVectorNumElements();
4082
Evan Cheng506d3df2006-03-29 23:07:14 +00004083 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004084 for (unsigned l = 0; l != NumElts; l += 8) {
4085 // 8 nodes per lane, but we only care about the last 4.
4086 for (unsigned i = 0; i < 4; ++i) {
4087 int Elt = N->getMaskElt(l+i+4);
4088 if (Elt < 0) continue;
4089 Elt &= 0x3; // only 2-bits.
4090 Mask |= Elt << (i * 2);
4091 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004092 }
Craig Topper6b28d352012-05-03 07:12:59 +00004093
Evan Cheng506d3df2006-03-29 23:07:14 +00004094 return Mask;
4095}
4096
4097/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004098/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004099static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004100 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004101
4102 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4103 "Unsupported vector type for PSHUFHW");
4104
4105 unsigned NumElts = VT.getVectorNumElements();
4106
Evan Cheng506d3df2006-03-29 23:07:14 +00004107 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004108 for (unsigned l = 0; l != NumElts; l += 8) {
4109 // 8 nodes per lane, but we only care about the first 4.
4110 for (unsigned i = 0; i < 4; ++i) {
4111 int Elt = N->getMaskElt(l+i);
4112 if (Elt < 0) continue;
4113 Elt &= 0x3; // only 2-bits
4114 Mask |= Elt << (i * 2);
4115 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004116 }
Craig Topper6b28d352012-05-03 07:12:59 +00004117
Evan Cheng506d3df2006-03-29 23:07:14 +00004118 return Mask;
4119}
4120
Nate Begemana09008b2009-10-19 02:17:23 +00004121/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4122/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004123static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004124 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004125 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004126
Craig Topper0e2037b2012-01-20 05:53:00 +00004127 unsigned NumElts = VT.getVectorNumElements();
4128 unsigned NumLanes = VT.getSizeInBits()/128;
4129 unsigned NumLaneElts = NumElts/NumLanes;
4130
4131 int Val = 0;
4132 unsigned i;
4133 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004134 Val = SVOp->getMaskElt(i);
4135 if (Val >= 0)
4136 break;
4137 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004138 if (Val >= (int)NumElts)
4139 Val -= NumElts - NumLaneElts;
4140
Eli Friedman63f8dde2011-07-25 21:36:45 +00004141 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004142 return (Val - i) * EltSize;
4143}
4144
David Greenec38a03e2011-02-03 15:50:00 +00004145/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4146/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4147/// instructions.
4148unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4149 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4150 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4151
4152 uint64_t Index =
4153 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4154
Craig Toppercfcab212013-01-19 08:27:45 +00004155 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4156 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004157
4158 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004159 return Index / NumElemsPerChunk;
4160}
4161
David Greeneccacdc12011-02-04 16:08:29 +00004162/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4163/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4164/// instructions.
4165unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4166 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4167 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4168
4169 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004170 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004171
Craig Toppercfcab212013-01-19 08:27:45 +00004172 MVT VecVT = N->getValueType(0).getSimpleVT();
4173 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004174
4175 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004176 return Index / NumElemsPerChunk;
4177}
4178
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004179/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4180/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4181/// Handles 256-bit.
4182static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004183 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004184
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004185 unsigned NumElts = VT.getVectorNumElements();
4186
Craig Topper095c5282012-04-15 23:48:57 +00004187 assert((VT.is256BitVector() && NumElts == 4) &&
4188 "Unsupported vector type for VPERMQ/VPERMPD");
4189
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004190 unsigned Mask = 0;
4191 for (unsigned i = 0; i != NumElts; ++i) {
4192 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004193 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004194 continue;
4195 Mask |= Elt << (i*2);
4196 }
4197
4198 return Mask;
4199}
Evan Cheng37b73872009-07-30 08:33:02 +00004200/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4201/// constant +0.0.
4202bool X86::isZeroNode(SDValue Elt) {
4203 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004204 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004205 (isa<ConstantFPSDNode>(Elt) &&
4206 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4207}
4208
Nate Begeman9008ca62009-04-27 18:41:29 +00004209/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4210/// their permute mask.
4211static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4212 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004213 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004214 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004215 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004216
Nate Begeman5a5ca152009-04-29 05:20:52 +00004217 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004218 int Idx = SVOp->getMaskElt(i);
4219 if (Idx >= 0) {
4220 if (Idx < (int)NumElems)
4221 Idx += NumElems;
4222 else
4223 Idx -= NumElems;
4224 }
4225 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004226 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004227 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4228 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229}
4230
Evan Cheng533a0aa2006-04-19 20:35:22 +00004231/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4232/// match movhlps. The lower half elements should come from upper half of
4233/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004234/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004235static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004236 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004237 return false;
4238 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004239 return false;
4240 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004241 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004242 return false;
4243 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004244 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004245 return false;
4246 return true;
4247}
4248
Evan Cheng5ced1d82006-04-06 23:23:56 +00004249/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004250/// is promoted to a vector. It also returns the LoadSDNode by reference if
4251/// required.
4252static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004253 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4254 return false;
4255 N = N->getOperand(0).getNode();
4256 if (!ISD::isNON_EXTLoad(N))
4257 return false;
4258 if (LD)
4259 *LD = cast<LoadSDNode>(N);
4260 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004261}
4262
Dan Gohman65fd6562011-11-03 21:49:52 +00004263// Test whether the given value is a vector value which will be legalized
4264// into a load.
4265static bool WillBeConstantPoolLoad(SDNode *N) {
4266 if (N->getOpcode() != ISD::BUILD_VECTOR)
4267 return false;
4268
4269 // Check for any non-constant elements.
4270 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4271 switch (N->getOperand(i).getNode()->getOpcode()) {
4272 case ISD::UNDEF:
4273 case ISD::ConstantFP:
4274 case ISD::Constant:
4275 break;
4276 default:
4277 return false;
4278 }
4279
4280 // Vectors of all-zeros and all-ones are materialized with special
4281 // instructions rather than being loaded.
4282 return !ISD::isBuildVectorAllZeros(N) &&
4283 !ISD::isBuildVectorAllOnes(N);
4284}
4285
Evan Cheng533a0aa2006-04-19 20:35:22 +00004286/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4287/// match movlp{s|d}. The lower half elements should come from lower half of
4288/// V1 (and in order), and the upper half elements should come from the upper
4289/// half of V2 (and in order). And since V1 will become the source of the
4290/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004291static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004292 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004293 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004294 return false;
4295
Evan Cheng466685d2006-10-09 20:57:25 +00004296 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004297 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004298 // Is V2 is a vector load, don't do this transformation. We will try to use
4299 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004300 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004301 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004302
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004303 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004304
Evan Cheng533a0aa2006-04-19 20:35:22 +00004305 if (NumElems != 2 && NumElems != 4)
4306 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004307 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004308 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004309 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004310 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004311 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004312 return false;
4313 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004314}
4315
Evan Cheng39623da2006-04-20 08:58:49 +00004316/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4317/// all the same.
4318static bool isSplatVector(SDNode *N) {
4319 if (N->getOpcode() != ISD::BUILD_VECTOR)
4320 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004321
Dan Gohman475871a2008-07-27 21:46:04 +00004322 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004323 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4324 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004325 return false;
4326 return true;
4327}
4328
Evan Cheng213d2cf2007-05-17 18:45:50 +00004329/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004330/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004331/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004332static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004333 SDValue V1 = N->getOperand(0);
4334 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004335 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4336 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004338 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004339 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004340 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4341 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004342 if (Opc != ISD::BUILD_VECTOR ||
4343 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004344 return false;
4345 } else if (Idx >= 0) {
4346 unsigned Opc = V1.getOpcode();
4347 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4348 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004349 if (Opc != ISD::BUILD_VECTOR ||
4350 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004351 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004352 }
4353 }
4354 return true;
4355}
4356
4357/// getZeroVector - Returns a vector of specified type with all zero elements.
4358///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004359static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004360 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004361 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004362
Dale Johannesen0488fb62010-09-30 23:57:10 +00004363 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004364 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004366 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004367 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004368 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4369 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4370 } else { // SSE1
4371 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4372 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4373 }
Craig Topper5a529e42013-01-18 06:44:29 +00004374 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004375 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004376 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4377 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4378 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4379 } else {
4380 // 256-bit logic and arithmetic instructions in AVX are all
4381 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4382 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4383 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4384 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4385 }
Craig Topper9d352402012-04-23 07:24:41 +00004386 } else
4387 llvm_unreachable("Unexpected vector type");
4388
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004389 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004390}
4391
Chris Lattner8a594482007-11-25 00:24:49 +00004392/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004393/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4394/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4395/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004396static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004397 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004398 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004399
Owen Anderson825b72b2009-08-11 20:47:22 +00004400 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004401 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004402 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004403 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004404 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4405 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4406 } else { // AVX
4407 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004408 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004409 }
Craig Topper5a529e42013-01-18 06:44:29 +00004410 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004411 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004412 } else
4413 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004414
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004415 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004416}
4417
Evan Cheng39623da2006-04-20 08:58:49 +00004418/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4419/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004420static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004421 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004422 if (Mask[i] > (int)NumElems) {
4423 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004424 }
Evan Cheng39623da2006-04-20 08:58:49 +00004425 }
Evan Cheng39623da2006-04-20 08:58:49 +00004426}
4427
Evan Cheng017dcc62006-04-21 01:05:10 +00004428/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4429/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004430static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 SDValue V2) {
4432 unsigned NumElems = VT.getVectorNumElements();
4433 SmallVector<int, 8> Mask;
4434 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004435 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 Mask.push_back(i);
4437 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004438}
4439
Nate Begeman9008ca62009-04-27 18:41:29 +00004440/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004441static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 SDValue V2) {
4443 unsigned NumElems = VT.getVectorNumElements();
4444 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004445 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 Mask.push_back(i);
4447 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004448 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004450}
4451
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004453static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 SDValue V2) {
4455 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004456 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004457 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 Mask.push_back(i + Half);
4459 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004460 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004461 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004462}
4463
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004464// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004465// a generic shuffle instruction because the target has no such instructions.
4466// Generate shuffles which repeat i16 and i8 several times until they can be
4467// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004468static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004471 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004472
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 while (NumElems > 4) {
4474 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004475 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004477 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 EltNo -= NumElems/2;
4479 }
4480 NumElems >>= 1;
4481 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004482 return V;
4483}
Eric Christopherfd179292009-08-27 18:07:15 +00004484
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004485/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4486static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4487 EVT VT = V.getValueType();
4488 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489
Craig Topper5a529e42013-01-18 06:44:29 +00004490 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004491 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004492 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004493 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4494 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004495 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004496 // To use VPERMILPS to splat scalars, the second half of indicies must
4497 // refer to the higher part, which is a duplication of the lower one,
4498 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4500 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004501
4502 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4503 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4504 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004505 } else
4506 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507
4508 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4509}
4510
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004511/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004512static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4513 EVT SrcVT = SV->getValueType(0);
4514 SDValue V1 = SV->getOperand(0);
4515 DebugLoc dl = SV->getDebugLoc();
4516
4517 int EltNo = SV->getSplatIndex();
4518 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004519 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004520
Craig Topper5a529e42013-01-18 06:44:29 +00004521 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4522 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004523
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004524 // Extract the 128-bit part containing the splat element and update
4525 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004526 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004527 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4528 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 EltNo -= NumElems/2;
4530 }
4531
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004532 // All i16 and i8 vector types can't be used directly by a generic shuffle
4533 // instruction because the target has no such instruction. Generate shuffles
4534 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004535 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004536 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004537 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004538 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004539
4540 // Recreate the 256-bit vector and place the same 128-bit vector
4541 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004542 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004543 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004544 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004545 }
4546
4547 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004548}
4549
Evan Chengba05f722006-04-21 23:03:30 +00004550/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004551/// vector of zero or undef vector. This produces a shuffle where the low
4552/// element of V2 is swizzled into the zero/undef vector, landing at element
4553/// 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 +00004554static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004555 bool IsZero,
4556 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004557 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004558 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004559 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004560 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 unsigned NumElems = VT.getVectorNumElements();
4562 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004563 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004564 // If this is the insertion idx, put the low elt of V2 here.
4565 MaskVec.push_back(i == Idx ? NumElems : i);
4566 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004567}
4568
Craig Toppera1ffc682012-03-20 06:42:26 +00004569/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4570/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004571/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004572static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004573 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004574 unsigned NumElems = VT.getVectorNumElements();
4575 SDValue ImmN;
4576
Craig Topper89f4e662012-03-20 07:17:59 +00004577 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004578 switch(N->getOpcode()) {
4579 case X86ISD::SHUFP:
4580 ImmN = N->getOperand(N->getNumOperands()-1);
4581 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4582 break;
4583 case X86ISD::UNPCKH:
4584 DecodeUNPCKHMask(VT, Mask);
4585 break;
4586 case X86ISD::UNPCKL:
4587 DecodeUNPCKLMask(VT, Mask);
4588 break;
4589 case X86ISD::MOVHLPS:
4590 DecodeMOVHLPSMask(NumElems, Mask);
4591 break;
4592 case X86ISD::MOVLHPS:
4593 DecodeMOVLHPSMask(NumElems, Mask);
4594 break;
4595 case X86ISD::PSHUFD:
4596 case X86ISD::VPERMILP:
4597 ImmN = N->getOperand(N->getNumOperands()-1);
4598 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004599 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004600 break;
4601 case X86ISD::PSHUFHW:
4602 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004603 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004604 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004605 break;
4606 case X86ISD::PSHUFLW:
4607 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004608 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004609 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004610 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004611 case X86ISD::VPERMI:
4612 ImmN = N->getOperand(N->getNumOperands()-1);
4613 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4614 IsUnary = true;
4615 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004616 case X86ISD::MOVSS:
4617 case X86ISD::MOVSD: {
4618 // The index 0 always comes from the first element of the second source,
4619 // this is why MOVSS and MOVSD are used in the first place. The other
4620 // elements come from the other positions of the first source vector
4621 Mask.push_back(NumElems);
4622 for (unsigned i = 1; i != NumElems; ++i) {
4623 Mask.push_back(i);
4624 }
4625 break;
4626 }
4627 case X86ISD::VPERM2X128:
4628 ImmN = N->getOperand(N->getNumOperands()-1);
4629 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004630 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004631 break;
4632 case X86ISD::MOVDDUP:
4633 case X86ISD::MOVLHPD:
4634 case X86ISD::MOVLPD:
4635 case X86ISD::MOVLPS:
4636 case X86ISD::MOVSHDUP:
4637 case X86ISD::MOVSLDUP:
4638 case X86ISD::PALIGN:
4639 // Not yet implemented
4640 return false;
4641 default: llvm_unreachable("unknown target shuffle node");
4642 }
4643
4644 return true;
4645}
4646
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004647/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4648/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004649static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004650 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004651 if (Depth == 6)
4652 return SDValue(); // Limit search depth.
4653
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 SDValue V = SDValue(N, 0);
4655 EVT VT = V.getValueType();
4656 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657
4658 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4659 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004660 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004661
Craig Topper3d092db2012-03-21 02:14:01 +00004662 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004663 return DAG.getUNDEF(VT.getVectorElementType());
4664
Craig Topperd156dc12012-02-06 07:17:51 +00004665 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004666 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4667 : SV->getOperand(1);
4668 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004669 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004670
4671 // Recurse into target specific vector shuffles to find scalars.
4672 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004673 MVT ShufVT = V.getValueType().getSimpleVT();
4674 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004675 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004676 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004677
Craig Topperd978c542012-05-06 19:46:21 +00004678 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004679 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004680
Craig Topper3d092db2012-03-21 02:14:01 +00004681 int Elt = ShuffleMask[Index];
4682 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004683 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004684
Craig Topper3d092db2012-03-21 02:14:01 +00004685 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004686 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004687 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004688 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689 }
4690
4691 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004692 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693 V = V.getOperand(0);
4694 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004695 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004696
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004697 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 return SDValue();
4699 }
4700
4701 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4702 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004703 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004704
4705 if (V.getOpcode() == ISD::BUILD_VECTOR)
4706 return V.getOperand(Index);
4707
4708 return SDValue();
4709}
4710
4711/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4712/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004713/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004714static
Craig Topper3d092db2012-03-21 02:14:01 +00004715unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004716 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004717 unsigned i;
4718 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004719 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004720 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004721 if (!(Elt.getNode() &&
4722 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4723 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004724 }
4725
4726 return i;
4727}
4728
Craig Topper3d092db2012-03-21 02:14:01 +00004729/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4730/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004731/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4732static
Craig Topper3d092db2012-03-21 02:14:01 +00004733bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4734 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4735 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004736 bool SeenV1 = false;
4737 bool SeenV2 = false;
4738
Craig Topper3d092db2012-03-21 02:14:01 +00004739 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 int Idx = SVOp->getMaskElt(i);
4741 // Ignore undef indicies
4742 if (Idx < 0)
4743 continue;
4744
Craig Topper3d092db2012-03-21 02:14:01 +00004745 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 SeenV1 = true;
4747 else
4748 SeenV2 = true;
4749
4750 // Only accept consecutive elements from the same vector
4751 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4752 return false;
4753 }
4754
4755 OpNum = SeenV1 ? 0 : 1;
4756 return true;
4757}
4758
4759/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4760/// logical left shift of a vector.
4761static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4762 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4763 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4764 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4765 false /* check zeros from right */, DAG);
4766 unsigned OpSrc;
4767
4768 if (!NumZeros)
4769 return false;
4770
4771 // Considering the elements in the mask that are not consecutive zeros,
4772 // check if they consecutively come from only one of the source vectors.
4773 //
4774 // V1 = {X, A, B, C} 0
4775 // \ \ \ /
4776 // vector_shuffle V1, V2 <1, 2, 3, X>
4777 //
4778 if (!isShuffleMaskConsecutive(SVOp,
4779 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004780 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004781 NumZeros, // Where to start looking in the src vector
4782 NumElems, // Number of elements in vector
4783 OpSrc)) // Which source operand ?
4784 return false;
4785
4786 isLeft = false;
4787 ShAmt = NumZeros;
4788 ShVal = SVOp->getOperand(OpSrc);
4789 return true;
4790}
4791
4792/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4793/// logical left shift of a vector.
4794static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4795 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4796 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4797 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4798 true /* check zeros from left */, DAG);
4799 unsigned OpSrc;
4800
4801 if (!NumZeros)
4802 return false;
4803
4804 // Considering the elements in the mask that are not consecutive zeros,
4805 // check if they consecutively come from only one of the source vectors.
4806 //
4807 // 0 { A, B, X, X } = V2
4808 // / \ / /
4809 // vector_shuffle V1, V2 <X, X, 4, 5>
4810 //
4811 if (!isShuffleMaskConsecutive(SVOp,
4812 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004813 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004814 0, // Where to start looking in the src vector
4815 NumElems, // Number of elements in vector
4816 OpSrc)) // Which source operand ?
4817 return false;
4818
4819 isLeft = true;
4820 ShAmt = NumZeros;
4821 ShVal = SVOp->getOperand(OpSrc);
4822 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004823}
4824
4825/// isVectorShift - Returns true if the shuffle can be implemented as a
4826/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004827static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004828 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004829 // Although the logic below support any bitwidth size, there are no
4830 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004831 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004832 return false;
4833
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004834 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4835 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4836 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004837
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004838 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004839}
4840
Evan Chengc78d3b42006-04-24 18:01:45 +00004841/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4842///
Dan Gohman475871a2008-07-27 21:46:04 +00004843static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004844 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004845 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004846 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004847 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004848 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004849 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004850
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004851 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 bool First = true;
4854 for (unsigned i = 0; i < 16; ++i) {
4855 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4856 if (ThisIsNonZero && First) {
4857 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004858 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004860 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004861 First = false;
4862 }
4863
4864 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004865 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004866 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4867 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004868 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004870 }
4871 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4873 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4874 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004877 } else
4878 ThisElt = LastElt;
4879
Gabor Greifba36cb52008-08-28 21:40:38 +00004880 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004882 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004883 }
4884 }
4885
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004886 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004887}
4888
Bill Wendlinga348c562007-03-22 18:42:45 +00004889/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004890///
Dan Gohman475871a2008-07-27 21:46:04 +00004891static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004892 unsigned NumNonZero, unsigned NumZero,
4893 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004894 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004895 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004896 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004897 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004898
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004899 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004900 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004901 bool First = true;
4902 for (unsigned i = 0; i < 8; ++i) {
4903 bool isNonZero = (NonZeros & (1 << i)) != 0;
4904 if (isNonZero) {
4905 if (First) {
4906 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004907 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004908 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004910 First = false;
4911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004912 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004913 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004914 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004915 }
4916 }
4917
4918 return V;
4919}
4920
Evan Chengf26ffe92008-05-29 08:22:04 +00004921/// getVShift - Return a vector logical shift node.
4922///
Owen Andersone50ed302009-08-10 22:56:29 +00004923static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004924 unsigned NumBits, SelectionDAG &DAG,
4925 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004926 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004927 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004928 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004929 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4930 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004931 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004932 DAG.getConstant(NumBits,
4933 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004934}
4935
Dan Gohman475871a2008-07-27 21:46:04 +00004936SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004937X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004938 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004939
Evan Chengc3630942009-12-09 21:00:30 +00004940 // Check if the scalar load can be widened into a vector load. And if
4941 // the address is "base + cst" see if the cst can be "absorbed" into
4942 // the shuffle mask.
4943 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4944 SDValue Ptr = LD->getBasePtr();
4945 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4946 return SDValue();
4947 EVT PVT = LD->getValueType(0);
4948 if (PVT != MVT::i32 && PVT != MVT::f32)
4949 return SDValue();
4950
4951 int FI = -1;
4952 int64_t Offset = 0;
4953 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4954 FI = FINode->getIndex();
4955 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004956 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004957 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4958 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4959 Offset = Ptr.getConstantOperandVal(1);
4960 Ptr = Ptr.getOperand(0);
4961 } else {
4962 return SDValue();
4963 }
4964
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 // FIXME: 256-bit vector instructions don't require a strict alignment,
4966 // improve this code to support it better.
4967 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004968 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004969 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004970 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004971 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004972 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004973 // Can't change the alignment. FIXME: It's possible to compute
4974 // the exact stack offset and reference FI + adjust offset instead.
4975 // If someone *really* cares about this. That's the way to implement it.
4976 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004977 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004978 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004979 }
4980 }
4981
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004983 // Ptr + (Offset & ~15).
4984 if (Offset < 0)
4985 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004986 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004987 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004988 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004989 if (StartOffset)
4990 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4991 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4992
4993 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004994 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004995
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4997 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004998 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004999 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005000
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005001 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005002 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005003 Mask.push_back(EltNo);
5004
Craig Toppercc3000632012-01-30 07:50:31 +00005005 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005006 }
5007
5008 return SDValue();
5009}
5010
Michael J. Spencerec38de22010-10-10 22:04:20 +00005011/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5012/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005013/// load which has the same value as a build_vector whose operands are 'elts'.
5014///
5015/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005016///
Nate Begeman1449f292010-03-24 22:19:06 +00005017/// FIXME: we'd also like to handle the case where the last elements are zero
5018/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5019/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005020static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005021 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 EVT EltVT = VT.getVectorElementType();
5023 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005024
Nate Begemanfdea31a2010-03-24 20:49:50 +00005025 LoadSDNode *LDBase = NULL;
5026 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005027
Nate Begeman1449f292010-03-24 22:19:06 +00005028 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005029 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005030 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 for (unsigned i = 0; i < NumElems; ++i) {
5032 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005033
Nate Begemanfdea31a2010-03-24 20:49:50 +00005034 if (!Elt.getNode() ||
5035 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5036 return SDValue();
5037 if (!LDBase) {
5038 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5039 return SDValue();
5040 LDBase = cast<LoadSDNode>(Elt.getNode());
5041 LastLoadedElt = i;
5042 continue;
5043 }
5044 if (Elt.getOpcode() == ISD::UNDEF)
5045 continue;
5046
5047 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5048 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5049 return SDValue();
5050 LastLoadedElt = i;
5051 }
Nate Begeman1449f292010-03-24 22:19:06 +00005052
5053 // If we have found an entire vector of loads and undefs, then return a large
5054 // load of the entire vector width starting at the base pointer. If we found
5055 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005056 if (LastLoadedElt == NumElems - 1) {
5057 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005058 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005059 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005060 LDBase->isVolatile(), LDBase->isNonTemporal(),
5061 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005062 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005063 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005064 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005065 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005066 }
5067 if (NumElems == 4 && LastLoadedElt == 1 &&
5068 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005069 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5070 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005071 SDValue ResNode =
5072 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5073 LDBase->getPointerInfo(),
5074 LDBase->getAlignment(),
5075 false/*isVolatile*/, true/*ReadMem*/,
5076 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005077
5078 // Make sure the newly-created LOAD is in the same position as LDBase in
5079 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5080 // update uses of LDBase's output chain to use the TokenFactor.
5081 if (LDBase->hasAnyUseOfValue(1)) {
5082 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5083 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5084 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5085 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5086 SDValue(ResNode.getNode(), 1));
5087 }
5088
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005089 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005090 }
5091 return SDValue();
5092}
5093
Nadav Rotem9d68b062012-04-08 12:54:54 +00005094/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5095/// to generate a splat value for the following cases:
5096/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005097/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005098/// a scalar load, or a constant.
5099/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005100/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005101SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005102X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005103 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005104 return SDValue();
5105
Craig Topper45e1c752013-01-20 00:38:18 +00005106 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005107 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005108
Craig Topper5da8a802012-05-04 05:49:51 +00005109 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5110 "Unsupported vector type for broadcast.");
5111
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005112 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005114
Nadav Rotem9d68b062012-04-08 12:54:54 +00005115 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005116 default:
5117 // Unknown pattern found.
5118 return SDValue();
5119
5120 case ISD::BUILD_VECTOR: {
5121 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005122 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005123 return SDValue();
5124
Nadav Rotem9d68b062012-04-08 12:54:54 +00005125 Ld = Op.getOperand(0);
5126 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5127 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005128
5129 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005131 // Constants may have multiple users.
5132 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005133 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005134 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005135 }
5136
5137 case ISD::VECTOR_SHUFFLE: {
5138 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5139
5140 // Shuffles must have a splat mask where the first element is
5141 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005142 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005143 return SDValue();
5144
5145 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005146 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005147 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5148
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005149 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005150 return SDValue();
5151
5152 // Use the register form of the broadcast instruction available on AVX2.
5153 if (VT.is256BitVector())
5154 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5155 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5156 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005157
5158 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005159 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005160 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005161
5162 // The scalar_to_vector node and the suspected
5163 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005164 // Constants may have multiple users.
5165 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005166 return SDValue();
5167 break;
5168 }
5169 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005170
Craig Topper7a9a28b2012-08-12 02:23:29 +00005171 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005172
5173 // Handle the broadcasting a single constant scalar from the constant pool
5174 // into a vector. On Sandybridge it is still better to load a constant vector
5175 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005176 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005177 EVT CVT = Ld.getValueType();
5178 assert(!CVT.isVector() && "Must not broadcast a vector type");
5179 unsigned ScalarSize = CVT.getSizeInBits();
5180
Craig Topper5da8a802012-05-04 05:49:51 +00005181 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005182 const Constant *C = 0;
5183 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5184 C = CI->getConstantIntValue();
5185 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5186 C = CF->getConstantFPValue();
5187
5188 assert(C && "Invalid constant type");
5189
Nadav Rotem154819d2012-04-09 07:45:58 +00005190 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005191 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005192 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005193 MachinePointerInfo::getConstantPool(),
5194 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005195
Nadav Rotem9d68b062012-04-08 12:54:54 +00005196 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5197 }
5198 }
5199
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005200 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005201 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5202
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005203 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005204 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005205 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5206 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5207
5208 // The scalar source must be a normal load.
5209 if (!IsLoad)
5210 return SDValue();
5211
Craig Topper5da8a802012-05-04 05:49:51 +00005212 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005213 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005214
Craig Toppera9376332012-01-10 08:23:59 +00005215 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005216 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005217 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005218 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005219 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005220 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005221
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005222 // Unsupported broadcast.
5223 return SDValue();
5224}
5225
Evan Chengc3630942009-12-09 21:00:30 +00005226SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005227X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5228 EVT VT = Op.getValueType();
5229
5230 // Skip if insert_vec_elt is not supported.
5231 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5232 return SDValue();
5233
5234 DebugLoc DL = Op.getDebugLoc();
5235 unsigned NumElems = Op.getNumOperands();
5236
5237 SDValue VecIn1;
5238 SDValue VecIn2;
5239 SmallVector<unsigned, 4> InsertIndices;
5240 SmallVector<int, 8> Mask(NumElems, -1);
5241
5242 for (unsigned i = 0; i != NumElems; ++i) {
5243 unsigned Opc = Op.getOperand(i).getOpcode();
5244
5245 if (Opc == ISD::UNDEF)
5246 continue;
5247
5248 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5249 // Quit if more than 1 elements need inserting.
5250 if (InsertIndices.size() > 1)
5251 return SDValue();
5252
5253 InsertIndices.push_back(i);
5254 continue;
5255 }
5256
5257 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5258 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5259
5260 // Quit if extracted from vector of different type.
5261 if (ExtractedFromVec.getValueType() != VT)
5262 return SDValue();
5263
5264 // Quit if non-constant index.
5265 if (!isa<ConstantSDNode>(ExtIdx))
5266 return SDValue();
5267
5268 if (VecIn1.getNode() == 0)
5269 VecIn1 = ExtractedFromVec;
5270 else if (VecIn1 != ExtractedFromVec) {
5271 if (VecIn2.getNode() == 0)
5272 VecIn2 = ExtractedFromVec;
5273 else if (VecIn2 != ExtractedFromVec)
5274 // Quit if more than 2 vectors to shuffle
5275 return SDValue();
5276 }
5277
5278 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5279
5280 if (ExtractedFromVec == VecIn1)
5281 Mask[i] = Idx;
5282 else if (ExtractedFromVec == VecIn2)
5283 Mask[i] = Idx + NumElems;
5284 }
5285
5286 if (VecIn1.getNode() == 0)
5287 return SDValue();
5288
5289 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5290 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5291 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5292 unsigned Idx = InsertIndices[i];
5293 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5294 DAG.getIntPtrConstant(Idx));
5295 }
5296
5297 return NV;
5298}
5299
5300SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005301X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005302 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005303
Craig Topper45e1c752013-01-20 00:38:18 +00005304 MVT VT = Op.getValueType().getSimpleVT();
5305 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005306 unsigned NumElems = Op.getNumOperands();
5307
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005308 // Vectors containing all zeros can be matched by pxor and xorps later
5309 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5310 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5311 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005312 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005313 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005315 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005316 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005317
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005318 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005319 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5320 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005321 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005322 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005323 return Op;
5324
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005325 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005326 }
5327
Nadav Rotem154819d2012-04-09 07:45:58 +00005328 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005329 if (Broadcast.getNode())
5330 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005331
Owen Andersone50ed302009-08-10 22:56:29 +00005332 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334 unsigned NumZero = 0;
5335 unsigned NumNonZero = 0;
5336 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005337 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005338 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005341 if (Elt.getOpcode() == ISD::UNDEF)
5342 continue;
5343 Values.insert(Elt);
5344 if (Elt.getOpcode() != ISD::Constant &&
5345 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005346 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005347 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005348 NumZero++;
5349 else {
5350 NonZeros |= (1 << i);
5351 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352 }
5353 }
5354
Chris Lattner97a2a562010-08-26 05:24:29 +00005355 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5356 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005357 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358
Chris Lattner67f453a2008-03-09 05:42:06 +00005359 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005360 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005362 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005363
Chris Lattner62098042008-03-09 01:05:04 +00005364 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5365 // the value are obviously zero, truncate the value to i32 and do the
5366 // insertion that way. Only do this if the value is non-constant or if the
5367 // value is a constant being inserted into element 0. It is cheaper to do
5368 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005370 (!IsAllConstants || Idx == 0)) {
5371 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005372 // Handle SSE only.
5373 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5374 EVT VecVT = MVT::v4i32;
5375 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005376
Chris Lattner62098042008-03-09 01:05:04 +00005377 // Truncate the value (which may itself be a constant) to i32, and
5378 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005379 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005380 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005381 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005382
Chris Lattner62098042008-03-09 01:05:04 +00005383 // Now we have our 32-bit value zero extended in the low element of
5384 // a vector. If Idx != 0, swizzle it into place.
5385 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 SmallVector<int, 4> Mask;
5387 Mask.push_back(Idx);
5388 for (unsigned i = 1; i != VecElts; ++i)
5389 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005390 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005392 }
Craig Topper07a27622012-01-22 03:07:48 +00005393 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005394 }
5395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005396
Chris Lattner19f79692008-03-08 22:59:52 +00005397 // If we have a constant or non-constant insertion into the low element of
5398 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5399 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005400 // depending on what the source datatype is.
5401 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005402 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005403 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005404
5405 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005407 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005408 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005409 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5410 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005411 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005412 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005413 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5414 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005415 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005416 }
5417
5418 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005420 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005421 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005422 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005423 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005424 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005425 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005426 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005427 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005428 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005429 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005430 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005431
5432 // Is it a vector logical left shift?
5433 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005434 X86::isZeroNode(Op.getOperand(0)) &&
5435 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005436 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005437 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005438 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005439 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005440 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005441 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005442
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005443 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005444 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445
Chris Lattner19f79692008-03-08 22:59:52 +00005446 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5447 // is a non-constant being inserted into an element other than the low one,
5448 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5449 // movd/movss) to move this into the low element, then shuffle it into
5450 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005452 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005453
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005455 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005456 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005457 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005458 MaskVec.push_back(i == Idx ? 0 : 1);
5459 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005460 }
5461 }
5462
Chris Lattner67f453a2008-03-09 05:42:06 +00005463 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005464 if (Values.size() == 1) {
5465 if (EVTBits == 32) {
5466 // Instead of a shuffle like this:
5467 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5468 // Check if it's possible to issue this instead.
5469 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5470 unsigned Idx = CountTrailingZeros_32(NonZeros);
5471 SDValue Item = Op.getOperand(Idx);
5472 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5473 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5474 }
Dan Gohman475871a2008-07-27 21:46:04 +00005475 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005476 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005477
Dan Gohmana3941172007-07-24 22:55:08 +00005478 // A vector full of immediates; various special cases are already
5479 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005480 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005481 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005482
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005483 // For AVX-length vectors, build the individual 128-bit pieces and use
5484 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005485 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005486 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005487 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005488 V.push_back(Op.getOperand(i));
5489
5490 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5491
5492 // Build both the lower and upper subvector.
5493 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5494 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5495 NumElems/2);
5496
5497 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005498 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005499 }
5500
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005501 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005502 if (EVTBits == 64) {
5503 if (NumNonZero == 1) {
5504 // One half is zero or undef.
5505 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005506 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005507 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005508 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005509 }
Dan Gohman475871a2008-07-27 21:46:04 +00005510 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005511 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005512
5513 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005514 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005515 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005516 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005517 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005518 }
5519
Bill Wendling826f36f2007-03-28 00:57:11 +00005520 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005522 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005523 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524 }
5525
5526 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005527 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005528 if (NumElems == 4 && NumZero > 0) {
5529 for (unsigned i = 0; i < 4; ++i) {
5530 bool isZero = !(NonZeros & (1 << i));
5531 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005532 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 else
Dale Johannesenace16102009-02-03 19:33:06 +00005534 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005535 }
5536
5537 for (unsigned i = 0; i < 2; ++i) {
5538 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5539 default: break;
5540 case 0:
5541 V[i] = V[i*2]; // Must be a zero vector.
5542 break;
5543 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 break;
5546 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005547 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005548 break;
5549 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005550 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551 break;
5552 }
5553 }
5554
Benjamin Kramer9c683542012-01-30 15:16:21 +00005555 bool Reverse1 = (NonZeros & 0x3) == 2;
5556 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5557 int MaskVec[] = {
5558 Reverse1 ? 1 : 0,
5559 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005560 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5561 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005562 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005563 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 }
5565
Craig Topper7a9a28b2012-08-12 02:23:29 +00005566 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005567 // Check for a build vector of consecutive loads.
5568 for (unsigned i = 0; i < NumElems; ++i)
5569 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005570
Nate Begemanfdea31a2010-03-24 20:49:50 +00005571 // Check for elements which are consecutive loads.
5572 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5573 if (LD.getNode())
5574 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005575
Michael Liaofacace82012-10-19 17:15:18 +00005576 // Check for a build vector from mostly shuffle plus few inserting.
5577 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5578 if (Sh.getNode())
5579 return Sh;
5580
Michael J. Spencerec38de22010-10-10 22:04:20 +00005581 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005582 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005583 SDValue Result;
5584 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5585 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5586 else
5587 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005588
Chris Lattner24faf612010-08-28 17:59:08 +00005589 for (unsigned i = 1; i < NumElems; ++i) {
5590 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5591 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005593 }
5594 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005595 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005596
Chris Lattner6e80e442010-08-28 17:15:43 +00005597 // Otherwise, expand into a number of unpckl*, start by extending each of
5598 // our (non-undef) elements to the full vector width with the element in the
5599 // bottom slot of the vector (which generates no code for SSE).
5600 for (unsigned i = 0; i < NumElems; ++i) {
5601 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5602 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5603 else
5604 V[i] = DAG.getUNDEF(VT);
5605 }
5606
5607 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005608 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5609 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5610 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005611 unsigned EltStride = NumElems >> 1;
5612 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005613 for (unsigned i = 0; i < EltStride; ++i) {
5614 // If V[i+EltStride] is undef and this is the first round of mixing,
5615 // then it is safe to just drop this shuffle: V[i] is already in the
5616 // right place, the one element (since it's the first round) being
5617 // inserted as undef can be dropped. This isn't safe for successive
5618 // rounds because they will permute elements within both vectors.
5619 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5620 EltStride == NumElems/2)
5621 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005622
Chris Lattner6e80e442010-08-28 17:15:43 +00005623 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005624 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005625 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626 }
5627 return V[0];
5628 }
Dan Gohman475871a2008-07-27 21:46:04 +00005629 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005630}
5631
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005632// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5633// to create 256-bit vectors from two other 128-bit ones.
5634static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5635 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005636 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005637
Craig Topper7a9a28b2012-08-12 02:23:29 +00005638 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005639
5640 SDValue V1 = Op.getOperand(0);
5641 SDValue V2 = Op.getOperand(1);
5642 unsigned NumElems = ResVT.getVectorNumElements();
5643
Craig Topper4c7972d2012-04-22 18:15:59 +00005644 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005645}
5646
Craig Topper55b24052012-09-11 06:15:32 +00005647static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005648 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005649
5650 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5651 // from two other 128-bit ones.
5652 return LowerAVXCONCAT_VECTORS(Op, DAG);
5653}
5654
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005655// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005656static SDValue
5657LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5658 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005659 SDValue V1 = SVOp->getOperand(0);
5660 SDValue V2 = SVOp->getOperand(1);
5661 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005662 MVT VT = SVOp->getValueType(0).getSimpleVT();
5663 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005664 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005665
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005666 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5667 return SDValue();
5668 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005669 return SDValue();
5670
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005671 // Check the mask for BLEND and build the value.
5672 unsigned MaskValue = 0;
5673 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005674 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005675 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005676
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005677 // Blend for v16i16 should be symetric for the both lanes.
5678 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005679
Craig Topper9b33ef72013-01-21 06:57:59 +00005680 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005681 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005682 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005683
Craig Topper04f74a12013-01-21 07:25:16 +00005684 if ((EltIdx < 0 || EltIdx == (int)i) &&
5685 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005686 continue;
5687
Craig Topper9b33ef72013-01-21 06:57:59 +00005688 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005689 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005690 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5691 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005692 else
Craig Topper1842ba02012-04-23 06:38:28 +00005693 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005694 }
5695
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005696 // Convert i32 vectors to floating point if it is not AVX2.
5697 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005698 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005699 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005700 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5701 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005702 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5703 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5704 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005705
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005706 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5707 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005708 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005709}
5710
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711// v8i16 shuffles - Prefer shuffles in the following order:
5712// 1. [all] pshuflw, pshufhw, optional move
5713// 2. [ssse3] 1 x pshufb
5714// 3. [ssse3] 2 x pshufb + 1 x por
5715// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005716static SDValue
5717LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5718 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005719 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005720 SDValue V1 = SVOp->getOperand(0);
5721 SDValue V2 = SVOp->getOperand(1);
5722 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005724
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 // Determine if more than 1 of the words in each of the low and high quadwords
5726 // of the result come from the same quadword of one of the two inputs. Undef
5727 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005728 unsigned LoQuad[] = { 0, 0, 0, 0 };
5729 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005730 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005732 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005733 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 MaskVals.push_back(EltIdx);
5735 if (EltIdx < 0) {
5736 ++Quad[0];
5737 ++Quad[1];
5738 ++Quad[2];
5739 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 }
5742 ++Quad[EltIdx / 4];
5743 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005744 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005747 unsigned MaxQuad = 1;
5748 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 if (LoQuad[i] > MaxQuad) {
5750 BestLoQuad = i;
5751 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005752 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005753 }
5754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005756 MaxQuad = 1;
5757 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 if (HiQuad[i] > MaxQuad) {
5759 BestHiQuad = i;
5760 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005761 }
5762 }
5763
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005765 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // single pshufb instruction is necessary. If There are more than 2 input
5767 // quads, disable the next transformation since it does not help SSSE3.
5768 bool V1Used = InputQuads[0] || InputQuads[1];
5769 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005770 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005772 BestLoQuad = InputQuads[0] ? 0 : 1;
5773 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 }
5775 if (InputQuads.count() > 2) {
5776 BestLoQuad = -1;
5777 BestHiQuad = -1;
5778 }
5779 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5782 // the shuffle mask. If a quad is scored as -1, that means that it contains
5783 // words from all 4 input quadwords.
5784 SDValue NewV;
5785 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005786 int MaskV[] = {
5787 BestLoQuad < 0 ? 0 : BestLoQuad,
5788 BestHiQuad < 0 ? 1 : BestHiQuad
5789 };
Eric Christopherfd179292009-08-27 18:07:15 +00005790 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5792 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5793 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5796 // source words for the shuffle, to aid later transformations.
5797 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005798 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005799 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005801 if (idx != (int)i)
5802 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005804 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 AllWordsInNewV = false;
5806 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005807 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005808
Nate Begemanb9a47b82009-02-23 08:49:38 +00005809 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5810 if (AllWordsInNewV) {
5811 for (int i = 0; i != 8; ++i) {
5812 int idx = MaskVals[i];
5813 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005814 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005815 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 if ((idx != i) && idx < 4)
5817 pshufhw = false;
5818 if ((idx != i) && idx > 3)
5819 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005820 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 V1 = NewV;
5822 V2Used = false;
5823 BestLoQuad = 0;
5824 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005825 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005826
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5828 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005829 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005830 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5831 unsigned TargetMask = 0;
5832 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005834 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5835 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5836 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005837 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005838 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005840 }
Eric Christopherfd179292009-08-27 18:07:15 +00005841
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005842 // Promote splats to a larger type which usually leads to more efficient code.
5843 // FIXME: Is this true if pshufb is available?
5844 if (SVOp->isSplat())
5845 return PromoteSplat(SVOp, DAG);
5846
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 // If we have SSSE3, and all words of the result are from 1 input vector,
5848 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5849 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005850 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005852
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005854 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 // mask, and elements that come from V1 in the V2 mask, so that the two
5856 // results can be OR'd together.
5857 bool TwoInputs = V1Used && V2Used;
5858 for (unsigned i = 0; i != 8; ++i) {
5859 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005860 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5861 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005862 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005863 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005865 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005866 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005867 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005868 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005869 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005870 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005871
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 // Calculate the shuffle mask for the second input, shuffle it, and
5873 // OR it with the first shuffled input.
5874 pshufbMask.clear();
5875 for (unsigned i = 0; i != 8; ++i) {
5876 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005877 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5878 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5879 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5880 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005882 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005883 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005884 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005885 MVT::v16i8, &pshufbMask[0], 16));
5886 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005887 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 }
5889
5890 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5891 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005892 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005894 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 for (int i = 0; i != 4; ++i) {
5896 int idx = MaskVals[i];
5897 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 InOrder.set(i);
5899 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005900 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 }
5903 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005905 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005906
Craig Topperdd637ae2012-02-19 05:41:45 +00005907 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5908 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005909 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005910 NewV.getOperand(0),
5911 getShufflePSHUFLWImmediate(SVOp), DAG);
5912 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005913 }
Eric Christopherfd179292009-08-27 18:07:15 +00005914
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5916 // and update MaskVals with the new element order.
5917 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005918 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 for (unsigned i = 4; i != 8; ++i) {
5920 int idx = MaskVals[i];
5921 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 InOrder.set(i);
5923 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005924 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 }
5927 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005928 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005929 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005930
Craig Topperdd637ae2012-02-19 05:41:45 +00005931 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5932 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005933 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005934 NewV.getOperand(0),
5935 getShufflePSHUFHWImmediate(SVOp), DAG);
5936 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005937 }
Eric Christopherfd179292009-08-27 18:07:15 +00005938
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 // In case BestHi & BestLo were both -1, which means each quadword has a word
5940 // from each of the four input quadwords, calculate the InOrder bitvector now
5941 // before falling through to the insert/extract cleanup.
5942 if (BestLoQuad == -1 && BestHiQuad == -1) {
5943 NewV = V1;
5944 for (int i = 0; i != 8; ++i)
5945 if (MaskVals[i] < 0 || MaskVals[i] == i)
5946 InOrder.set(i);
5947 }
Eric Christopherfd179292009-08-27 18:07:15 +00005948
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 // The other elements are put in the right place using pextrw and pinsrw.
5950 for (unsigned i = 0; i != 8; ++i) {
5951 if (InOrder[i])
5952 continue;
5953 int EltIdx = MaskVals[i];
5954 if (EltIdx < 0)
5955 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005956 SDValue ExtOp = (EltIdx < 8) ?
5957 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5958 DAG.getIntPtrConstant(EltIdx)) :
5959 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005960 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005961 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005962 DAG.getIntPtrConstant(i));
5963 }
5964 return NewV;
5965}
5966
5967// v16i8 shuffles - Prefer shuffles in the following order:
5968// 1. [ssse3] 1 x pshufb
5969// 2. [ssse3] 2 x pshufb + 1 x por
5970// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5971static
Nate Begeman9008ca62009-04-27 18:41:29 +00005972SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005973 SelectionDAG &DAG,
5974 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005975 SDValue V1 = SVOp->getOperand(0);
5976 SDValue V2 = SVOp->getOperand(1);
5977 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005978 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005979
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005980 // Promote splats to a larger type which usually leads to more efficient code.
5981 // FIXME: Is this true if pshufb is available?
5982 if (SVOp->isSplat())
5983 return PromoteSplat(SVOp, DAG);
5984
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005986 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005990 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005992
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005994 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 //
5996 // Otherwise, we have elements from both input vectors, and must zero out
5997 // elements that come from V2 in the first mask, and V1 in the second mask
5998 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005999 for (unsigned i = 0; i != 16; ++i) {
6000 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006001 if (EltIdx < 0 || EltIdx >= 16)
6002 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006004 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006006 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006008
6009 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6010 // the 2nd operand if it's undefined or zero.
6011 if (V2.getOpcode() == ISD::UNDEF ||
6012 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006014
Nate Begemanb9a47b82009-02-23 08:49:38 +00006015 // Calculate the shuffle mask for the second input, shuffle it, and
6016 // OR it with the first shuffled input.
6017 pshufbMask.clear();
6018 for (unsigned i = 0; i != 16; ++i) {
6019 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006020 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006021 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006024 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006025 MVT::v16i8, &pshufbMask[0], 16));
6026 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 }
Eric Christopherfd179292009-08-27 18:07:15 +00006028
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 // No SSSE3 - Calculate in place words and then fix all out of place words
6030 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6031 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006032 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6033 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006034 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006035 for (int i = 0; i != 8; ++i) {
6036 int Elt0 = MaskVals[i*2];
6037 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006038
Nate Begemanb9a47b82009-02-23 08:49:38 +00006039 // This word of the result is all undef, skip it.
6040 if (Elt0 < 0 && Elt1 < 0)
6041 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006042
Nate Begemanb9a47b82009-02-23 08:49:38 +00006043 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006044 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006046
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6048 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6049 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006050
6051 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6052 // using a single extract together, load it and store it.
6053 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006055 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006057 DAG.getIntPtrConstant(i));
6058 continue;
6059 }
6060
Nate Begemanb9a47b82009-02-23 08:49:38 +00006061 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006062 // source byte is not also odd, shift the extracted word left 8 bits
6063 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006064 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006065 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006066 DAG.getIntPtrConstant(Elt1 / 2));
6067 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006069 DAG.getConstant(8,
6070 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006071 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006072 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6073 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 }
6075 // If Elt0 is defined, extract it from the appropriate source. If the
6076 // source byte is not also even, shift the extracted word right 8 bits. If
6077 // Elt1 was also defined, OR the extracted values together before
6078 // inserting them in the result.
6079 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006080 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006081 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6082 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006083 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006084 DAG.getConstant(8,
6085 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006086 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6088 DAG.getConstant(0x00FF, MVT::i16));
6089 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006090 : InsElt0;
6091 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006092 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 DAG.getIntPtrConstant(i));
6094 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006095 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006096}
6097
Elena Demikhovsky41789462012-09-06 12:42:01 +00006098// v32i8 shuffles - Translate to VPSHUFB if possible.
6099static
6100SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006101 const X86Subtarget *Subtarget,
6102 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006103 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006104 SDValue V1 = SVOp->getOperand(0);
6105 SDValue V2 = SVOp->getOperand(1);
6106 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006107 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006108
6109 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006110 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6111 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006112
Michael Liao471b9172012-10-03 23:43:52 +00006113 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006114 // (1) one of input vector is undefined or zeroinitializer.
6115 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6116 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006117 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006118 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006119 return SDValue();
6120
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006121 if (V1IsAllZero && !V2IsAllZero) {
6122 CommuteVectorShuffleMask(MaskVals, 32);
6123 V1 = V2;
6124 }
6125 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006126 for (unsigned i = 0; i != 32; i++) {
6127 int EltIdx = MaskVals[i];
6128 if (EltIdx < 0 || EltIdx >= 32)
6129 EltIdx = 0x80;
6130 else {
6131 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6132 // Cross lane is not allowed.
6133 return SDValue();
6134 EltIdx &= 0xf;
6135 }
6136 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6137 }
6138 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6139 DAG.getNode(ISD::BUILD_VECTOR, dl,
6140 MVT::v32i8, &pshufbMask[0], 32));
6141}
6142
Evan Cheng7a831ce2007-12-15 03:00:47 +00006143/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006144/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006145/// done when every pair / quad of shuffle mask elements point to elements in
6146/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006147/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006148static
Nate Begeman9008ca62009-04-27 18:41:29 +00006149SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006150 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006151 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006152 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006153 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006154 MVT NewVT;
6155 unsigned Scale;
6156 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006157 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006158 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6159 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6160 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6161 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6162 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6163 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006164 }
6165
Nate Begeman9008ca62009-04-27 18:41:29 +00006166 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006167 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006168 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006169 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006170 int EltIdx = SVOp->getMaskElt(i+j);
6171 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006172 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006173 if (StartIdx < 0)
6174 StartIdx = (EltIdx / Scale);
6175 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006176 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006177 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006178 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006179 }
6180
Craig Topper11ac1f82012-05-04 04:08:44 +00006181 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6182 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006183 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006184}
6185
Evan Chengd880b972008-05-09 21:53:03 +00006186/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006187///
Craig Topperf84b7502013-01-20 00:50:58 +00006188static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006189 SDValue SrcOp, SelectionDAG &DAG,
6190 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006191 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006192 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006193 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006194 LD = dyn_cast<LoadSDNode>(SrcOp);
6195 if (!LD) {
6196 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6197 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006198 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006199 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006200 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006201 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006202 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006203 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006204 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006205 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006206 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6207 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6208 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006209 SrcOp.getOperand(0)
6210 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006211 }
6212 }
6213 }
6214
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006215 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006216 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006217 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006218 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006219}
6220
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006221/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6222/// which could not be matched by any known target speficic shuffle
6223static SDValue
6224LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006225
6226 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6227 if (NewOp.getNode())
6228 return NewOp;
6229
Craig Topper657a99c2013-01-19 23:36:09 +00006230 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006231
Craig Topper8f35c132012-01-20 09:29:03 +00006232 unsigned NumElems = VT.getVectorNumElements();
6233 unsigned NumLaneElems = NumElems / 2;
6234
Craig Topper8f35c132012-01-20 09:29:03 +00006235 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006236 MVT EltVT = VT.getVectorElementType();
6237 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006238 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006239
Craig Topper9a2b6e12012-04-06 07:45:23 +00006240 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006241 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006242 // Build a shuffle mask for the output, discovering on the fly which
6243 // input vectors to use as shuffle operands (recorded in InputUsed).
6244 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006245 // out with UseBuildVector set.
6246 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006247 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006248 unsigned LaneStart = l * NumLaneElems;
6249 for (unsigned i = 0; i != NumLaneElems; ++i) {
6250 // The mask element. This indexes into the input.
6251 int Idx = SVOp->getMaskElt(i+LaneStart);
6252 if (Idx < 0) {
6253 // the mask element does not index into any input vector.
6254 Mask.push_back(-1);
6255 continue;
6256 }
Craig Topper8f35c132012-01-20 09:29:03 +00006257
Craig Topper9a2b6e12012-04-06 07:45:23 +00006258 // The input vector this mask element indexes into.
6259 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006260
Craig Topper9a2b6e12012-04-06 07:45:23 +00006261 // Turn the index into an offset from the start of the input vector.
6262 Idx -= Input * NumLaneElems;
6263
6264 // Find or create a shuffle vector operand to hold this input.
6265 unsigned OpNo;
6266 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6267 if (InputUsed[OpNo] == Input)
6268 // This input vector is already an operand.
6269 break;
6270 if (InputUsed[OpNo] < 0) {
6271 // Create a new operand for this input vector.
6272 InputUsed[OpNo] = Input;
6273 break;
6274 }
6275 }
6276
6277 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006278 // More than two input vectors used! Give up on trying to create a
6279 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6280 UseBuildVector = true;
6281 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006282 }
6283
6284 // Add the mask index for the new shuffle vector.
6285 Mask.push_back(Idx + OpNo * NumLaneElems);
6286 }
6287
Craig Topper8ae97ba2012-05-21 06:40:16 +00006288 if (UseBuildVector) {
6289 SmallVector<SDValue, 16> SVOps;
6290 for (unsigned i = 0; i != NumLaneElems; ++i) {
6291 // The mask element. This indexes into the input.
6292 int Idx = SVOp->getMaskElt(i+LaneStart);
6293 if (Idx < 0) {
6294 SVOps.push_back(DAG.getUNDEF(EltVT));
6295 continue;
6296 }
6297
6298 // The input vector this mask element indexes into.
6299 int Input = Idx / NumElems;
6300
6301 // Turn the index into an offset from the start of the input vector.
6302 Idx -= Input * NumElems;
6303
6304 // Extract the vector element by hand.
6305 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6306 SVOp->getOperand(Input),
6307 DAG.getIntPtrConstant(Idx)));
6308 }
6309
6310 // Construct the output using a BUILD_VECTOR.
6311 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6312 SVOps.size());
6313 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006314 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006315 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006316 } else {
6317 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006318 (InputUsed[0] % 2) * NumLaneElems,
6319 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006320 // If only one input was used, use an undefined vector for the other.
6321 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6322 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006323 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006324 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006325 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006326 }
6327
6328 Mask.clear();
6329 }
Craig Topper8f35c132012-01-20 09:29:03 +00006330
6331 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006332 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006333}
6334
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006335/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6336/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006337static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006338LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006339 SDValue V1 = SVOp->getOperand(0);
6340 SDValue V2 = SVOp->getOperand(1);
6341 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006342 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006343
Craig Topper7a9a28b2012-08-12 02:23:29 +00006344 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006345
Benjamin Kramer9c683542012-01-30 15:16:21 +00006346 std::pair<int, int> Locs[4];
6347 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006348 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006349
Evan Chengace3c172008-07-22 21:13:36 +00006350 unsigned NumHi = 0;
6351 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006352 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006353 int Idx = PermMask[i];
6354 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006355 Locs[i] = std::make_pair(-1, -1);
6356 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006357 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6358 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006359 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006360 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006361 NumLo++;
6362 } else {
6363 Locs[i] = std::make_pair(1, NumHi);
6364 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006365 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006366 NumHi++;
6367 }
6368 }
6369 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006370
Evan Chengace3c172008-07-22 21:13:36 +00006371 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006372 // If no more than two elements come from either vector. This can be
6373 // implemented with two shuffles. First shuffle gather the elements.
6374 // The second shuffle, which takes the first shuffle as both of its
6375 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006376 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006377
Benjamin Kramer9c683542012-01-30 15:16:21 +00006378 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006379
Benjamin Kramer9c683542012-01-30 15:16:21 +00006380 for (unsigned i = 0; i != 4; ++i)
6381 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006382 unsigned Idx = (i < 2) ? 0 : 4;
6383 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006385 }
Evan Chengace3c172008-07-22 21:13:36 +00006386
Nate Begeman9008ca62009-04-27 18:41:29 +00006387 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006388 }
6389
6390 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006391 // Otherwise, we must have three elements from one vector, call it X, and
6392 // one element from the other, call it Y. First, use a shufps to build an
6393 // intermediate vector with the one element from Y and the element from X
6394 // that will be in the same half in the final destination (the indexes don't
6395 // matter). Then, use a shufps to build the final vector, taking the half
6396 // containing the element from Y from the intermediate, and the other half
6397 // from X.
6398 if (NumHi == 3) {
6399 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006400 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006401 std::swap(V1, V2);
6402 }
6403
6404 // Find the element from V2.
6405 unsigned HiIndex;
6406 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006407 int Val = PermMask[HiIndex];
6408 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006409 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006410 if (Val >= 4)
6411 break;
6412 }
6413
Nate Begeman9008ca62009-04-27 18:41:29 +00006414 Mask1[0] = PermMask[HiIndex];
6415 Mask1[1] = -1;
6416 Mask1[2] = PermMask[HiIndex^1];
6417 Mask1[3] = -1;
6418 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006419
6420 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006421 Mask1[0] = PermMask[0];
6422 Mask1[1] = PermMask[1];
6423 Mask1[2] = HiIndex & 1 ? 6 : 4;
6424 Mask1[3] = HiIndex & 1 ? 4 : 6;
6425 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006426 }
Craig Topper69947b92012-04-23 06:57:04 +00006427
6428 Mask1[0] = HiIndex & 1 ? 2 : 0;
6429 Mask1[1] = HiIndex & 1 ? 0 : 2;
6430 Mask1[2] = PermMask[2];
6431 Mask1[3] = PermMask[3];
6432 if (Mask1[2] >= 0)
6433 Mask1[2] += 4;
6434 if (Mask1[3] >= 0)
6435 Mask1[3] += 4;
6436 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006437 }
6438
6439 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006440 int LoMask[] = { -1, -1, -1, -1 };
6441 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006442
Benjamin Kramer9c683542012-01-30 15:16:21 +00006443 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006444 unsigned MaskIdx = 0;
6445 unsigned LoIdx = 0;
6446 unsigned HiIdx = 2;
6447 for (unsigned i = 0; i != 4; ++i) {
6448 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006449 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006450 MaskIdx = 1;
6451 LoIdx = 0;
6452 HiIdx = 2;
6453 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006454 int Idx = PermMask[i];
6455 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006456 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006457 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006458 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006459 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006460 LoIdx++;
6461 } else {
6462 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006463 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006464 HiIdx++;
6465 }
6466 }
6467
Nate Begeman9008ca62009-04-27 18:41:29 +00006468 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6469 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006470 int MaskOps[] = { -1, -1, -1, -1 };
6471 for (unsigned i = 0; i != 4; ++i)
6472 if (Locs[i].first != -1)
6473 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006474 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006475}
6476
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006477static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006478 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006479 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006480
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006481 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6482 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006483 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6484 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6485 // BUILD_VECTOR (load), undef
6486 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006487
6488 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006489}
6490
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006491static
Evan Cheng835580f2010-10-07 20:50:20 +00006492SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6493 EVT VT = Op.getValueType();
6494
6495 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006496 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6497 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006498 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6499 V1, DAG));
6500}
6501
6502static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006503SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006504 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006505 SDValue V1 = Op.getOperand(0);
6506 SDValue V2 = Op.getOperand(1);
6507 EVT VT = Op.getValueType();
6508
6509 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6510
Craig Topper1accb7e2012-01-10 06:54:16 +00006511 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006512 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6513
Evan Cheng0899f5c2011-08-31 02:05:24 +00006514 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6515 return DAG.getNode(ISD::BITCAST, dl, VT,
6516 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6517 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6518 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006519}
6520
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006521static
6522SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6523 SDValue V1 = Op.getOperand(0);
6524 SDValue V2 = Op.getOperand(1);
6525 EVT VT = Op.getValueType();
6526
6527 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6528 "unsupported shuffle type");
6529
6530 if (V2.getOpcode() == ISD::UNDEF)
6531 V2 = V1;
6532
6533 // v4i32 or v4f32
6534 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6535}
6536
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006537static
Craig Topper1accb7e2012-01-10 06:54:16 +00006538SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006539 SDValue V1 = Op.getOperand(0);
6540 SDValue V2 = Op.getOperand(1);
6541 EVT VT = Op.getValueType();
6542 unsigned NumElems = VT.getVectorNumElements();
6543
6544 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6545 // operand of these instructions is only memory, so check if there's a
6546 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6547 // same masks.
6548 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006550 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006551 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006552 CanFoldLoad = true;
6553
6554 // When V1 is a load, it can be folded later into a store in isel, example:
6555 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6556 // turns into:
6557 // (MOVLPSmr addr:$src1, VR128:$src2)
6558 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006559 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006560 CanFoldLoad = true;
6561
Dan Gohman65fd6562011-11-03 21:49:52 +00006562 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006563 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006564 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006565 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6566
6567 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006568 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006569 if (SVOp->getMaskElt(1) != -1)
6570 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006571 }
6572
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006573 // movl and movlp will both match v2i64, but v2i64 is never matched by
6574 // movl earlier because we make it strict to avoid messing with the movlp load
6575 // folding logic (see the code above getMOVLP call). Match it here then,
6576 // this is horrible, but will stay like this until we move all shuffle
6577 // matching to x86 specific nodes. Note that for the 1st condition all
6578 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006579 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006580 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6581 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006582 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006583 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006584 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006585 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006586
6587 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6588
6589 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006590 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006591 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592}
6593
Michael Liaod9d09602012-10-23 17:34:00 +00006594// Reduce a vector shuffle to zext.
6595SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006596X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006597 // PMOVZX is only available from SSE41.
6598 if (!Subtarget->hasSSE41())
6599 return SDValue();
6600
6601 EVT VT = Op.getValueType();
6602
6603 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006604 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006605 return SDValue();
6606
6607 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6608 DebugLoc DL = Op.getDebugLoc();
6609 SDValue V1 = Op.getOperand(0);
6610 SDValue V2 = Op.getOperand(1);
6611 unsigned NumElems = VT.getVectorNumElements();
6612
6613 // Extending is an unary operation and the element type of the source vector
6614 // won't be equal to or larger than i64.
6615 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6616 VT.getVectorElementType() == MVT::i64)
6617 return SDValue();
6618
6619 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6620 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006621 while ((1U << Shift) < NumElems) {
6622 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006623 break;
6624 Shift += 1;
6625 // The maximal ratio is 8, i.e. from i8 to i64.
6626 if (Shift > 3)
6627 return SDValue();
6628 }
6629
6630 // Check the shuffle mask.
6631 unsigned Mask = (1U << Shift) - 1;
6632 for (unsigned i = 0; i != NumElems; ++i) {
6633 int EltIdx = SVOp->getMaskElt(i);
6634 if ((i & Mask) != 0 && EltIdx != -1)
6635 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006636 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006637 return SDValue();
6638 }
6639
6640 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6641 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6642 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6643
6644 if (!isTypeLegal(NVT))
6645 return SDValue();
6646
6647 // Simplify the operand as it's prepared to be fed into shuffle.
6648 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6649 if (V1.getOpcode() == ISD::BITCAST &&
6650 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6651 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6652 V1.getOperand(0)
6653 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6654 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6655 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006656 ConstantSDNode *CIdx =
6657 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006658 // If it's foldable, i.e. normal load with single use, we will let code
6659 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006660 if (CIdx && CIdx->getZExtValue() == 0 &&
6661 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006662 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6663 }
6664
6665 return DAG.getNode(ISD::BITCAST, DL, VT,
6666 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6667}
6668
Nadav Rotem154819d2012-04-09 07:45:58 +00006669SDValue
6670X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006671 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006672 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006673 DebugLoc dl = Op.getDebugLoc();
6674 SDValue V1 = Op.getOperand(0);
6675 SDValue V2 = Op.getOperand(1);
6676
6677 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006678 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006679
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006680 // Handle splat operations
6681 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006682 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006683 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006684 if (Broadcast.getNode())
6685 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006686 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006687
Michael Liaod9d09602012-10-23 17:34:00 +00006688 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006689 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006690 if (NewOp.getNode())
6691 return NewOp;
6692
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006693 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6694 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006695 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6696 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006697 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006698 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006699 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006700 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006701 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006702 // FIXME: Figure out a cleaner way to do this.
6703 // Try to make use of movq to zero out the top part.
6704 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006705 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006706 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006707 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006708 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6709 NewVT, true, false))
6710 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006711 DAG, Subtarget, dl);
6712 }
6713 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006714 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006715 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006716 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006717 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6718 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6719 DAG, Subtarget, dl);
6720 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006721 }
6722 }
6723 return SDValue();
6724}
6725
Dan Gohman475871a2008-07-27 21:46:04 +00006726SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006727X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006728 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006729 SDValue V1 = Op.getOperand(0);
6730 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006731 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006732 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006733 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006734 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006736 bool V1IsSplat = false;
6737 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006738 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006739 bool HasFp256 = Subtarget->hasFp256();
6740 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006741 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006742 bool OptForSize = MF.getFunction()->getAttributes().
6743 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744
Craig Topper3426a3e2011-11-14 06:46:21 +00006745 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006746
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006747 if (V1IsUndef && V2IsUndef)
6748 return DAG.getUNDEF(VT);
6749
6750 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006751
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006752 // Vector shuffle lowering takes 3 steps:
6753 //
6754 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6755 // narrowing and commutation of operands should be handled.
6756 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6757 // shuffle nodes.
6758 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6759 // so the shuffle can be broken into other shuffles and the legalizer can
6760 // try the lowering again.
6761 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006762 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006763 // be matched during isel, all of them must be converted to a target specific
6764 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006765
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006766 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6767 // narrowing and commutation of operands should be handled. The actual code
6768 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006769 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006770 if (NewOp.getNode())
6771 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006772
Craig Topper5aaffa82012-02-19 02:53:47 +00006773 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6774
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006775 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6776 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006777 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006778 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006779 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006780 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006781
Craig Topperdd637ae2012-02-19 05:41:45 +00006782 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006783 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006784 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006785
Craig Topperdd637ae2012-02-19 05:41:45 +00006786 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006787 return getMOVHighToLow(Op, dl, DAG);
6788
6789 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006790 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006791 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006792 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006793
Craig Topper5aaffa82012-02-19 02:53:47 +00006794 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006795 // The actual implementation will match the mask in the if above and then
6796 // during isel it can match several different instructions, not only pshufd
6797 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006798 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6799 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006800
Craig Topper5aaffa82012-02-19 02:53:47 +00006801 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006802
Craig Topper1accb7e2012-01-10 06:54:16 +00006803 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006804 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6805
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006806 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6807 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6808 DAG);
6809
Craig Topperb3982da2011-12-31 23:50:21 +00006810 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006811 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006812 }
Eric Christopherfd179292009-08-27 18:07:15 +00006813
Evan Chengf26ffe92008-05-29 08:22:04 +00006814 // Check if this can be converted into a logical shift.
6815 bool isLeft = false;
6816 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006818 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006819 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006820 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006821 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006822 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006823 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006824 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006825 }
Eric Christopherfd179292009-08-27 18:07:15 +00006826
Craig Topper5aaffa82012-02-19 02:53:47 +00006827 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006828 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006829 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006830 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006831 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006832 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6833
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006834 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006835 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6836 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006837 }
Eric Christopherfd179292009-08-27 18:07:15 +00006838
Nate Begeman9008ca62009-04-27 18:41:29 +00006839 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006840 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006841 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006842
Craig Topperdd637ae2012-02-19 05:41:45 +00006843 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006844 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006845
Craig Topperdd637ae2012-02-19 05:41:45 +00006846 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006847 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006848
Craig Topperdd637ae2012-02-19 05:41:45 +00006849 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006850 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006851
Craig Topperdd637ae2012-02-19 05:41:45 +00006852 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006853 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006854
Craig Topperdd637ae2012-02-19 05:41:45 +00006855 if (ShouldXformToMOVHLPS(M, VT) ||
6856 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006857 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006858
Evan Chengf26ffe92008-05-29 08:22:04 +00006859 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006860 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006861 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006862 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006863 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006864 }
Eric Christopherfd179292009-08-27 18:07:15 +00006865
Evan Cheng9eca5e82006-10-25 21:49:50 +00006866 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006867 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6868 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006869 V1IsSplat = isSplatVector(V1.getNode());
6870 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006871
Chris Lattner8a594482007-11-25 00:24:49 +00006872 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006873 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6874 CommuteVectorShuffleMask(M, NumElems);
6875 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006876 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006877 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006878 }
6879
Craig Topperbeabc6c2011-12-05 06:56:46 +00006880 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006881 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006882 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006883 return V1;
6884 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6885 // the instruction selector will not match, so get a canonical MOVL with
6886 // swapped operands to undo the commute.
6887 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006888 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006889
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006890 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006891 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006892
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006893 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006894 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006895
Evan Cheng9bbbb982006-10-25 20:48:19 +00006896 if (V2IsSplat) {
6897 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006898 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006899 // new vector_shuffle with the corrected mask.p
6900 SmallVector<int, 8> NewMask(M.begin(), M.end());
6901 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006902 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006903 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006904 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006905 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006906 }
6907
Evan Cheng9eca5e82006-10-25 21:49:50 +00006908 if (Commuted) {
6909 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006910 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006911 CommuteVectorShuffleMask(M, NumElems);
6912 std::swap(V1, V2);
6913 std::swap(V1IsSplat, V2IsSplat);
6914 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006915
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006916 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006917 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006918
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006919 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006920 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006921 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006922
Nate Begeman9008ca62009-04-27 18:41:29 +00006923 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006924 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006925 return CommuteVectorShuffle(SVOp, DAG);
6926
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006927 // The checks below are all present in isShuffleMaskLegal, but they are
6928 // inlined here right now to enable us to directly emit target specific
6929 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006930
Craig Topper0e2037b2012-01-20 05:53:00 +00006931 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006932 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006933 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006934 DAG);
6935
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006936 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6937 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006938 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006939 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006940 }
6941
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006942 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006943 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006944 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006945 DAG);
6946
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006948 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006949 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006950 DAG);
6951
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006952 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006953 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006954 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006955
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006956 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006957 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006958 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006959 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006960
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006961 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006962 // Generate target specific nodes for 128 or 256-bit shuffles only
6963 // supported in the AVX instruction set.
6964 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006965
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006966 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006967 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006968 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6969
Craig Topper70b883b2011-11-28 10:14:51 +00006970 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006971 if (isVPERMILPMask(M, VT, HasFp256)) {
6972 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006973 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006974 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006975 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006976 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006977 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006978
Craig Topper70b883b2011-11-28 10:14:51 +00006979 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006980 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006981 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006982 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006983
Craig Topper1842ba02012-04-23 06:38:28 +00006984 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006985 if (BlendOp.getNode())
6986 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006987
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006988 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006989 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006990 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006991 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006992 }
Craig Topper92040742012-04-16 06:43:40 +00006993 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6994 &permclMask[0], 8);
6995 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006996 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006997 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006998 }
Craig Topper095c5282012-04-15 23:48:57 +00006999
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007000 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007001 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007002 getShuffleCLImmediate(SVOp), DAG);
7003
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007004 //===--------------------------------------------------------------------===//
7005 // Since no target specific shuffle was selected for this generic one,
7006 // lower it into other known shuffles. FIXME: this isn't true yet, but
7007 // this is the plan.
7008 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007009
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007010 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7011 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007012 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007013 if (NewOp.getNode())
7014 return NewOp;
7015 }
7016
7017 if (VT == MVT::v16i8) {
7018 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7019 if (NewOp.getNode())
7020 return NewOp;
7021 }
7022
Elena Demikhovsky41789462012-09-06 12:42:01 +00007023 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007024 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007025 if (NewOp.getNode())
7026 return NewOp;
7027 }
7028
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007029 // Handle all 128-bit wide vectors with 4 elements, and match them with
7030 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007031 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007032 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7033
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007034 // Handle general 256-bit shuffles
7035 if (VT.is256BitVector())
7036 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7037
Dan Gohman475871a2008-07-27 21:46:04 +00007038 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007039}
7040
Craig Topperf84b7502013-01-20 00:50:58 +00007041static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007042 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007043 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007044
Craig Topper45e1c752013-01-20 00:38:18 +00007045 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007046 return SDValue();
7047
Duncan Sands83ec4b62008-06-06 12:08:01 +00007048 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007050 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007051 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007052 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007053 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007054 }
7055
7056 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007057 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7058 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7059 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7061 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007062 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007064 Op.getOperand(0)),
7065 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007067 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007069 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007070 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007071 }
7072
7073 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007074 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7075 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007076 // result has a single use which is a store or a bitcast to i32. And in
7077 // the case of a store, it's not worth it if the index is a constant 0,
7078 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007079 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007080 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007082 if ((User->getOpcode() != ISD::STORE ||
7083 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7084 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007085 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007087 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007089 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007090 Op.getOperand(0)),
7091 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007092 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007093 }
7094
7095 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007096 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007097 if (isa<ConstantSDNode>(Op.getOperand(1)))
7098 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007099 }
Dan Gohman475871a2008-07-27 21:46:04 +00007100 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007101}
7102
Dan Gohman475871a2008-07-27 21:46:04 +00007103SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007104X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7105 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007106 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007107 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007108
David Greene74a579d2011-02-10 16:57:36 +00007109 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007110 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007111
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007112 // If this is a 256-bit vector result, first extract the 128-bit vector and
7113 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007114 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007115 DebugLoc dl = Op.getNode()->getDebugLoc();
7116 unsigned NumElems = VecVT.getVectorNumElements();
7117 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007118 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7119
7120 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007121 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007122
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007123 if (IdxVal >= NumElems/2)
7124 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007125 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007126 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007127 }
7128
Craig Topper7a9a28b2012-08-12 02:23:29 +00007129 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007130
Craig Topperd0a31172012-01-10 06:37:29 +00007131 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007132 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007133 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007134 return Res;
7135 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007136
Craig Topper45e1c752013-01-20 00:38:18 +00007137 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007138 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007140 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007141 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007142 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007143 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007144 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7145 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007146 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007148 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007150 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007151 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007152 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007153 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007154 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007155 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007156 }
7157
7158 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007159 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 if (Idx == 0)
7161 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007162
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007164 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007165 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007166 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007167 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007168 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007169 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007170 }
7171
7172 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007173 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7174 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7175 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007176 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007177 if (Idx == 0)
7178 return Op;
7179
7180 // UNPCKHPD the element to the lowest double word, then movsd.
7181 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7182 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007183 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007184 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007185 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007186 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007187 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007188 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007189 }
7190
Dan Gohman475871a2008-07-27 21:46:04 +00007191 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007192}
7193
Craig Topperf84b7502013-01-20 00:50:58 +00007194static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007195 MVT VT = Op.getValueType().getSimpleVT();
7196 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007197 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007198
Dan Gohman475871a2008-07-27 21:46:04 +00007199 SDValue N0 = Op.getOperand(0);
7200 SDValue N1 = Op.getOperand(1);
7201 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007202
Craig Topper7a9a28b2012-08-12 02:23:29 +00007203 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007204 return SDValue();
7205
Dan Gohman8a55ce42009-09-23 21:02:20 +00007206 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007207 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007208 unsigned Opc;
7209 if (VT == MVT::v8i16)
7210 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007211 else if (VT == MVT::v16i8)
7212 Opc = X86ISD::PINSRB;
7213 else
7214 Opc = X86ISD::PINSRB;
7215
Nate Begeman14d12ca2008-02-11 04:19:36 +00007216 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7217 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 if (N1.getValueType() != MVT::i32)
7219 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7220 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007221 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007222 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007223 }
7224
7225 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007226 // Bits [7:6] of the constant are the source select. This will always be
7227 // zero here. The DAG Combiner may combine an extract_elt index into these
7228 // bits. For example (insert (extract, 3), 2) could be matched by putting
7229 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007230 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007231 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007232 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007233 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007234 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007235 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007236 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007237 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007238 }
7239
7240 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007241 // PINSR* works with constant index.
7242 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243 }
Dan Gohman475871a2008-07-27 21:46:04 +00007244 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007245}
7246
Dan Gohman475871a2008-07-27 21:46:04 +00007247SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007248X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007249 MVT VT = Op.getValueType().getSimpleVT();
7250 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007251
David Greene6b381262011-02-09 15:32:06 +00007252 DebugLoc dl = Op.getDebugLoc();
7253 SDValue N0 = Op.getOperand(0);
7254 SDValue N1 = Op.getOperand(1);
7255 SDValue N2 = Op.getOperand(2);
7256
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007257 // If this is a 256-bit vector result, first extract the 128-bit vector,
7258 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007259 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007260 if (!isa<ConstantSDNode>(N2))
7261 return SDValue();
7262
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007263 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007264 unsigned NumElems = VT.getVectorNumElements();
7265 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007266 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007267
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007268 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007269 bool Upper = IdxVal >= NumElems/2;
7270 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7271 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007272
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007273 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007274 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007275 }
7276
Craig Topperd0a31172012-01-10 06:37:29 +00007277 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007278 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7279
Dan Gohman8a55ce42009-09-23 21:02:20 +00007280 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007281 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007282
Dan Gohman8a55ce42009-09-23 21:02:20 +00007283 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007284 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7285 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007286 if (N1.getValueType() != MVT::i32)
7287 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7288 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007289 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007290 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007291 }
Dan Gohman475871a2008-07-27 21:46:04 +00007292 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007293}
7294
Craig Topper55b24052012-09-11 06:15:32 +00007295static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007296 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007297 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007298 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007299
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007300 // If this is a 256-bit vector result, first insert into a 128-bit
7301 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007302 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007303 // Insert into a 128-bit vector.
7304 EVT VT128 = EVT::getVectorVT(*Context,
7305 OpVT.getVectorElementType(),
7306 OpVT.getVectorNumElements() / 2);
7307
7308 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7309
7310 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007311 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007312 }
7313
Craig Topperd77d2fe2012-04-29 20:22:05 +00007314 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007315 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007317
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007319 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007320 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007321 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007322}
7323
David Greene91585092011-01-26 15:38:49 +00007324// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7325// a simple subregister reference or explicit instructions to grab
7326// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007327static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7328 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007329 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007330 DebugLoc dl = Op.getNode()->getDebugLoc();
7331 SDValue Vec = Op.getNode()->getOperand(0);
7332 SDValue Idx = Op.getNode()->getOperand(1);
7333
Craig Topper7a9a28b2012-08-12 02:23:29 +00007334 if (Op.getNode()->getValueType(0).is128BitVector() &&
7335 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007336 isa<ConstantSDNode>(Idx)) {
7337 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7338 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007339 }
David Greene91585092011-01-26 15:38:49 +00007340 }
7341 return SDValue();
7342}
7343
David Greenecfe33c42011-01-26 19:13:22 +00007344// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7345// simple superregister reference or explicit instructions to insert
7346// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007347static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7348 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007349 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007350 DebugLoc dl = Op.getNode()->getDebugLoc();
7351 SDValue Vec = Op.getNode()->getOperand(0);
7352 SDValue SubVec = Op.getNode()->getOperand(1);
7353 SDValue Idx = Op.getNode()->getOperand(2);
7354
Craig Topper7a9a28b2012-08-12 02:23:29 +00007355 if (Op.getNode()->getValueType(0).is256BitVector() &&
7356 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007357 isa<ConstantSDNode>(Idx)) {
7358 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7359 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007360 }
7361 }
7362 return SDValue();
7363}
7364
Bill Wendling056292f2008-09-16 21:48:12 +00007365// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7366// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7367// one of the above mentioned nodes. It has to be wrapped because otherwise
7368// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7369// be used to form addressing mode. These wrapped nodes will be selected
7370// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007371SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007372X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007373 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007374
Chris Lattner41621a22009-06-26 19:22:52 +00007375 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7376 // global base reg.
7377 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007378 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007379 CodeModel::Model M = getTargetMachine().getCodeModel();
7380
Chris Lattner4f066492009-07-11 20:29:19 +00007381 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007382 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007383 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007384 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007385 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007386 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007387 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007388
Evan Cheng1606e8e2009-03-13 07:51:59 +00007389 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007390 CP->getAlignment(),
7391 CP->getOffset(), OpFlag);
7392 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007393 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007394 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007395 if (OpFlag) {
7396 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007397 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007398 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007399 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007400 }
7401
7402 return Result;
7403}
7404
Dan Gohmand858e902010-04-17 15:26:15 +00007405SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007406 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007407
Chris Lattner18c59872009-06-27 04:16:01 +00007408 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7409 // global base reg.
7410 unsigned char OpFlag = 0;
7411 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007412 CodeModel::Model M = getTargetMachine().getCodeModel();
7413
Chris Lattner4f066492009-07-11 20:29:19 +00007414 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007415 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007416 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007417 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007418 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007419 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007420 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007421
Chris Lattner18c59872009-06-27 04:16:01 +00007422 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7423 OpFlag);
7424 DebugLoc DL = JT->getDebugLoc();
7425 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007426
Chris Lattner18c59872009-06-27 04:16:01 +00007427 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007428 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007429 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7430 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007431 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007432 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007433
Chris Lattner18c59872009-06-27 04:16:01 +00007434 return Result;
7435}
7436
7437SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007438X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007439 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007440
Chris Lattner18c59872009-06-27 04:16:01 +00007441 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7442 // global base reg.
7443 unsigned char OpFlag = 0;
7444 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007445 CodeModel::Model M = getTargetMachine().getCodeModel();
7446
Chris Lattner4f066492009-07-11 20:29:19 +00007447 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007448 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7449 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7450 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007451 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007452 } else if (Subtarget->isPICStyleGOT()) {
7453 OpFlag = X86II::MO_GOT;
7454 } else if (Subtarget->isPICStyleStubPIC()) {
7455 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7456 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7457 OpFlag = X86II::MO_DARWIN_NONLAZY;
7458 }
Eric Christopherfd179292009-08-27 18:07:15 +00007459
Chris Lattner18c59872009-06-27 04:16:01 +00007460 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007461
Chris Lattner18c59872009-06-27 04:16:01 +00007462 DebugLoc DL = Op.getDebugLoc();
7463 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007464
Chris Lattner18c59872009-06-27 04:16:01 +00007465 // With PIC, the address is actually $g + Offset.
7466 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007467 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007468 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7469 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007470 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007471 Result);
7472 }
Eric Christopherfd179292009-08-27 18:07:15 +00007473
Eli Friedman586272d2011-08-11 01:48:05 +00007474 // For symbols that require a load from a stub to get the address, emit the
7475 // load.
7476 if (isGlobalStubReference(OpFlag))
7477 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007478 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007479
Chris Lattner18c59872009-06-27 04:16:01 +00007480 return Result;
7481}
7482
Dan Gohman475871a2008-07-27 21:46:04 +00007483SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007484X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007485 // Create the TargetBlockAddressAddress node.
7486 unsigned char OpFlags =
7487 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007488 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007489 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007490 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007491 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007492 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7493 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007494
Dan Gohmanf705adb2009-10-30 01:28:02 +00007495 if (Subtarget->isPICStyleRIPRel() &&
7496 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007497 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7498 else
7499 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007500
Dan Gohman29cbade2009-11-20 23:18:13 +00007501 // With PIC, the address is actually $g + Offset.
7502 if (isGlobalRelativeToPICBase(OpFlags)) {
7503 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7504 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7505 Result);
7506 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007507
7508 return Result;
7509}
7510
7511SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007512X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007513 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007514 // Create the TargetGlobalAddress node, folding in the constant
7515 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007516 unsigned char OpFlags =
7517 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007518 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007519 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007520 if (OpFlags == X86II::MO_NO_FLAG &&
7521 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007522 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007523 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007524 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007525 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007526 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007527 }
Eric Christopherfd179292009-08-27 18:07:15 +00007528
Chris Lattner4f066492009-07-11 20:29:19 +00007529 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007530 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007531 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7532 else
7533 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007534
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007535 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007536 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007537 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7538 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007539 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007541
Chris Lattner36c25012009-07-10 07:34:39 +00007542 // For globals that require a load from a stub to get the address, emit the
7543 // load.
7544 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007545 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007546 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007547
Dan Gohman6520e202008-10-18 02:06:02 +00007548 // If there was a non-zero offset that we didn't fold, create an explicit
7549 // addition for it.
7550 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007551 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007552 DAG.getConstant(Offset, getPointerTy()));
7553
Evan Cheng0db9fe62006-04-25 20:13:52 +00007554 return Result;
7555}
7556
Evan Chengda43bcf2008-09-24 00:05:32 +00007557SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007558X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007559 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007560 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007561 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007562}
7563
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007564static SDValue
7565GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007566 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007567 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007568 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007569 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007570 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007571 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007572 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007573 GA->getOffset(),
7574 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007575
7576 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7577 : X86ISD::TLSADDR;
7578
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007579 if (InFlag) {
7580 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007581 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007582 } else {
7583 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007584 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007585 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007586
7587 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007588 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007589
Rafael Espindola15f1b662009-04-24 12:59:40 +00007590 SDValue Flag = Chain.getValue(1);
7591 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007592}
7593
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007594// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007595static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007596LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007597 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007598 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007599 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7600 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007601 DAG.getNode(X86ISD::GlobalBaseReg,
7602 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007603 InFlag = Chain.getValue(1);
7604
Chris Lattnerb903bed2009-06-26 21:20:29 +00007605 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007606}
7607
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007608// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007609static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007610LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007611 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007612 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7613 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007614}
7615
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007616static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7617 SelectionDAG &DAG,
7618 const EVT PtrVT,
7619 bool is64Bit) {
7620 DebugLoc dl = GA->getDebugLoc();
7621
7622 // Get the start address of the TLS block for this module.
7623 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7624 .getInfo<X86MachineFunctionInfo>();
7625 MFI->incNumLocalDynamicTLSAccesses();
7626
7627 SDValue Base;
7628 if (is64Bit) {
7629 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7630 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7631 } else {
7632 SDValue InFlag;
7633 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7634 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7635 InFlag = Chain.getValue(1);
7636 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7637 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7638 }
7639
7640 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7641 // of Base.
7642
7643 // Build x@dtpoff.
7644 unsigned char OperandFlags = X86II::MO_DTPOFF;
7645 unsigned WrapperKind = X86ISD::Wrapper;
7646 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7647 GA->getValueType(0),
7648 GA->getOffset(), OperandFlags);
7649 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7650
7651 // Add x@dtpoff with the base.
7652 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7653}
7654
Hans Wennborg228756c2012-05-11 10:11:01 +00007655// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007656static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007657 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007658 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007659 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007661 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7662 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7663 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007664
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007666 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007667 MachinePointerInfo(Ptr),
7668 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007669
Chris Lattnerb903bed2009-06-26 21:20:29 +00007670 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007671 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7672 // initialexec.
7673 unsigned WrapperKind = X86ISD::Wrapper;
7674 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007675 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007676 } else if (model == TLSModel::InitialExec) {
7677 if (is64Bit) {
7678 OperandFlags = X86II::MO_GOTTPOFF;
7679 WrapperKind = X86ISD::WrapperRIP;
7680 } else {
7681 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7682 }
Chris Lattner18c59872009-06-27 04:16:01 +00007683 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007684 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007685 }
Eric Christopherfd179292009-08-27 18:07:15 +00007686
Hans Wennborg228756c2012-05-11 10:11:01 +00007687 // emit "addl x@ntpoff,%eax" (local exec)
7688 // or "addl x@indntpoff,%eax" (initial exec)
7689 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007690 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007691 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007692 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007693 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007694
Hans Wennborg228756c2012-05-11 10:11:01 +00007695 if (model == TLSModel::InitialExec) {
7696 if (isPIC && !is64Bit) {
7697 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7698 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7699 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007700 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007701
7702 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7703 MachinePointerInfo::getGOT(), false, false, false,
7704 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007705 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007706
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007707 // The address of the thread local variable is the add of the thread
7708 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007709 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007710}
7711
Dan Gohman475871a2008-07-27 21:46:04 +00007712SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007713X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007714
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007715 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007716 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007717
Eric Christopher30ef0e52010-06-03 04:07:48 +00007718 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007719 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007720
Eric Christopher30ef0e52010-06-03 04:07:48 +00007721 switch (model) {
7722 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007723 if (Subtarget->is64Bit())
7724 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7725 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007726 case TLSModel::LocalDynamic:
7727 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7728 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007729 case TLSModel::InitialExec:
7730 case TLSModel::LocalExec:
7731 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007732 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007733 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007734 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007735 llvm_unreachable("Unknown TLS model.");
7736 }
7737
7738 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007739 // Darwin only has one model of TLS. Lower to that.
7740 unsigned char OpFlag = 0;
7741 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7742 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007743
Eric Christopher30ef0e52010-06-03 04:07:48 +00007744 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7745 // global base reg.
7746 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7747 !Subtarget->is64Bit();
7748 if (PIC32)
7749 OpFlag = X86II::MO_TLVP_PIC_BASE;
7750 else
7751 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007752 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007753 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007754 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007755 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007756 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 // With PIC32, the address is actually $g + Offset.
7759 if (PIC32)
7760 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7761 DAG.getNode(X86ISD::GlobalBaseReg,
7762 DebugLoc(), getPointerTy()),
7763 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007764
Eric Christopher30ef0e52010-06-03 04:07:48 +00007765 // Lowering the machine isd will make sure everything is in the right
7766 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007767 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007768 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007769 SDValue Args[] = { Chain, Offset };
7770 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007771
Eric Christopher30ef0e52010-06-03 04:07:48 +00007772 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7773 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7774 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007775
Eric Christopher30ef0e52010-06-03 04:07:48 +00007776 // And our return value (tls address) is in the standard call return value
7777 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007778 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007779 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7780 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007781 }
7782
7783 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007784 // Just use the implicit TLS architecture
7785 // Need to generate someting similar to:
7786 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7787 // ; from TEB
7788 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7789 // mov rcx, qword [rdx+rcx*8]
7790 // mov eax, .tls$:tlsvar
7791 // [rax+rcx] contains the address
7792 // Windows 64bit: gs:0x58
7793 // Windows 32bit: fs:__tls_array
7794
7795 // If GV is an alias then use the aliasee for determining
7796 // thread-localness.
7797 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7798 GV = GA->resolveAliasedGlobal(false);
7799 DebugLoc dl = GA->getDebugLoc();
7800 SDValue Chain = DAG.getEntryNode();
7801
7802 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7803 // %gs:0x58 (64-bit).
7804 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7805 ? Type::getInt8PtrTy(*DAG.getContext(),
7806 256)
7807 : Type::getInt32PtrTy(*DAG.getContext(),
7808 257));
7809
7810 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7811 Subtarget->is64Bit()
7812 ? DAG.getIntPtrConstant(0x58)
7813 : DAG.getExternalSymbol("_tls_array",
7814 getPointerTy()),
7815 MachinePointerInfo(Ptr),
7816 false, false, false, 0);
7817
7818 // Load the _tls_index variable
7819 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7820 if (Subtarget->is64Bit())
7821 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7822 IDX, MachinePointerInfo(), MVT::i32,
7823 false, false, 0);
7824 else
7825 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7826 false, false, false, 0);
7827
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007828 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007829 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007830 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7831
7832 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7833 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7834 false, false, false, 0);
7835
7836 // Get the offset of start of .tls section
7837 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7838 GA->getValueType(0),
7839 GA->getOffset(), X86II::MO_SECREL);
7840 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7841
7842 // The address of the thread local variable is the add of the thread
7843 // pointer with the offset of the variable.
7844 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007845 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007846
David Blaikie4d6ccb52012-01-20 21:51:11 +00007847 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007848}
7849
Chad Rosierb90d2a92012-01-03 23:19:12 +00007850/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7851/// and take a 2 x i32 value to shift plus a shift amount.
7852SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007853 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007854 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007855 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007856 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007857 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007858 SDValue ShOpLo = Op.getOperand(0);
7859 SDValue ShOpHi = Op.getOperand(1);
7860 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007861 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007863 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007864
Dan Gohman475871a2008-07-27 21:46:04 +00007865 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007866 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007867 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7868 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007869 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007870 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7871 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007872 }
Evan Chenge3413162006-01-09 18:33:28 +00007873
Owen Anderson825b72b2009-08-11 20:47:22 +00007874 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7875 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007876 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007878
Dan Gohman475871a2008-07-27 21:46:04 +00007879 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007881 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7882 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007883
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007884 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007885 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7886 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007887 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007888 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7889 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007890 }
7891
Dan Gohman475871a2008-07-27 21:46:04 +00007892 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007893 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007894}
Evan Chenga3195e82006-01-12 22:54:21 +00007895
Dan Gohmand858e902010-04-17 15:26:15 +00007896SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7897 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007898 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007899
Dale Johannesen0488fb62010-09-30 23:57:10 +00007900 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007901 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007902
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007904 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007905
Eli Friedman36df4992009-05-27 00:47:34 +00007906 // These are really Legal; return the operand so the caller accepts it as
7907 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007909 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007911 Subtarget->is64Bit()) {
7912 return Op;
7913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007914
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007915 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007916 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007917 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007918 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007919 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007920 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007921 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007922 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007923 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007924 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7925}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007926
Owen Andersone50ed302009-08-10 22:56:29 +00007927SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007928 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007929 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007930 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007931 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007932 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007933 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007934 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007935 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007936 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007938
Chris Lattner492a43e2010-09-22 01:28:21 +00007939 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007940
Stuart Hastings84be9582011-06-02 15:57:11 +00007941 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7942 MachineMemOperand *MMO;
7943 if (FI) {
7944 int SSFI = FI->getIndex();
7945 MMO =
7946 DAG.getMachineFunction()
7947 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7948 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7949 } else {
7950 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7951 StackSlot = StackSlot.getOperand(1);
7952 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007953 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007954 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7955 X86ISD::FILD, DL,
7956 Tys, Ops, array_lengthof(Ops),
7957 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007958
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007959 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962
7963 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7964 // shouldn't be necessary except that RFP cannot be live across
7965 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007966 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007967 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7968 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007969 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007970 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007971 SDValue Ops[] = {
7972 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7973 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 MachineMemOperand *MMO =
7975 DAG.getMachineFunction()
7976 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007977 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007978
Chris Lattner492a43e2010-09-22 01:28:21 +00007979 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7980 Ops, array_lengthof(Ops),
7981 Op.getValueType(), MMO);
7982 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007983 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007984 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007985 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007986
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 return Result;
7988}
7989
Bill Wendling8b8a6362009-01-17 03:56:04 +00007990// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007991SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7992 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007993 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007994 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007995 movq %rax, %xmm0
7996 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7997 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7998 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007999 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008000 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008001 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008002 addpd %xmm1, %xmm0
8003 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008004 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008005
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008006 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008007 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008008
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008009 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008010 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8011 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008012 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008013
Chris Lattner97484792012-01-25 09:56:22 +00008014 SmallVector<Constant*,2> CV1;
8015 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008016 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8017 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008018 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008019 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8020 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008021 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008022 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008023
Bill Wendling397ae212012-01-05 02:13:20 +00008024 // Load the 64-bit value into an XMM register.
8025 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8026 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008027 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008028 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008029 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008030 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8031 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8032 CLod0);
8033
Owen Anderson825b72b2009-08-11 20:47:22 +00008034 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008035 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008036 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008037 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008039 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008040
Craig Topperd0a31172012-01-10 06:37:29 +00008041 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008042 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8043 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8044 } else {
8045 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8046 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8047 S2F, 0x4E, DAG);
8048 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8049 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8050 Sub);
8051 }
8052
8053 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008054 DAG.getIntPtrConstant(0));
8055}
8056
Bill Wendling8b8a6362009-01-17 03:56:04 +00008057// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008058SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8059 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008060 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008061 // FP constant to bias correct the final result.
8062 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008064
8065 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008066 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008067 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008068
Eli Friedmanf3704762011-08-29 21:15:46 +00008069 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008070 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008071
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008073 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008074 DAG.getIntPtrConstant(0));
8075
8076 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008078 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008079 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008081 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008082 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 MVT::v2f64, Bias)));
8084 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008085 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008086 DAG.getIntPtrConstant(0));
8087
8088 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008090
8091 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008092 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008093
Craig Topper69947b92012-04-23 06:57:04 +00008094 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008095 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008096 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008097 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008098 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008099
8100 // Handle final rounding.
8101 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008102}
8103
Michael Liaoa7554632012-10-23 17:36:08 +00008104SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8105 SelectionDAG &DAG) const {
8106 SDValue N0 = Op.getOperand(0);
8107 EVT SVT = N0.getValueType();
8108 DebugLoc dl = Op.getDebugLoc();
8109
8110 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8111 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8112 "Custom UINT_TO_FP is not supported!");
8113
Craig Topperb99bafe2013-01-21 06:21:54 +00008114 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8115 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008116 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8117 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8118}
8119
Dan Gohmand858e902010-04-17 15:26:15 +00008120SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8121 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008122 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008123 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008124
Michael Liaoa7554632012-10-23 17:36:08 +00008125 if (Op.getValueType().isVector())
8126 return lowerUINT_TO_FP_vec(Op, DAG);
8127
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008128 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008129 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8130 // the optimization here.
8131 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008132 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008133
Owen Andersone50ed302009-08-10 22:56:29 +00008134 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008135 EVT DstVT = Op.getValueType();
8136 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008137 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008138 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008139 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008140 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008141 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008142
8143 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008145 if (SrcVT == MVT::i32) {
8146 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8147 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8148 getPointerTy(), StackSlot, WordOff);
8149 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008150 StackSlot, MachinePointerInfo(),
8151 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008152 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008153 OffsetSlot, MachinePointerInfo(),
8154 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008155 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8156 return Fild;
8157 }
8158
8159 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8160 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008161 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008162 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008163 // For i64 source, we need to add the appropriate power of 2 if the input
8164 // was negative. This is the same as the optimization in
8165 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8166 // we must be careful to do the computation in x87 extended precision, not
8167 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008168 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8169 MachineMemOperand *MMO =
8170 DAG.getMachineFunction()
8171 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8172 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008173
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008174 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8175 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008176 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8177 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008178
8179 APInt FF(32, 0x5F800000ULL);
8180
8181 // Check whether the sign bit is set.
8182 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8183 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8184 ISD::SETLT);
8185
8186 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8187 SDValue FudgePtr = DAG.getConstantPool(
8188 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8189 getPointerTy());
8190
8191 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8192 SDValue Zero = DAG.getIntPtrConstant(0);
8193 SDValue Four = DAG.getIntPtrConstant(4);
8194 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8195 Zero, Four);
8196 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8197
8198 // Load the value out, extending it from f32 to f80.
8199 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008200 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008201 FudgePtr, MachinePointerInfo::getConstantPool(),
8202 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008203 // Extend everything to 80 bits to force it to be done on x87.
8204 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8205 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008206}
8207
Craig Topperb99bafe2013-01-21 06:21:54 +00008208std::pair<SDValue,SDValue>
8209X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8210 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008211 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008212
Owen Andersone50ed302009-08-10 22:56:29 +00008213 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008214
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008215 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8217 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008218 }
8219
Owen Anderson825b72b2009-08-11 20:47:22 +00008220 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8221 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008222 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008223
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008224 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008225 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008226 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008227 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008228 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008229 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008230 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008231 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008232
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008233 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8234 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008235 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008236 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008237 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008238 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008239
Evan Cheng0db9fe62006-04-25 20:13:52 +00008240 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008241 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8242 Opc = X86ISD::WIN_FTOL;
8243 else
8244 switch (DstTy.getSimpleVT().SimpleTy) {
8245 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8246 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8247 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8248 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8249 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008250
Dan Gohman475871a2008-07-27 21:46:04 +00008251 SDValue Chain = DAG.getEntryNode();
8252 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008253 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008254 // FIXME This causes a redundant load/store if the SSE-class value is already
8255 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008256 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008258 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008259 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008260 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008261 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008262 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008263 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008264 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008265
Chris Lattner492a43e2010-09-22 01:28:21 +00008266 MachineMemOperand *MMO =
8267 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8268 MachineMemOperand::MOLoad, MemSize, MemSize);
8269 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8270 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008271 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008272 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008273 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8274 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008275
Chris Lattner07290932010-09-22 01:05:16 +00008276 MachineMemOperand *MMO =
8277 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8278 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008279
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008280 if (Opc != X86ISD::WIN_FTOL) {
8281 // Build the FP_TO_INT*_IN_MEM
8282 SDValue Ops[] = { Chain, Value, StackSlot };
8283 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8284 Ops, 3, DstTy, MMO);
8285 return std::make_pair(FIST, StackSlot);
8286 } else {
8287 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8288 DAG.getVTList(MVT::Other, MVT::Glue),
8289 Chain, Value);
8290 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8291 MVT::i32, ftol.getValue(1));
8292 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8293 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008294 SDValue Ops[] = { eax, edx };
8295 SDValue pair = IsReplace
8296 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8297 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008298 return std::make_pair(pair, SDValue());
8299 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008300}
8301
Nadav Rotem0509db22012-12-28 05:45:24 +00008302static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8303 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008304 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008305 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008306 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008307 DebugLoc dl = Op->getDebugLoc();
8308
8309 // Optimize vectors in AVX mode:
8310 //
8311 // v8i16 -> v8i32
8312 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8313 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8314 // Concat upper and lower parts.
8315 //
8316 // v4i32 -> v4i64
8317 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8318 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8319 // Concat upper and lower parts.
8320 //
8321
8322 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8323 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8324 return SDValue();
8325
8326 if (Subtarget->hasInt256())
8327 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8328
8329 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8330 SDValue Undef = DAG.getUNDEF(InVT);
8331 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8332 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8333 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8334
Craig Toppera080daf2013-01-20 21:50:27 +00008335 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008336 VT.getVectorNumElements()/2);
8337
8338 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8339 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8340
8341 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8342}
8343
8344SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8345 SelectionDAG &DAG) const {
8346 if (Subtarget->hasFp256()) {
8347 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8348 if (Res.getNode())
8349 return Res;
8350 }
8351
8352 return SDValue();
8353}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008354SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8355 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008356 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008357 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008358 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008359 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008360
Nadav Rotem0509db22012-12-28 05:45:24 +00008361 if (Subtarget->hasFp256()) {
8362 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8363 if (Res.getNode())
8364 return Res;
8365 }
8366
Michael Liaoa7554632012-10-23 17:36:08 +00008367 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8368 VT.getVectorNumElements() != SVT.getVectorNumElements())
8369 return SDValue();
8370
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008371 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008372
8373 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008374 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008375 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8376
8377 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8378 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8379 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008380 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8381 DAG.getUNDEF(MVT::v8i16),
8382 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008383
8384 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8385}
8386
Craig Topperd713c0f2013-01-20 21:34:37 +00008387SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008388 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008389 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008390 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008391 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008392
Nadav Rotem3c22a442012-12-27 07:45:10 +00008393 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8394 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8395 if (Subtarget->hasInt256()) {
8396 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8397 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8398 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8399 ShufMask);
8400 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8401 DAG.getIntPtrConstant(0));
8402 }
8403
8404 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8405 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8406 DAG.getIntPtrConstant(0));
8407 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8408 DAG.getIntPtrConstant(2));
8409
8410 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8411 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8412
8413 // The PSHUFD mask:
8414 static const int ShufMask1[] = {0, 2, 0, 0};
8415 SDValue Undef = DAG.getUNDEF(VT);
8416 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8417 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8418
8419 // The MOVLHPS mask:
8420 static const int ShufMask2[] = {0, 1, 4, 5};
8421 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8422 }
8423
8424 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8425 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8426 if (Subtarget->hasInt256()) {
8427 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8428
8429 SmallVector<SDValue,32> pshufbMask;
8430 for (unsigned i = 0; i < 2; ++i) {
8431 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8432 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8433 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8434 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8435 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8436 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8437 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8438 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8439 for (unsigned j = 0; j < 8; ++j)
8440 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8441 }
8442 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8443 &pshufbMask[0], 32);
8444 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8445 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8446
8447 static const int ShufMask[] = {0, 2, -1, -1};
8448 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8449 &ShufMask[0]);
8450 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8451 DAG.getIntPtrConstant(0));
8452 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8453 }
8454
8455 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8456 DAG.getIntPtrConstant(0));
8457
8458 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8459 DAG.getIntPtrConstant(4));
8460
8461 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8462 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8463
8464 // The PSHUFB mask:
8465 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8466 -1, -1, -1, -1, -1, -1, -1, -1};
8467
8468 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8469 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8470 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8471
8472 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8473 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8474
8475 // The MOVLHPS Mask:
8476 static const int ShufMask2[] = {0, 1, 4, 5};
8477 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8478 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8479 }
8480
8481 // Handle truncation of V256 to V128 using shuffles.
8482 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008483 return SDValue();
8484
Nadav Rotem3c22a442012-12-27 07:45:10 +00008485 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8486 "Invalid op");
8487 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008488
8489 unsigned NumElems = VT.getVectorNumElements();
8490 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8491 NumElems * 2);
8492
Michael Liaobedcbd42012-10-16 18:14:11 +00008493 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8494 // Prepare truncation shuffle mask
8495 for (unsigned i = 0; i != NumElems; ++i)
8496 MaskVec[i] = i * 2;
8497 SDValue V = DAG.getVectorShuffle(NVT, DL,
8498 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8499 DAG.getUNDEF(NVT), &MaskVec[0]);
8500 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8501 DAG.getIntPtrConstant(0));
8502}
8503
Dan Gohmand858e902010-04-17 15:26:15 +00008504SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8505 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008506 MVT VT = Op.getValueType().getSimpleVT();
8507 if (VT.isVector()) {
8508 if (VT == MVT::v8i16)
8509 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008510 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8511 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008512 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008513 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008514
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008515 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8516 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008517 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008518 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8519 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008520
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008521 if (StackSlot.getNode())
8522 // Load the result.
8523 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8524 FIST, StackSlot, MachinePointerInfo(),
8525 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008526
8527 // The node is the result.
8528 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008529}
8530
Dan Gohmand858e902010-04-17 15:26:15 +00008531SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8532 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008533 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8534 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008535 SDValue FIST = Vals.first, StackSlot = Vals.second;
8536 assert(FIST.getNode() && "Unexpected failure");
8537
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008538 if (StackSlot.getNode())
8539 // Load the result.
8540 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8541 FIST, StackSlot, MachinePointerInfo(),
8542 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008543
8544 // The node is the result.
8545 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008546}
8547
Craig Topperb84b4232013-01-21 06:13:28 +00008548static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008549 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008550 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008551 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008552 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008553
8554 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8555
8556 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8557 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8558 In, DAG.getUNDEF(SVT)));
8559}
8560
Craig Topper43620672012-09-08 07:31:51 +00008561SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008562 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008563 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008564 MVT VT = Op.getValueType().getSimpleVT();
8565 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008566 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8567 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008568 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008569 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008570 }
Craig Topper43620672012-09-08 07:31:51 +00008571 Constant *C;
8572 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008573 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8574 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008575 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008576 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8577 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008578 C = ConstantVector::getSplat(NumElts, C);
8579 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8580 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008581 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008582 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008583 false, false, false, Alignment);
8584 if (VT.isVector()) {
8585 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8586 return DAG.getNode(ISD::BITCAST, dl, VT,
8587 DAG.getNode(ISD::AND, dl, ANDVT,
8588 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8589 Op.getOperand(0)),
8590 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8591 }
Dale Johannesenace16102009-02-03 19:33:06 +00008592 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008593}
8594
Dan Gohmand858e902010-04-17 15:26:15 +00008595SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008596 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008597 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008598 MVT VT = Op.getValueType().getSimpleVT();
8599 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008600 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8601 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008602 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008603 NumElts = VT.getVectorNumElements();
8604 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008605 Constant *C;
8606 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008607 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8608 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008609 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008610 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8611 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008612 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008613 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8614 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008615 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008616 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008617 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008618 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008619 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008620 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008621 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008622 DAG.getNode(ISD::BITCAST, dl, XORVT,
8623 Op.getOperand(0)),
8624 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008625 }
Craig Topper69947b92012-04-23 06:57:04 +00008626
8627 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008628}
8629
Dan Gohmand858e902010-04-17 15:26:15 +00008630SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008631 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008632 SDValue Op0 = Op.getOperand(0);
8633 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008634 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008635 MVT VT = Op.getValueType().getSimpleVT();
8636 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008637
8638 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008639 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008640 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008641 SrcVT = VT;
8642 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008643 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008644 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008645 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008646 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008647 }
8648
8649 // At this point the operands and the result should have the same
8650 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008651
Evan Cheng68c47cb2007-01-05 07:55:56 +00008652 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008653 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008654 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008655 const fltSemantics &Sem = APFloat::IEEEdouble;
8656 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8657 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008658 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008659 const fltSemantics &Sem = APFloat::IEEEsingle;
8660 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8661 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8662 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8663 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008664 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008665 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008666 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008667 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008668 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008669 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008670 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008671
8672 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008673 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 // Op0 is MVT::f32, Op1 is MVT::f64.
8675 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8676 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8677 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008678 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008679 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008680 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008681 }
8682
Evan Cheng73d6cf12007-01-05 21:37:56 +00008683 // Clear first operand sign bit.
8684 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008686 const fltSemantics &Sem = APFloat::IEEEdouble;
8687 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8688 APInt(64, ~(1ULL << 63)))));
8689 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008690 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008691 const fltSemantics &Sem = APFloat::IEEEsingle;
8692 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8693 APInt(32, ~(1U << 31)))));
8694 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8695 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8696 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008697 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008698 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008699 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008700 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008701 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008702 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008703 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008704
8705 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008706 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008707}
8708
Craig Topper55b24052012-09-11 06:15:32 +00008709static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008710 SDValue N0 = Op.getOperand(0);
8711 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008712 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008713
8714 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8715 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8716 DAG.getConstant(1, VT));
8717 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8718}
8719
Michael Liaof966e4e2012-09-13 20:24:54 +00008720// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8721//
Craig Topperb99bafe2013-01-21 06:21:54 +00008722SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8723 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008724 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8725
8726 if (!Subtarget->hasSSE41())
8727 return SDValue();
8728
8729 if (!Op->hasOneUse())
8730 return SDValue();
8731
8732 SDNode *N = Op.getNode();
8733 DebugLoc DL = N->getDebugLoc();
8734
8735 SmallVector<SDValue, 8> Opnds;
8736 DenseMap<SDValue, unsigned> VecInMap;
8737 EVT VT = MVT::Other;
8738
8739 // Recognize a special case where a vector is casted into wide integer to
8740 // test all 0s.
8741 Opnds.push_back(N->getOperand(0));
8742 Opnds.push_back(N->getOperand(1));
8743
8744 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8745 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8746 // BFS traverse all OR'd operands.
8747 if (I->getOpcode() == ISD::OR) {
8748 Opnds.push_back(I->getOperand(0));
8749 Opnds.push_back(I->getOperand(1));
8750 // Re-evaluate the number of nodes to be traversed.
8751 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8752 continue;
8753 }
8754
8755 // Quit if a non-EXTRACT_VECTOR_ELT
8756 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8757 return SDValue();
8758
8759 // Quit if without a constant index.
8760 SDValue Idx = I->getOperand(1);
8761 if (!isa<ConstantSDNode>(Idx))
8762 return SDValue();
8763
8764 SDValue ExtractedFromVec = I->getOperand(0);
8765 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8766 if (M == VecInMap.end()) {
8767 VT = ExtractedFromVec.getValueType();
8768 // Quit if not 128/256-bit vector.
8769 if (!VT.is128BitVector() && !VT.is256BitVector())
8770 return SDValue();
8771 // Quit if not the same type.
8772 if (VecInMap.begin() != VecInMap.end() &&
8773 VT != VecInMap.begin()->first.getValueType())
8774 return SDValue();
8775 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8776 }
8777 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8778 }
8779
8780 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008781 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008782
8783 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8784 SmallVector<SDValue, 8> VecIns;
8785
8786 for (DenseMap<SDValue, unsigned>::const_iterator
8787 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8788 // Quit if not all elements are used.
8789 if (I->second != FullMask)
8790 return SDValue();
8791 VecIns.push_back(I->first);
8792 }
8793
8794 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8795
8796 // Cast all vectors into TestVT for PTEST.
8797 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8798 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8799
8800 // If more than one full vectors are evaluated, OR them first before PTEST.
8801 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8802 // Each iteration will OR 2 nodes and append the result until there is only
8803 // 1 node left, i.e. the final OR'd value of all vectors.
8804 SDValue LHS = VecIns[Slot];
8805 SDValue RHS = VecIns[Slot + 1];
8806 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8807 }
8808
8809 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8810 VecIns.back(), VecIns.back());
8811}
8812
Dan Gohman076aee32009-03-04 19:44:21 +00008813/// Emit nodes that will be selected as "test Op0,Op0", or something
8814/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008815SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008816 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008817 DebugLoc dl = Op.getDebugLoc();
8818
Dan Gohman31125812009-03-07 01:58:32 +00008819 // CF and OF aren't always set the way we want. Determine which
8820 // of these we need.
8821 bool NeedCF = false;
8822 bool NeedOF = false;
8823 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008824 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008825 case X86::COND_A: case X86::COND_AE:
8826 case X86::COND_B: case X86::COND_BE:
8827 NeedCF = true;
8828 break;
8829 case X86::COND_G: case X86::COND_GE:
8830 case X86::COND_L: case X86::COND_LE:
8831 case X86::COND_O: case X86::COND_NO:
8832 NeedOF = true;
8833 break;
Dan Gohman31125812009-03-07 01:58:32 +00008834 }
8835
Dan Gohman076aee32009-03-04 19:44:21 +00008836 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008837 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8838 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008839 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8840 // Emit a CMP with 0, which is the TEST pattern.
8841 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8842 DAG.getConstant(0, Op.getValueType()));
8843
8844 unsigned Opcode = 0;
8845 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008846
8847 // Truncate operations may prevent the merge of the SETCC instruction
8848 // and the arithmetic intruction before it. Attempt to truncate the operands
8849 // of the arithmetic instruction and use a reduced bit-width instruction.
8850 bool NeedTruncation = false;
8851 SDValue ArithOp = Op;
8852 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8853 SDValue Arith = Op->getOperand(0);
8854 // Both the trunc and the arithmetic op need to have one user each.
8855 if (Arith->hasOneUse())
8856 switch (Arith.getOpcode()) {
8857 default: break;
8858 case ISD::ADD:
8859 case ISD::SUB:
8860 case ISD::AND:
8861 case ISD::OR:
8862 case ISD::XOR: {
8863 NeedTruncation = true;
8864 ArithOp = Arith;
8865 }
8866 }
8867 }
8868
8869 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8870 // which may be the result of a CAST. We use the variable 'Op', which is the
8871 // non-casted variable when we check for possible users.
8872 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008873 case ISD::ADD:
8874 // Due to an isel shortcoming, be conservative if this add is likely to be
8875 // selected as part of a load-modify-store instruction. When the root node
8876 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8877 // uses of other nodes in the match, such as the ADD in this case. This
8878 // leads to the ADD being left around and reselected, with the result being
8879 // two adds in the output. Alas, even if none our users are stores, that
8880 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8881 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8882 // climbing the DAG back to the root, and it doesn't seem to be worth the
8883 // effort.
8884 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008885 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8886 if (UI->getOpcode() != ISD::CopyToReg &&
8887 UI->getOpcode() != ISD::SETCC &&
8888 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008889 goto default_case;
8890
8891 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008892 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008893 // An add of one will be selected as an INC.
8894 if (C->getAPIntValue() == 1) {
8895 Opcode = X86ISD::INC;
8896 NumOperands = 1;
8897 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008898 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008899
8900 // An add of negative one (subtract of one) will be selected as a DEC.
8901 if (C->getAPIntValue().isAllOnesValue()) {
8902 Opcode = X86ISD::DEC;
8903 NumOperands = 1;
8904 break;
8905 }
Dan Gohman076aee32009-03-04 19:44:21 +00008906 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008907
8908 // Otherwise use a regular EFLAGS-setting add.
8909 Opcode = X86ISD::ADD;
8910 NumOperands = 2;
8911 break;
8912 case ISD::AND: {
8913 // If the primary and result isn't used, don't bother using X86ISD::AND,
8914 // because a TEST instruction will be better.
8915 bool NonFlagUse = false;
8916 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8917 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8918 SDNode *User = *UI;
8919 unsigned UOpNo = UI.getOperandNo();
8920 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8921 // Look pass truncate.
8922 UOpNo = User->use_begin().getOperandNo();
8923 User = *User->use_begin();
8924 }
8925
8926 if (User->getOpcode() != ISD::BRCOND &&
8927 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008928 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008929 NonFlagUse = true;
8930 break;
8931 }
Dan Gohman076aee32009-03-04 19:44:21 +00008932 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008933
8934 if (!NonFlagUse)
8935 break;
8936 }
8937 // FALL THROUGH
8938 case ISD::SUB:
8939 case ISD::OR:
8940 case ISD::XOR:
8941 // Due to the ISEL shortcoming noted above, be conservative if this op is
8942 // likely to be selected as part of a load-modify-store instruction.
8943 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8944 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8945 if (UI->getOpcode() == ISD::STORE)
8946 goto default_case;
8947
8948 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008949 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008950 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008951 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008952 case ISD::XOR: Opcode = X86ISD::XOR; break;
8953 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008954 case ISD::OR: {
8955 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8956 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8957 if (EFLAGS.getNode())
8958 return EFLAGS;
8959 }
8960 Opcode = X86ISD::OR;
8961 break;
8962 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008963 }
8964
8965 NumOperands = 2;
8966 break;
8967 case X86ISD::ADD:
8968 case X86ISD::SUB:
8969 case X86ISD::INC:
8970 case X86ISD::DEC:
8971 case X86ISD::OR:
8972 case X86ISD::XOR:
8973 case X86ISD::AND:
8974 return SDValue(Op.getNode(), 1);
8975 default:
8976 default_case:
8977 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008978 }
8979
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008980 // If we found that truncation is beneficial, perform the truncation and
8981 // update 'Op'.
8982 if (NeedTruncation) {
8983 EVT VT = Op.getValueType();
8984 SDValue WideVal = Op->getOperand(0);
8985 EVT WideVT = WideVal.getValueType();
8986 unsigned ConvertedOp = 0;
8987 // Use a target machine opcode to prevent further DAGCombine
8988 // optimizations that may separate the arithmetic operations
8989 // from the setcc node.
8990 switch (WideVal.getOpcode()) {
8991 default: break;
8992 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8993 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8994 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8995 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8996 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8997 }
8998
8999 if (ConvertedOp) {
9000 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9001 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9002 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9003 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9004 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9005 }
9006 }
9007 }
9008
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009009 if (Opcode == 0)
9010 // Emit a CMP with 0, which is the TEST pattern.
9011 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9012 DAG.getConstant(0, Op.getValueType()));
9013
9014 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9015 SmallVector<SDValue, 4> Ops;
9016 for (unsigned i = 0; i != NumOperands; ++i)
9017 Ops.push_back(Op.getOperand(i));
9018
9019 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9020 DAG.ReplaceAllUsesWith(Op, New);
9021 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009022}
9023
9024/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9025/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009026SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009027 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9029 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009030 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009031
9032 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009033 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9034 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9035 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9036 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9037 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9038 Op0, Op1);
9039 return SDValue(Sub.getNode(), 1);
9040 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009041 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009042}
9043
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009044/// Convert a comparison if required by the subtarget.
9045SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9046 SelectionDAG &DAG) const {
9047 // If the subtarget does not support the FUCOMI instruction, floating-point
9048 // comparisons have to be converted.
9049 if (Subtarget->hasCMov() ||
9050 Cmp.getOpcode() != X86ISD::CMP ||
9051 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9052 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9053 return Cmp;
9054
9055 // The instruction selector will select an FUCOM instruction instead of
9056 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9057 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9058 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9059 DebugLoc dl = Cmp.getDebugLoc();
9060 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9061 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9062 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9063 DAG.getConstant(8, MVT::i8));
9064 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9065 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9066}
9067
Evan Cheng4e544802012-12-05 00:10:38 +00009068static bool isAllOnes(SDValue V) {
9069 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9070 return C && C->isAllOnesValue();
9071}
9072
Evan Chengd40d03e2010-01-06 19:38:29 +00009073/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9074/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009075SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9076 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009077 SDValue Op0 = And.getOperand(0);
9078 SDValue Op1 = And.getOperand(1);
9079 if (Op0.getOpcode() == ISD::TRUNCATE)
9080 Op0 = Op0.getOperand(0);
9081 if (Op1.getOpcode() == ISD::TRUNCATE)
9082 Op1 = Op1.getOperand(0);
9083
Evan Chengd40d03e2010-01-06 19:38:29 +00009084 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009085 if (Op1.getOpcode() == ISD::SHL)
9086 std::swap(Op0, Op1);
9087 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009088 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9089 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009090 // If we looked past a truncate, check that it's only truncating away
9091 // known zeros.
9092 unsigned BitWidth = Op0.getValueSizeInBits();
9093 unsigned AndBitWidth = And.getValueSizeInBits();
9094 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009095 APInt Zeros, Ones;
9096 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009097 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9098 return SDValue();
9099 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009100 LHS = Op1;
9101 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009102 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009103 } else if (Op1.getOpcode() == ISD::Constant) {
9104 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009105 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009106 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009107
9108 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009109 LHS = AndLHS.getOperand(0);
9110 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009111 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009112
9113 // Use BT if the immediate can't be encoded in a TEST instruction.
9114 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9115 LHS = AndLHS;
9116 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9117 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009118 }
Evan Cheng0488db92007-09-25 01:57:46 +00009119
Evan Chengd40d03e2010-01-06 19:38:29 +00009120 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009121 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9122 // the condition code later.
9123 bool Invert = false;
9124 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9125 Invert = true;
9126 LHS = LHS.getOperand(0);
9127 }
9128
Evan Chenge5b51ac2010-04-17 06:13:15 +00009129 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009130 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009131 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009132 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009133 // Also promote i16 to i32 for performance / code size reason.
9134 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009135 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009136 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009137
Evan Chengd40d03e2010-01-06 19:38:29 +00009138 // If the operand types disagree, extend the shift amount to match. Since
9139 // BT ignores high bits (like shifts) we can use anyextend.
9140 if (LHS.getValueType() != RHS.getValueType())
9141 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009142
Evan Chengd40d03e2010-01-06 19:38:29 +00009143 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009144 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9145 // Flip the condition if the LHS was a not instruction
9146 if (Invert)
9147 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009148 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9149 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009150 }
9151
Evan Cheng54de3ea2010-01-05 06:52:31 +00009152 return SDValue();
9153}
9154
Craig Topper89af15e2011-09-18 08:03:58 +00009155// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009156// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009157static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009158 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009159
Craig Topper7a9a28b2012-08-12 02:23:29 +00009160 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009161 "Unsupported value type for operation");
9162
Craig Topper66ddd152012-04-27 22:54:43 +00009163 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009164 DebugLoc dl = Op.getDebugLoc();
9165 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009166
9167 // Extract the LHS vectors
9168 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009169 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9170 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009171
9172 // Extract the RHS vectors
9173 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009174 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9175 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009176
9177 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009178 MVT EltVT = VT.getVectorElementType();
9179 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009180 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9181 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9182 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9183}
9184
Craig Topper26827f32013-01-20 09:02:22 +00009185static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9186 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009187 SDValue Cond;
9188 SDValue Op0 = Op.getOperand(0);
9189 SDValue Op1 = Op.getOperand(1);
9190 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009191 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009192 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009193 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009194 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009195
9196 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009197#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009198 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009199 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9200#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009201
Craig Topper523908d2012-08-13 02:34:03 +00009202 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009203 bool Swap = false;
9204
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009205 // SSE Condition code mapping:
9206 // 0 - EQ
9207 // 1 - LT
9208 // 2 - LE
9209 // 3 - UNORD
9210 // 4 - NEQ
9211 // 5 - NLT
9212 // 6 - NLE
9213 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009214 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009215 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009216 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009217 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009218 case ISD::SETOGT:
9219 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009220 case ISD::SETLT:
9221 case ISD::SETOLT: SSECC = 1; break;
9222 case ISD::SETOGE:
9223 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009224 case ISD::SETLE:
9225 case ISD::SETOLE: SSECC = 2; break;
9226 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009227 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009228 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009229 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009230 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009231 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009232 case ISD::SETUGT: SSECC = 6; break;
9233 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009234 case ISD::SETUEQ:
9235 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009236 }
9237 if (Swap)
9238 std::swap(Op0, Op1);
9239
Nate Begemanfb8ead02008-07-25 19:05:58 +00009240 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009241 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009242 unsigned CC0, CC1;
9243 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009244 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009245 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9246 } else {
9247 assert(SetCCOpcode == ISD::SETONE);
9248 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009249 }
Craig Topper523908d2012-08-13 02:34:03 +00009250
9251 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9252 DAG.getConstant(CC0, MVT::i8));
9253 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9254 DAG.getConstant(CC1, MVT::i8));
9255 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009256 }
9257 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009258 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9259 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009261
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009262 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009263 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009264 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009265
Nate Begeman30a0de92008-07-17 16:51:19 +00009266 // We are handling one of the integer comparisons here. Since SSE only has
9267 // GT and EQ comparisons for integer, swapping operands and multiple
9268 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009269 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009270 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009271
Nate Begeman30a0de92008-07-17 16:51:19 +00009272 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009273 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009274 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009275 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009276 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009277 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009278 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009279 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009280 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009281 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009282 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009283 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009284 }
9285 if (Swap)
9286 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009287
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009288 // Check that the operation in question is available (most are plain SSE2,
9289 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009290 if (VT == MVT::v2i64) {
9291 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9292 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009293 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9294 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009295 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009296 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9297
9298 // First cast everything to the right type,
9299 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9300 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9301
9302 // Do the compare.
9303 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9304
9305 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009306 const int Mask[] = { 1, 0, 3, 2 };
9307 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9308 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009309
9310 if (Invert)
9311 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9312
9313 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9314 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009315 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009316
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9318 // bits of the inputs before performing those operations.
9319 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009320 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009321 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9322 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009323 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009324 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9325 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009326 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9327 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009328 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009329
Dale Johannesenace16102009-02-03 19:33:06 +00009330 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009331
9332 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009333 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009334 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009335
Nate Begeman30a0de92008-07-17 16:51:19 +00009336 return Result;
9337}
Evan Cheng0488db92007-09-25 01:57:46 +00009338
Craig Topper26827f32013-01-20 09:02:22 +00009339SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9340
9341 MVT VT = Op.getValueType().getSimpleVT();
9342
9343 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9344
9345 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9346 SDValue Op0 = Op.getOperand(0);
9347 SDValue Op1 = Op.getOperand(1);
9348 DebugLoc dl = Op.getDebugLoc();
9349 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9350
9351 // Optimize to BT if possible.
9352 // Lower (X & (1 << N)) == 0 to BT(X, N).
9353 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9354 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9355 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9356 Op1.getOpcode() == ISD::Constant &&
9357 cast<ConstantSDNode>(Op1)->isNullValue() &&
9358 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9359 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9360 if (NewSetCC.getNode())
9361 return NewSetCC;
9362 }
9363
9364 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9365 // these.
9366 if (Op1.getOpcode() == ISD::Constant &&
9367 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9368 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9369 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9370
9371 // If the input is a setcc, then reuse the input setcc or use a new one with
9372 // the inverted condition.
9373 if (Op0.getOpcode() == X86ISD::SETCC) {
9374 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9375 bool Invert = (CC == ISD::SETNE) ^
9376 cast<ConstantSDNode>(Op1)->isNullValue();
9377 if (!Invert) return Op0;
9378
9379 CCode = X86::GetOppositeBranchCondition(CCode);
9380 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9381 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9382 }
9383 }
9384
9385 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9386 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9387 if (X86CC == X86::COND_INVALID)
9388 return SDValue();
9389
9390 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9391 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9392 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9393 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9394}
9395
Evan Cheng370e5342008-12-03 08:38:43 +00009396// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009397static bool isX86LogicalCmp(SDValue Op) {
9398 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009399 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9400 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009401 return true;
9402 if (Op.getResNo() == 1 &&
9403 (Opc == X86ISD::ADD ||
9404 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009405 Opc == X86ISD::ADC ||
9406 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009407 Opc == X86ISD::SMUL ||
9408 Opc == X86ISD::UMUL ||
9409 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009410 Opc == X86ISD::DEC ||
9411 Opc == X86ISD::OR ||
9412 Opc == X86ISD::XOR ||
9413 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009414 return true;
9415
Chris Lattner9637d5b2010-12-05 07:49:54 +00009416 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9417 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009418
Dan Gohman076aee32009-03-04 19:44:21 +00009419 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009420}
9421
Chris Lattnera2b56002010-12-05 01:23:24 +00009422static bool isZero(SDValue V) {
9423 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9424 return C && C->isNullValue();
9425}
9426
Evan Chengb64dd5f2012-08-07 22:21:00 +00009427static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9428 if (V.getOpcode() != ISD::TRUNCATE)
9429 return false;
9430
9431 SDValue VOp0 = V.getOperand(0);
9432 unsigned InBits = VOp0.getValueSizeInBits();
9433 unsigned Bits = V.getValueSizeInBits();
9434 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9435}
9436
Dan Gohmand858e902010-04-17 15:26:15 +00009437SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009438 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009439 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009440 SDValue Op1 = Op.getOperand(1);
9441 SDValue Op2 = Op.getOperand(2);
9442 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009443 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009444
Dan Gohman1a492952009-10-20 16:22:37 +00009445 if (Cond.getOpcode() == ISD::SETCC) {
9446 SDValue NewCond = LowerSETCC(Cond, DAG);
9447 if (NewCond.getNode())
9448 Cond = NewCond;
9449 }
Evan Cheng734503b2006-09-11 02:19:56 +00009450
Chris Lattnera2b56002010-12-05 01:23:24 +00009451 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009452 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009453 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009454 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009455 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009456 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9457 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009458 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009459
Chris Lattnera2b56002010-12-05 01:23:24 +00009460 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009461
9462 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009463 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9464 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009465
9466 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009467 // Apply further optimizations for special cases
9468 // (select (x != 0), -1, 0) -> neg & sbb
9469 // (select (x == 0), 0, -1) -> neg & sbb
9470 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009471 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009472 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9473 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009474 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9475 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009476 CmpOp0);
9477 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9478 DAG.getConstant(X86::COND_B, MVT::i8),
9479 SDValue(Neg.getNode(), 1));
9480 return Res;
9481 }
9482
Chris Lattnera2b56002010-12-05 01:23:24 +00009483 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9484 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009485 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009486
Chris Lattner96908b12010-12-05 02:00:51 +00009487 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009488 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9489 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009490
Chris Lattner96908b12010-12-05 02:00:51 +00009491 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9492 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009493
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009494 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009495 if (N2C == 0 || !N2C->isNullValue())
9496 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9497 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009498 }
9499 }
9500
Chris Lattnera2b56002010-12-05 01:23:24 +00009501 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009502 if (Cond.getOpcode() == ISD::AND &&
9503 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9504 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009505 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009506 Cond = Cond.getOperand(0);
9507 }
9508
Evan Cheng3f41d662007-10-08 22:16:29 +00009509 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9510 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009511 unsigned CondOpcode = Cond.getOpcode();
9512 if (CondOpcode == X86ISD::SETCC ||
9513 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009514 CC = Cond.getOperand(0);
9515
Dan Gohman475871a2008-07-27 21:46:04 +00009516 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009517 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009518 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009519
Evan Cheng3f41d662007-10-08 22:16:29 +00009520 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009521 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009522 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009523 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009524
Chris Lattnerd1980a52009-03-12 06:52:53 +00009525 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9526 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009527 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009528 addTest = false;
9529 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009530 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9531 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9532 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9533 Cond.getOperand(0).getValueType() != MVT::i8)) {
9534 SDValue LHS = Cond.getOperand(0);
9535 SDValue RHS = Cond.getOperand(1);
9536 unsigned X86Opcode;
9537 unsigned X86Cond;
9538 SDVTList VTs;
9539 switch (CondOpcode) {
9540 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9541 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9542 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9543 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9544 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9545 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9546 default: llvm_unreachable("unexpected overflowing operator");
9547 }
9548 if (CondOpcode == ISD::UMULO)
9549 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9550 MVT::i32);
9551 else
9552 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9553
9554 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9555
9556 if (CondOpcode == ISD::UMULO)
9557 Cond = X86Op.getValue(2);
9558 else
9559 Cond = X86Op.getValue(1);
9560
9561 CC = DAG.getConstant(X86Cond, MVT::i8);
9562 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009563 }
9564
9565 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009566 // Look pass the truncate if the high bits are known zero.
9567 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9568 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009569
9570 // We know the result of AND is compared against zero. Try to match
9571 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009572 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009573 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009574 if (NewSetCC.getNode()) {
9575 CC = NewSetCC.getOperand(0);
9576 Cond = NewSetCC.getOperand(1);
9577 addTest = false;
9578 }
9579 }
9580 }
9581
9582 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009584 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009585 }
9586
Benjamin Kramere915ff32010-12-22 23:09:28 +00009587 // a < b ? -1 : 0 -> RES = ~setcc_carry
9588 // a < b ? 0 : -1 -> RES = setcc_carry
9589 // a >= b ? -1 : 0 -> RES = setcc_carry
9590 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009591 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009592 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009593 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9594
9595 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9596 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9597 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9598 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9599 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9600 return DAG.getNOT(DL, Res, Res.getValueType());
9601 return Res;
9602 }
9603 }
9604
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009605 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9606 // widen the cmov and push the truncate through. This avoids introducing a new
9607 // branch during isel and doesn't add any extensions.
9608 if (Op.getValueType() == MVT::i8 &&
9609 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9610 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9611 if (T1.getValueType() == T2.getValueType() &&
9612 // Blacklist CopyFromReg to avoid partial register stalls.
9613 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9614 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009615 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009616 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9617 }
9618 }
9619
Evan Cheng0488db92007-09-25 01:57:46 +00009620 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9621 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009622 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009623 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009624 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009625}
9626
Nadav Rotem1a330af2012-12-27 22:47:16 +00009627SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9628 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009629 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009630 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009631 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009632 DebugLoc dl = Op->getDebugLoc();
9633
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009634 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9635 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9636 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009637
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009638 if (Subtarget->hasInt256())
9639 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009640
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009641 // Optimize vectors in AVX mode
9642 // Sign extend v8i16 to v8i32 and
9643 // v4i32 to v4i64
9644 //
9645 // Divide input vector into two parts
9646 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9647 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9648 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009649
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009650 unsigned NumElems = InVT.getVectorNumElements();
9651 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009652
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009653 SmallVector<int,8> ShufMask1(NumElems, -1);
9654 for (unsigned i = 0; i != NumElems/2; ++i)
9655 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009656
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009657 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009658
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009659 SmallVector<int,8> ShufMask2(NumElems, -1);
9660 for (unsigned i = 0; i != NumElems/2; ++i)
9661 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009662
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009663 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009664
Craig Toppera080daf2013-01-20 21:50:27 +00009665 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009666 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009667
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009668 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9669 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009670
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009671 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009672}
9673
Evan Cheng370e5342008-12-03 08:38:43 +00009674// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9675// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9676// from the AND / OR.
9677static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9678 Opc = Op.getOpcode();
9679 if (Opc != ISD::OR && Opc != ISD::AND)
9680 return false;
9681 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9682 Op.getOperand(0).hasOneUse() &&
9683 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9684 Op.getOperand(1).hasOneUse());
9685}
9686
Evan Cheng961d6d42009-02-02 08:19:07 +00009687// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9688// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009689static bool isXor1OfSetCC(SDValue Op) {
9690 if (Op.getOpcode() != ISD::XOR)
9691 return false;
9692 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9693 if (N1C && N1C->getAPIntValue() == 1) {
9694 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9695 Op.getOperand(0).hasOneUse();
9696 }
9697 return false;
9698}
9699
Dan Gohmand858e902010-04-17 15:26:15 +00009700SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009701 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009702 SDValue Chain = Op.getOperand(0);
9703 SDValue Cond = Op.getOperand(1);
9704 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009705 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009706 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009707 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009708
Dan Gohman1a492952009-10-20 16:22:37 +00009709 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009710 // Check for setcc([su]{add,sub,mul}o == 0).
9711 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9712 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9713 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9714 Cond.getOperand(0).getResNo() == 1 &&
9715 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9716 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9717 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9718 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9719 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9720 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9721 Inverted = true;
9722 Cond = Cond.getOperand(0);
9723 } else {
9724 SDValue NewCond = LowerSETCC(Cond, DAG);
9725 if (NewCond.getNode())
9726 Cond = NewCond;
9727 }
Dan Gohman1a492952009-10-20 16:22:37 +00009728 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009729#if 0
9730 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009731 else if (Cond.getOpcode() == X86ISD::ADD ||
9732 Cond.getOpcode() == X86ISD::SUB ||
9733 Cond.getOpcode() == X86ISD::SMUL ||
9734 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009735 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009736#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009737
Evan Chengad9c0a32009-12-15 00:53:42 +00009738 // Look pass (and (setcc_carry (cmp ...)), 1).
9739 if (Cond.getOpcode() == ISD::AND &&
9740 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9741 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009742 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009743 Cond = Cond.getOperand(0);
9744 }
9745
Evan Cheng3f41d662007-10-08 22:16:29 +00009746 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9747 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009748 unsigned CondOpcode = Cond.getOpcode();
9749 if (CondOpcode == X86ISD::SETCC ||
9750 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009751 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009752
Dan Gohman475871a2008-07-27 21:46:04 +00009753 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009754 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009755 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009756 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009757 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009758 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009759 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009760 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009761 default: break;
9762 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009763 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009764 // These can only come from an arithmetic instruction with overflow,
9765 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009766 Cond = Cond.getNode()->getOperand(1);
9767 addTest = false;
9768 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009769 }
Evan Cheng0488db92007-09-25 01:57:46 +00009770 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009771 }
9772 CondOpcode = Cond.getOpcode();
9773 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9774 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9775 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9776 Cond.getOperand(0).getValueType() != MVT::i8)) {
9777 SDValue LHS = Cond.getOperand(0);
9778 SDValue RHS = Cond.getOperand(1);
9779 unsigned X86Opcode;
9780 unsigned X86Cond;
9781 SDVTList VTs;
9782 switch (CondOpcode) {
9783 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9784 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9785 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9786 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9787 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9788 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9789 default: llvm_unreachable("unexpected overflowing operator");
9790 }
9791 if (Inverted)
9792 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9793 if (CondOpcode == ISD::UMULO)
9794 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9795 MVT::i32);
9796 else
9797 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9798
9799 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9800
9801 if (CondOpcode == ISD::UMULO)
9802 Cond = X86Op.getValue(2);
9803 else
9804 Cond = X86Op.getValue(1);
9805
9806 CC = DAG.getConstant(X86Cond, MVT::i8);
9807 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009808 } else {
9809 unsigned CondOpc;
9810 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9811 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009812 if (CondOpc == ISD::OR) {
9813 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9814 // two branches instead of an explicit OR instruction with a
9815 // separate test.
9816 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009817 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009818 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009819 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009820 Chain, Dest, CC, Cmp);
9821 CC = Cond.getOperand(1).getOperand(0);
9822 Cond = Cmp;
9823 addTest = false;
9824 }
9825 } else { // ISD::AND
9826 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9827 // two branches instead of an explicit AND instruction with a
9828 // separate test. However, we only do this if this block doesn't
9829 // have a fall-through edge, because this requires an explicit
9830 // jmp when the condition is false.
9831 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009832 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009833 Op.getNode()->hasOneUse()) {
9834 X86::CondCode CCode =
9835 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9836 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009837 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009838 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009839 // Look for an unconditional branch following this conditional branch.
9840 // We need this because we need to reverse the successors in order
9841 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009842 if (User->getOpcode() == ISD::BR) {
9843 SDValue FalseBB = User->getOperand(1);
9844 SDNode *NewBR =
9845 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009846 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009847 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009848 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009849
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009851 Chain, Dest, CC, Cmp);
9852 X86::CondCode CCode =
9853 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9854 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009855 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009856 Cond = Cmp;
9857 addTest = false;
9858 }
9859 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009860 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009861 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9862 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9863 // It should be transformed during dag combiner except when the condition
9864 // is set by a arithmetics with overflow node.
9865 X86::CondCode CCode =
9866 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9867 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009868 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009869 Cond = Cond.getOperand(0).getOperand(1);
9870 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009871 } else if (Cond.getOpcode() == ISD::SETCC &&
9872 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9873 // For FCMP_OEQ, we can emit
9874 // two branches instead of an explicit AND instruction with a
9875 // separate test. However, we only do this if this block doesn't
9876 // have a fall-through edge, because this requires an explicit
9877 // jmp when the condition is false.
9878 if (Op.getNode()->hasOneUse()) {
9879 SDNode *User = *Op.getNode()->use_begin();
9880 // Look for an unconditional branch following this conditional branch.
9881 // We need this because we need to reverse the successors in order
9882 // to implement FCMP_OEQ.
9883 if (User->getOpcode() == ISD::BR) {
9884 SDValue FalseBB = User->getOperand(1);
9885 SDNode *NewBR =
9886 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9887 assert(NewBR == User);
9888 (void)NewBR;
9889 Dest = FalseBB;
9890
9891 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9892 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009893 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009894 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9895 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9896 Chain, Dest, CC, Cmp);
9897 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9898 Cond = Cmp;
9899 addTest = false;
9900 }
9901 }
9902 } else if (Cond.getOpcode() == ISD::SETCC &&
9903 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9904 // For FCMP_UNE, we can emit
9905 // two branches instead of an explicit AND instruction with a
9906 // separate test. However, we only do this if this block doesn't
9907 // have a fall-through edge, because this requires an explicit
9908 // jmp when the condition is false.
9909 if (Op.getNode()->hasOneUse()) {
9910 SDNode *User = *Op.getNode()->use_begin();
9911 // Look for an unconditional branch following this conditional branch.
9912 // We need this because we need to reverse the successors in order
9913 // to implement FCMP_UNE.
9914 if (User->getOpcode() == ISD::BR) {
9915 SDValue FalseBB = User->getOperand(1);
9916 SDNode *NewBR =
9917 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9918 assert(NewBR == User);
9919 (void)NewBR;
9920
9921 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9922 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009923 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009924 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9925 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9926 Chain, Dest, CC, Cmp);
9927 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9928 Cond = Cmp;
9929 addTest = false;
9930 Dest = FalseBB;
9931 }
9932 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009933 }
Evan Cheng0488db92007-09-25 01:57:46 +00009934 }
9935
9936 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009937 // Look pass the truncate if the high bits are known zero.
9938 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9939 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009940
9941 // We know the result of AND is compared against zero. Try to match
9942 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009943 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009944 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9945 if (NewSetCC.getNode()) {
9946 CC = NewSetCC.getOperand(0);
9947 Cond = NewSetCC.getOperand(1);
9948 addTest = false;
9949 }
9950 }
9951 }
9952
9953 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009954 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009955 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009956 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009957 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009958 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009959 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009960}
9961
Anton Korobeynikove060b532007-04-17 19:34:00 +00009962// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9963// Calls to _alloca is needed to probe the stack when allocating more than 4k
9964// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9965// that the guard pages used by the OS virtual memory manager are allocated in
9966// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009967SDValue
9968X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009969 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009970 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009971 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009972 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009973 "are being used");
9974 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009975 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009976
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009977 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009978 SDValue Chain = Op.getOperand(0);
9979 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009980 // FIXME: Ensure alignment here
9981
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009982 bool Is64Bit = Subtarget->is64Bit();
9983 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009984
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009985 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009986 MachineFunction &MF = DAG.getMachineFunction();
9987 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009988
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009989 if (Is64Bit) {
9990 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009991 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009992 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009993
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009994 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009995 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009996 if (I->hasNestAttr())
9997 report_fatal_error("Cannot use segmented stacks with functions that "
9998 "have nested arguments.");
9999 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010000
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010001 const TargetRegisterClass *AddrRegClass =
10002 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10003 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10004 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10005 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10006 DAG.getRegister(Vreg, SPTy));
10007 SDValue Ops1[2] = { Value, Chain };
10008 return DAG.getMergeValues(Ops1, 2, dl);
10009 } else {
10010 SDValue Flag;
10011 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010012
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010013 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10014 Flag = Chain.getValue(1);
10015 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010016
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010017 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10018 Flag = Chain.getValue(1);
10019
Michael Liaoc5c970e2012-10-31 04:14:09 +000010020 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10021 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010022
10023 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10024 return DAG.getMergeValues(Ops1, 2, dl);
10025 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010026}
10027
Dan Gohmand858e902010-04-17 15:26:15 +000010028SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010029 MachineFunction &MF = DAG.getMachineFunction();
10030 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10031
Dan Gohman69de1932008-02-06 22:27:42 +000010032 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010033 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010034
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010035 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010036 // vastart just stores the address of the VarArgsFrameIndex slot into the
10037 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010038 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10039 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010040 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10041 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010042 }
10043
10044 // __va_list_tag:
10045 // gp_offset (0 - 6 * 8)
10046 // fp_offset (48 - 48 + 8 * 16)
10047 // overflow_arg_area (point to parameters coming in memory).
10048 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010049 SmallVector<SDValue, 8> MemOps;
10050 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010051 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010052 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010053 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10054 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010055 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010056 MemOps.push_back(Store);
10057
10058 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010059 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010060 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010061 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010062 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10063 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010064 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010065 MemOps.push_back(Store);
10066
10067 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010068 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010069 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010070 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10071 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010072 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10073 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010074 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010075 MemOps.push_back(Store);
10076
10077 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010078 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010079 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010080 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10081 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010082 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10083 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010084 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010085 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010086 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010087}
10088
Dan Gohmand858e902010-04-17 15:26:15 +000010089SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010090 assert(Subtarget->is64Bit() &&
10091 "LowerVAARG only handles 64-bit va_arg!");
10092 assert((Subtarget->isTargetLinux() ||
10093 Subtarget->isTargetDarwin()) &&
10094 "Unhandled target in LowerVAARG");
10095 assert(Op.getNode()->getNumOperands() == 4);
10096 SDValue Chain = Op.getOperand(0);
10097 SDValue SrcPtr = Op.getOperand(1);
10098 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10099 unsigned Align = Op.getConstantOperandVal(3);
10100 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010101
Dan Gohman320afb82010-10-12 18:00:49 +000010102 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010103 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010104 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010105 uint8_t ArgMode;
10106
10107 // Decide which area this value should be read from.
10108 // TODO: Implement the AMD64 ABI in its entirety. This simple
10109 // selection mechanism works only for the basic types.
10110 if (ArgVT == MVT::f80) {
10111 llvm_unreachable("va_arg for f80 not yet implemented");
10112 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10113 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10114 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10115 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10116 } else {
10117 llvm_unreachable("Unhandled argument type in LowerVAARG");
10118 }
10119
10120 if (ArgMode == 2) {
10121 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010122 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010123 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010124 .getFunction()->getAttributes()
10125 .hasAttribute(AttributeSet::FunctionIndex,
10126 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010127 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010128 }
10129
10130 // Insert VAARG_64 node into the DAG
10131 // VAARG_64 returns two values: Variable Argument Address, Chain
10132 SmallVector<SDValue, 11> InstOps;
10133 InstOps.push_back(Chain);
10134 InstOps.push_back(SrcPtr);
10135 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10136 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10137 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10138 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10139 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10140 VTs, &InstOps[0], InstOps.size(),
10141 MVT::i64,
10142 MachinePointerInfo(SV),
10143 /*Align=*/0,
10144 /*Volatile=*/false,
10145 /*ReadMem=*/true,
10146 /*WriteMem=*/true);
10147 Chain = VAARG.getValue(1);
10148
10149 // Load the next argument and return it
10150 return DAG.getLoad(ArgVT, dl,
10151 Chain,
10152 VAARG,
10153 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010154 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010155}
10156
Craig Topper55b24052012-09-11 06:15:32 +000010157static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10158 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010159 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010160 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010161 SDValue Chain = Op.getOperand(0);
10162 SDValue DstPtr = Op.getOperand(1);
10163 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010164 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10165 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010166 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010167
Chris Lattnere72f2022010-09-21 05:40:29 +000010168 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010169 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010170 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010171 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010172}
10173
Craig Topper80e46362012-01-23 06:16:53 +000010174// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10175// may or may not be a constant. Takes immediate version of shift as input.
10176static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10177 SDValue SrcOp, SDValue ShAmt,
10178 SelectionDAG &DAG) {
10179 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10180
10181 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010182 // Constant may be a TargetConstant. Use a regular constant.
10183 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010184 switch (Opc) {
10185 default: llvm_unreachable("Unknown target vector shift node");
10186 case X86ISD::VSHLI:
10187 case X86ISD::VSRLI:
10188 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010189 return DAG.getNode(Opc, dl, VT, SrcOp,
10190 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010191 }
10192 }
10193
10194 // Change opcode to non-immediate version
10195 switch (Opc) {
10196 default: llvm_unreachable("Unknown target vector shift node");
10197 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10198 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10199 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10200 }
10201
10202 // Need to build a vector containing shift amount
10203 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10204 SDValue ShOps[4];
10205 ShOps[0] = ShAmt;
10206 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010207 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010208 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010209
10210 // The return type has to be a 128-bit type with the same element
10211 // type as the input type.
10212 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10213 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10214
10215 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010216 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10217}
10218
Craig Topper55b24052012-09-11 06:15:32 +000010219static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010220 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010221 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010222 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010223 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010224 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010225 case Intrinsic::x86_sse_comieq_ss:
10226 case Intrinsic::x86_sse_comilt_ss:
10227 case Intrinsic::x86_sse_comile_ss:
10228 case Intrinsic::x86_sse_comigt_ss:
10229 case Intrinsic::x86_sse_comige_ss:
10230 case Intrinsic::x86_sse_comineq_ss:
10231 case Intrinsic::x86_sse_ucomieq_ss:
10232 case Intrinsic::x86_sse_ucomilt_ss:
10233 case Intrinsic::x86_sse_ucomile_ss:
10234 case Intrinsic::x86_sse_ucomigt_ss:
10235 case Intrinsic::x86_sse_ucomige_ss:
10236 case Intrinsic::x86_sse_ucomineq_ss:
10237 case Intrinsic::x86_sse2_comieq_sd:
10238 case Intrinsic::x86_sse2_comilt_sd:
10239 case Intrinsic::x86_sse2_comile_sd:
10240 case Intrinsic::x86_sse2_comigt_sd:
10241 case Intrinsic::x86_sse2_comige_sd:
10242 case Intrinsic::x86_sse2_comineq_sd:
10243 case Intrinsic::x86_sse2_ucomieq_sd:
10244 case Intrinsic::x86_sse2_ucomilt_sd:
10245 case Intrinsic::x86_sse2_ucomile_sd:
10246 case Intrinsic::x86_sse2_ucomigt_sd:
10247 case Intrinsic::x86_sse2_ucomige_sd:
10248 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010249 unsigned Opc;
10250 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010251 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010252 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010253 case Intrinsic::x86_sse_comieq_ss:
10254 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010255 Opc = X86ISD::COMI;
10256 CC = ISD::SETEQ;
10257 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010258 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010259 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010260 Opc = X86ISD::COMI;
10261 CC = ISD::SETLT;
10262 break;
10263 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010264 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010265 Opc = X86ISD::COMI;
10266 CC = ISD::SETLE;
10267 break;
10268 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010269 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010270 Opc = X86ISD::COMI;
10271 CC = ISD::SETGT;
10272 break;
10273 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010274 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010275 Opc = X86ISD::COMI;
10276 CC = ISD::SETGE;
10277 break;
10278 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010279 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010280 Opc = X86ISD::COMI;
10281 CC = ISD::SETNE;
10282 break;
10283 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010284 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010285 Opc = X86ISD::UCOMI;
10286 CC = ISD::SETEQ;
10287 break;
10288 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010289 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010290 Opc = X86ISD::UCOMI;
10291 CC = ISD::SETLT;
10292 break;
10293 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010294 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010295 Opc = X86ISD::UCOMI;
10296 CC = ISD::SETLE;
10297 break;
10298 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010299 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010300 Opc = X86ISD::UCOMI;
10301 CC = ISD::SETGT;
10302 break;
10303 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010304 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010305 Opc = X86ISD::UCOMI;
10306 CC = ISD::SETGE;
10307 break;
10308 case Intrinsic::x86_sse_ucomineq_ss:
10309 case Intrinsic::x86_sse2_ucomineq_sd:
10310 Opc = X86ISD::UCOMI;
10311 CC = ISD::SETNE;
10312 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010313 }
Evan Cheng734503b2006-09-11 02:19:56 +000010314
Dan Gohman475871a2008-07-27 21:46:04 +000010315 SDValue LHS = Op.getOperand(1);
10316 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010317 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010318 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010319 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10320 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10321 DAG.getConstant(X86CC, MVT::i8), Cond);
10322 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010323 }
Craig Topper6d688152012-08-14 07:43:25 +000010324
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010325 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010326 case Intrinsic::x86_sse2_pmulu_dq:
10327 case Intrinsic::x86_avx2_pmulu_dq:
10328 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10329 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010330
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010331 // SSE2/AVX2 sub with unsigned saturation intrinsics
10332 case Intrinsic::x86_sse2_psubus_b:
10333 case Intrinsic::x86_sse2_psubus_w:
10334 case Intrinsic::x86_avx2_psubus_b:
10335 case Intrinsic::x86_avx2_psubus_w:
10336 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10337 Op.getOperand(1), Op.getOperand(2));
10338
Craig Topper6d688152012-08-14 07:43:25 +000010339 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010340 case Intrinsic::x86_sse3_hadd_ps:
10341 case Intrinsic::x86_sse3_hadd_pd:
10342 case Intrinsic::x86_avx_hadd_ps_256:
10343 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010344 case Intrinsic::x86_sse3_hsub_ps:
10345 case Intrinsic::x86_sse3_hsub_pd:
10346 case Intrinsic::x86_avx_hsub_ps_256:
10347 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010348 case Intrinsic::x86_ssse3_phadd_w_128:
10349 case Intrinsic::x86_ssse3_phadd_d_128:
10350 case Intrinsic::x86_avx2_phadd_w:
10351 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010352 case Intrinsic::x86_ssse3_phsub_w_128:
10353 case Intrinsic::x86_ssse3_phsub_d_128:
10354 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010355 case Intrinsic::x86_avx2_phsub_d: {
10356 unsigned Opcode;
10357 switch (IntNo) {
10358 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10359 case Intrinsic::x86_sse3_hadd_ps:
10360 case Intrinsic::x86_sse3_hadd_pd:
10361 case Intrinsic::x86_avx_hadd_ps_256:
10362 case Intrinsic::x86_avx_hadd_pd_256:
10363 Opcode = X86ISD::FHADD;
10364 break;
10365 case Intrinsic::x86_sse3_hsub_ps:
10366 case Intrinsic::x86_sse3_hsub_pd:
10367 case Intrinsic::x86_avx_hsub_ps_256:
10368 case Intrinsic::x86_avx_hsub_pd_256:
10369 Opcode = X86ISD::FHSUB;
10370 break;
10371 case Intrinsic::x86_ssse3_phadd_w_128:
10372 case Intrinsic::x86_ssse3_phadd_d_128:
10373 case Intrinsic::x86_avx2_phadd_w:
10374 case Intrinsic::x86_avx2_phadd_d:
10375 Opcode = X86ISD::HADD;
10376 break;
10377 case Intrinsic::x86_ssse3_phsub_w_128:
10378 case Intrinsic::x86_ssse3_phsub_d_128:
10379 case Intrinsic::x86_avx2_phsub_w:
10380 case Intrinsic::x86_avx2_phsub_d:
10381 Opcode = X86ISD::HSUB;
10382 break;
10383 }
10384 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010385 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010386 }
10387
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010388 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10389 case Intrinsic::x86_sse2_pmaxu_b:
10390 case Intrinsic::x86_sse41_pmaxuw:
10391 case Intrinsic::x86_sse41_pmaxud:
10392 case Intrinsic::x86_avx2_pmaxu_b:
10393 case Intrinsic::x86_avx2_pmaxu_w:
10394 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010395 case Intrinsic::x86_sse2_pminu_b:
10396 case Intrinsic::x86_sse41_pminuw:
10397 case Intrinsic::x86_sse41_pminud:
10398 case Intrinsic::x86_avx2_pminu_b:
10399 case Intrinsic::x86_avx2_pminu_w:
10400 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010401 case Intrinsic::x86_sse41_pmaxsb:
10402 case Intrinsic::x86_sse2_pmaxs_w:
10403 case Intrinsic::x86_sse41_pmaxsd:
10404 case Intrinsic::x86_avx2_pmaxs_b:
10405 case Intrinsic::x86_avx2_pmaxs_w:
10406 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010407 case Intrinsic::x86_sse41_pminsb:
10408 case Intrinsic::x86_sse2_pmins_w:
10409 case Intrinsic::x86_sse41_pminsd:
10410 case Intrinsic::x86_avx2_pmins_b:
10411 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010412 case Intrinsic::x86_avx2_pmins_d: {
10413 unsigned Opcode;
10414 switch (IntNo) {
10415 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10416 case Intrinsic::x86_sse2_pmaxu_b:
10417 case Intrinsic::x86_sse41_pmaxuw:
10418 case Intrinsic::x86_sse41_pmaxud:
10419 case Intrinsic::x86_avx2_pmaxu_b:
10420 case Intrinsic::x86_avx2_pmaxu_w:
10421 case Intrinsic::x86_avx2_pmaxu_d:
10422 Opcode = X86ISD::UMAX;
10423 break;
10424 case Intrinsic::x86_sse2_pminu_b:
10425 case Intrinsic::x86_sse41_pminuw:
10426 case Intrinsic::x86_sse41_pminud:
10427 case Intrinsic::x86_avx2_pminu_b:
10428 case Intrinsic::x86_avx2_pminu_w:
10429 case Intrinsic::x86_avx2_pminu_d:
10430 Opcode = X86ISD::UMIN;
10431 break;
10432 case Intrinsic::x86_sse41_pmaxsb:
10433 case Intrinsic::x86_sse2_pmaxs_w:
10434 case Intrinsic::x86_sse41_pmaxsd:
10435 case Intrinsic::x86_avx2_pmaxs_b:
10436 case Intrinsic::x86_avx2_pmaxs_w:
10437 case Intrinsic::x86_avx2_pmaxs_d:
10438 Opcode = X86ISD::SMAX;
10439 break;
10440 case Intrinsic::x86_sse41_pminsb:
10441 case Intrinsic::x86_sse2_pmins_w:
10442 case Intrinsic::x86_sse41_pminsd:
10443 case Intrinsic::x86_avx2_pmins_b:
10444 case Intrinsic::x86_avx2_pmins_w:
10445 case Intrinsic::x86_avx2_pmins_d:
10446 Opcode = X86ISD::SMIN;
10447 break;
10448 }
10449 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010450 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010451 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010452
Craig Topper6d183e42012-12-29 16:44:25 +000010453 // SSE/SSE2/AVX floating point max/min intrinsics.
10454 case Intrinsic::x86_sse_max_ps:
10455 case Intrinsic::x86_sse2_max_pd:
10456 case Intrinsic::x86_avx_max_ps_256:
10457 case Intrinsic::x86_avx_max_pd_256:
10458 case Intrinsic::x86_sse_min_ps:
10459 case Intrinsic::x86_sse2_min_pd:
10460 case Intrinsic::x86_avx_min_ps_256:
10461 case Intrinsic::x86_avx_min_pd_256: {
10462 unsigned Opcode;
10463 switch (IntNo) {
10464 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10465 case Intrinsic::x86_sse_max_ps:
10466 case Intrinsic::x86_sse2_max_pd:
10467 case Intrinsic::x86_avx_max_ps_256:
10468 case Intrinsic::x86_avx_max_pd_256:
10469 Opcode = X86ISD::FMAX;
10470 break;
10471 case Intrinsic::x86_sse_min_ps:
10472 case Intrinsic::x86_sse2_min_pd:
10473 case Intrinsic::x86_avx_min_ps_256:
10474 case Intrinsic::x86_avx_min_pd_256:
10475 Opcode = X86ISD::FMIN;
10476 break;
10477 }
10478 return DAG.getNode(Opcode, dl, Op.getValueType(),
10479 Op.getOperand(1), Op.getOperand(2));
10480 }
10481
Craig Topper6d688152012-08-14 07:43:25 +000010482 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010483 case Intrinsic::x86_avx2_psllv_d:
10484 case Intrinsic::x86_avx2_psllv_q:
10485 case Intrinsic::x86_avx2_psllv_d_256:
10486 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010487 case Intrinsic::x86_avx2_psrlv_d:
10488 case Intrinsic::x86_avx2_psrlv_q:
10489 case Intrinsic::x86_avx2_psrlv_d_256:
10490 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010491 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010492 case Intrinsic::x86_avx2_psrav_d_256: {
10493 unsigned Opcode;
10494 switch (IntNo) {
10495 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10496 case Intrinsic::x86_avx2_psllv_d:
10497 case Intrinsic::x86_avx2_psllv_q:
10498 case Intrinsic::x86_avx2_psllv_d_256:
10499 case Intrinsic::x86_avx2_psllv_q_256:
10500 Opcode = ISD::SHL;
10501 break;
10502 case Intrinsic::x86_avx2_psrlv_d:
10503 case Intrinsic::x86_avx2_psrlv_q:
10504 case Intrinsic::x86_avx2_psrlv_d_256:
10505 case Intrinsic::x86_avx2_psrlv_q_256:
10506 Opcode = ISD::SRL;
10507 break;
10508 case Intrinsic::x86_avx2_psrav_d:
10509 case Intrinsic::x86_avx2_psrav_d_256:
10510 Opcode = ISD::SRA;
10511 break;
10512 }
10513 return DAG.getNode(Opcode, dl, Op.getValueType(),
10514 Op.getOperand(1), Op.getOperand(2));
10515 }
10516
Craig Topper969ba282012-01-25 06:43:11 +000010517 case Intrinsic::x86_ssse3_pshuf_b_128:
10518 case Intrinsic::x86_avx2_pshuf_b:
10519 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10520 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010521
Craig Topper969ba282012-01-25 06:43:11 +000010522 case Intrinsic::x86_ssse3_psign_b_128:
10523 case Intrinsic::x86_ssse3_psign_w_128:
10524 case Intrinsic::x86_ssse3_psign_d_128:
10525 case Intrinsic::x86_avx2_psign_b:
10526 case Intrinsic::x86_avx2_psign_w:
10527 case Intrinsic::x86_avx2_psign_d:
10528 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10529 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010530
Craig Toppere566cd02012-01-26 07:18:03 +000010531 case Intrinsic::x86_sse41_insertps:
10532 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10533 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010534
Craig Toppere566cd02012-01-26 07:18:03 +000010535 case Intrinsic::x86_avx_vperm2f128_ps_256:
10536 case Intrinsic::x86_avx_vperm2f128_pd_256:
10537 case Intrinsic::x86_avx_vperm2f128_si_256:
10538 case Intrinsic::x86_avx2_vperm2i128:
10539 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10540 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010541
Craig Topperffa6c402012-04-16 07:13:00 +000010542 case Intrinsic::x86_avx2_permd:
10543 case Intrinsic::x86_avx2_permps:
10544 // Operands intentionally swapped. Mask is last operand to intrinsic,
10545 // but second operand for node/intruction.
10546 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10547 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010548
Craig Topper22d8f0d2012-12-29 18:18:20 +000010549 case Intrinsic::x86_sse_sqrt_ps:
10550 case Intrinsic::x86_sse2_sqrt_pd:
10551 case Intrinsic::x86_avx_sqrt_ps_256:
10552 case Intrinsic::x86_avx_sqrt_pd_256:
10553 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10554
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010555 // ptest and testp intrinsics. The intrinsic these come from are designed to
10556 // return an integer value, not just an instruction so lower it to the ptest
10557 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010558 case Intrinsic::x86_sse41_ptestz:
10559 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010560 case Intrinsic::x86_sse41_ptestnzc:
10561 case Intrinsic::x86_avx_ptestz_256:
10562 case Intrinsic::x86_avx_ptestc_256:
10563 case Intrinsic::x86_avx_ptestnzc_256:
10564 case Intrinsic::x86_avx_vtestz_ps:
10565 case Intrinsic::x86_avx_vtestc_ps:
10566 case Intrinsic::x86_avx_vtestnzc_ps:
10567 case Intrinsic::x86_avx_vtestz_pd:
10568 case Intrinsic::x86_avx_vtestc_pd:
10569 case Intrinsic::x86_avx_vtestnzc_pd:
10570 case Intrinsic::x86_avx_vtestz_ps_256:
10571 case Intrinsic::x86_avx_vtestc_ps_256:
10572 case Intrinsic::x86_avx_vtestnzc_ps_256:
10573 case Intrinsic::x86_avx_vtestz_pd_256:
10574 case Intrinsic::x86_avx_vtestc_pd_256:
10575 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10576 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010577 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010578 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010579 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010580 case Intrinsic::x86_avx_vtestz_ps:
10581 case Intrinsic::x86_avx_vtestz_pd:
10582 case Intrinsic::x86_avx_vtestz_ps_256:
10583 case Intrinsic::x86_avx_vtestz_pd_256:
10584 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010585 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010586 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010587 // ZF = 1
10588 X86CC = X86::COND_E;
10589 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010590 case Intrinsic::x86_avx_vtestc_ps:
10591 case Intrinsic::x86_avx_vtestc_pd:
10592 case Intrinsic::x86_avx_vtestc_ps_256:
10593 case Intrinsic::x86_avx_vtestc_pd_256:
10594 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010595 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010596 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010597 // CF = 1
10598 X86CC = X86::COND_B;
10599 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010600 case Intrinsic::x86_avx_vtestnzc_ps:
10601 case Intrinsic::x86_avx_vtestnzc_pd:
10602 case Intrinsic::x86_avx_vtestnzc_ps_256:
10603 case Intrinsic::x86_avx_vtestnzc_pd_256:
10604 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010605 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010606 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010607 // ZF and CF = 0
10608 X86CC = X86::COND_A;
10609 break;
10610 }
Eric Christopherfd179292009-08-27 18:07:15 +000010611
Eric Christopher71c67532009-07-29 00:28:05 +000010612 SDValue LHS = Op.getOperand(1);
10613 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010614 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10615 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010616 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10617 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10618 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010619 }
Evan Cheng5759f972008-05-04 09:15:50 +000010620
Craig Topper80e46362012-01-23 06:16:53 +000010621 // SSE/AVX shift intrinsics
10622 case Intrinsic::x86_sse2_psll_w:
10623 case Intrinsic::x86_sse2_psll_d:
10624 case Intrinsic::x86_sse2_psll_q:
10625 case Intrinsic::x86_avx2_psll_w:
10626 case Intrinsic::x86_avx2_psll_d:
10627 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010628 case Intrinsic::x86_sse2_psrl_w:
10629 case Intrinsic::x86_sse2_psrl_d:
10630 case Intrinsic::x86_sse2_psrl_q:
10631 case Intrinsic::x86_avx2_psrl_w:
10632 case Intrinsic::x86_avx2_psrl_d:
10633 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010634 case Intrinsic::x86_sse2_psra_w:
10635 case Intrinsic::x86_sse2_psra_d:
10636 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010637 case Intrinsic::x86_avx2_psra_d: {
10638 unsigned Opcode;
10639 switch (IntNo) {
10640 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10641 case Intrinsic::x86_sse2_psll_w:
10642 case Intrinsic::x86_sse2_psll_d:
10643 case Intrinsic::x86_sse2_psll_q:
10644 case Intrinsic::x86_avx2_psll_w:
10645 case Intrinsic::x86_avx2_psll_d:
10646 case Intrinsic::x86_avx2_psll_q:
10647 Opcode = X86ISD::VSHL;
10648 break;
10649 case Intrinsic::x86_sse2_psrl_w:
10650 case Intrinsic::x86_sse2_psrl_d:
10651 case Intrinsic::x86_sse2_psrl_q:
10652 case Intrinsic::x86_avx2_psrl_w:
10653 case Intrinsic::x86_avx2_psrl_d:
10654 case Intrinsic::x86_avx2_psrl_q:
10655 Opcode = X86ISD::VSRL;
10656 break;
10657 case Intrinsic::x86_sse2_psra_w:
10658 case Intrinsic::x86_sse2_psra_d:
10659 case Intrinsic::x86_avx2_psra_w:
10660 case Intrinsic::x86_avx2_psra_d:
10661 Opcode = X86ISD::VSRA;
10662 break;
10663 }
10664 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010665 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010666 }
10667
10668 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010669 case Intrinsic::x86_sse2_pslli_w:
10670 case Intrinsic::x86_sse2_pslli_d:
10671 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010672 case Intrinsic::x86_avx2_pslli_w:
10673 case Intrinsic::x86_avx2_pslli_d:
10674 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010675 case Intrinsic::x86_sse2_psrli_w:
10676 case Intrinsic::x86_sse2_psrli_d:
10677 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010678 case Intrinsic::x86_avx2_psrli_w:
10679 case Intrinsic::x86_avx2_psrli_d:
10680 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010681 case Intrinsic::x86_sse2_psrai_w:
10682 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010683 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010684 case Intrinsic::x86_avx2_psrai_d: {
10685 unsigned Opcode;
10686 switch (IntNo) {
10687 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10688 case Intrinsic::x86_sse2_pslli_w:
10689 case Intrinsic::x86_sse2_pslli_d:
10690 case Intrinsic::x86_sse2_pslli_q:
10691 case Intrinsic::x86_avx2_pslli_w:
10692 case Intrinsic::x86_avx2_pslli_d:
10693 case Intrinsic::x86_avx2_pslli_q:
10694 Opcode = X86ISD::VSHLI;
10695 break;
10696 case Intrinsic::x86_sse2_psrli_w:
10697 case Intrinsic::x86_sse2_psrli_d:
10698 case Intrinsic::x86_sse2_psrli_q:
10699 case Intrinsic::x86_avx2_psrli_w:
10700 case Intrinsic::x86_avx2_psrli_d:
10701 case Intrinsic::x86_avx2_psrli_q:
10702 Opcode = X86ISD::VSRLI;
10703 break;
10704 case Intrinsic::x86_sse2_psrai_w:
10705 case Intrinsic::x86_sse2_psrai_d:
10706 case Intrinsic::x86_avx2_psrai_w:
10707 case Intrinsic::x86_avx2_psrai_d:
10708 Opcode = X86ISD::VSRAI;
10709 break;
10710 }
10711 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010712 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010713 }
10714
Craig Topper4feb6472012-08-06 06:22:36 +000010715 case Intrinsic::x86_sse42_pcmpistria128:
10716 case Intrinsic::x86_sse42_pcmpestria128:
10717 case Intrinsic::x86_sse42_pcmpistric128:
10718 case Intrinsic::x86_sse42_pcmpestric128:
10719 case Intrinsic::x86_sse42_pcmpistrio128:
10720 case Intrinsic::x86_sse42_pcmpestrio128:
10721 case Intrinsic::x86_sse42_pcmpistris128:
10722 case Intrinsic::x86_sse42_pcmpestris128:
10723 case Intrinsic::x86_sse42_pcmpistriz128:
10724 case Intrinsic::x86_sse42_pcmpestriz128: {
10725 unsigned Opcode;
10726 unsigned X86CC;
10727 switch (IntNo) {
10728 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10729 case Intrinsic::x86_sse42_pcmpistria128:
10730 Opcode = X86ISD::PCMPISTRI;
10731 X86CC = X86::COND_A;
10732 break;
10733 case Intrinsic::x86_sse42_pcmpestria128:
10734 Opcode = X86ISD::PCMPESTRI;
10735 X86CC = X86::COND_A;
10736 break;
10737 case Intrinsic::x86_sse42_pcmpistric128:
10738 Opcode = X86ISD::PCMPISTRI;
10739 X86CC = X86::COND_B;
10740 break;
10741 case Intrinsic::x86_sse42_pcmpestric128:
10742 Opcode = X86ISD::PCMPESTRI;
10743 X86CC = X86::COND_B;
10744 break;
10745 case Intrinsic::x86_sse42_pcmpistrio128:
10746 Opcode = X86ISD::PCMPISTRI;
10747 X86CC = X86::COND_O;
10748 break;
10749 case Intrinsic::x86_sse42_pcmpestrio128:
10750 Opcode = X86ISD::PCMPESTRI;
10751 X86CC = X86::COND_O;
10752 break;
10753 case Intrinsic::x86_sse42_pcmpistris128:
10754 Opcode = X86ISD::PCMPISTRI;
10755 X86CC = X86::COND_S;
10756 break;
10757 case Intrinsic::x86_sse42_pcmpestris128:
10758 Opcode = X86ISD::PCMPESTRI;
10759 X86CC = X86::COND_S;
10760 break;
10761 case Intrinsic::x86_sse42_pcmpistriz128:
10762 Opcode = X86ISD::PCMPISTRI;
10763 X86CC = X86::COND_E;
10764 break;
10765 case Intrinsic::x86_sse42_pcmpestriz128:
10766 Opcode = X86ISD::PCMPESTRI;
10767 X86CC = X86::COND_E;
10768 break;
10769 }
10770 SmallVector<SDValue, 5> NewOps;
10771 NewOps.append(Op->op_begin()+1, Op->op_end());
10772 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10773 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10774 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10775 DAG.getConstant(X86CC, MVT::i8),
10776 SDValue(PCMP.getNode(), 1));
10777 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10778 }
Craig Topper6d688152012-08-14 07:43:25 +000010779
Craig Topper4feb6472012-08-06 06:22:36 +000010780 case Intrinsic::x86_sse42_pcmpistri128:
10781 case Intrinsic::x86_sse42_pcmpestri128: {
10782 unsigned Opcode;
10783 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10784 Opcode = X86ISD::PCMPISTRI;
10785 else
10786 Opcode = X86ISD::PCMPESTRI;
10787
10788 SmallVector<SDValue, 5> NewOps;
10789 NewOps.append(Op->op_begin()+1, Op->op_end());
10790 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10791 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10792 }
Craig Topper0e292372012-08-24 04:03:22 +000010793 case Intrinsic::x86_fma_vfmadd_ps:
10794 case Intrinsic::x86_fma_vfmadd_pd:
10795 case Intrinsic::x86_fma_vfmsub_ps:
10796 case Intrinsic::x86_fma_vfmsub_pd:
10797 case Intrinsic::x86_fma_vfnmadd_ps:
10798 case Intrinsic::x86_fma_vfnmadd_pd:
10799 case Intrinsic::x86_fma_vfnmsub_ps:
10800 case Intrinsic::x86_fma_vfnmsub_pd:
10801 case Intrinsic::x86_fma_vfmaddsub_ps:
10802 case Intrinsic::x86_fma_vfmaddsub_pd:
10803 case Intrinsic::x86_fma_vfmsubadd_ps:
10804 case Intrinsic::x86_fma_vfmsubadd_pd:
10805 case Intrinsic::x86_fma_vfmadd_ps_256:
10806 case Intrinsic::x86_fma_vfmadd_pd_256:
10807 case Intrinsic::x86_fma_vfmsub_ps_256:
10808 case Intrinsic::x86_fma_vfmsub_pd_256:
10809 case Intrinsic::x86_fma_vfnmadd_ps_256:
10810 case Intrinsic::x86_fma_vfnmadd_pd_256:
10811 case Intrinsic::x86_fma_vfnmsub_ps_256:
10812 case Intrinsic::x86_fma_vfnmsub_pd_256:
10813 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10814 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10815 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10816 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010817 unsigned Opc;
10818 switch (IntNo) {
10819 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10820 case Intrinsic::x86_fma_vfmadd_ps:
10821 case Intrinsic::x86_fma_vfmadd_pd:
10822 case Intrinsic::x86_fma_vfmadd_ps_256:
10823 case Intrinsic::x86_fma_vfmadd_pd_256:
10824 Opc = X86ISD::FMADD;
10825 break;
10826 case Intrinsic::x86_fma_vfmsub_ps:
10827 case Intrinsic::x86_fma_vfmsub_pd:
10828 case Intrinsic::x86_fma_vfmsub_ps_256:
10829 case Intrinsic::x86_fma_vfmsub_pd_256:
10830 Opc = X86ISD::FMSUB;
10831 break;
10832 case Intrinsic::x86_fma_vfnmadd_ps:
10833 case Intrinsic::x86_fma_vfnmadd_pd:
10834 case Intrinsic::x86_fma_vfnmadd_ps_256:
10835 case Intrinsic::x86_fma_vfnmadd_pd_256:
10836 Opc = X86ISD::FNMADD;
10837 break;
10838 case Intrinsic::x86_fma_vfnmsub_ps:
10839 case Intrinsic::x86_fma_vfnmsub_pd:
10840 case Intrinsic::x86_fma_vfnmsub_ps_256:
10841 case Intrinsic::x86_fma_vfnmsub_pd_256:
10842 Opc = X86ISD::FNMSUB;
10843 break;
10844 case Intrinsic::x86_fma_vfmaddsub_ps:
10845 case Intrinsic::x86_fma_vfmaddsub_pd:
10846 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10847 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10848 Opc = X86ISD::FMADDSUB;
10849 break;
10850 case Intrinsic::x86_fma_vfmsubadd_ps:
10851 case Intrinsic::x86_fma_vfmsubadd_pd:
10852 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10853 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10854 Opc = X86ISD::FMSUBADD;
10855 break;
10856 }
10857
10858 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10859 Op.getOperand(2), Op.getOperand(3));
10860 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010861 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010862}
Evan Cheng72261582005-12-20 06:22:03 +000010863
Craig Topper55b24052012-09-11 06:15:32 +000010864static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010865 DebugLoc dl = Op.getDebugLoc();
10866 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10867 switch (IntNo) {
10868 default: return SDValue(); // Don't custom lower most intrinsics.
10869
10870 // RDRAND intrinsics.
10871 case Intrinsic::x86_rdrand_16:
10872 case Intrinsic::x86_rdrand_32:
10873 case Intrinsic::x86_rdrand_64: {
10874 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010875 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10876 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010877
10878 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10879 // return the value from Rand, which is always 0, casted to i32.
10880 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10881 DAG.getConstant(1, Op->getValueType(1)),
10882 DAG.getConstant(X86::COND_B, MVT::i32),
10883 SDValue(Result.getNode(), 1) };
10884 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10885 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10886 Ops, 4);
10887
10888 // Return { result, isValid, chain }.
10889 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010890 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010891 }
10892 }
10893}
10894
Dan Gohmand858e902010-04-17 15:26:15 +000010895SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10896 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010897 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10898 MFI->setReturnAddressIsTaken(true);
10899
Bill Wendling64e87322009-01-16 19:25:27 +000010900 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010901 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010902 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010903
10904 if (Depth > 0) {
10905 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10906 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010907 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10908 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10909 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010910 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010911 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010912 }
10913
10914 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010915 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010916 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010917 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010918}
10919
Dan Gohmand858e902010-04-17 15:26:15 +000010920SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010921 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10922 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010923
Owen Andersone50ed302009-08-10 22:56:29 +000010924 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010925 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010926 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10927 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010928 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010929 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010930 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10931 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010932 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010933 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010934}
10935
Dan Gohman475871a2008-07-27 21:46:04 +000010936SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010937 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010938 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010939}
10940
Dan Gohmand858e902010-04-17 15:26:15 +000010941SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010942 SDValue Chain = Op.getOperand(0);
10943 SDValue Offset = Op.getOperand(1);
10944 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010945 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010946
Dan Gohmand8816272010-08-11 18:14:00 +000010947 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10948 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10949 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010950 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010951
Dan Gohmand8816272010-08-11 18:14:00 +000010952 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010953 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010954 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010955 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10956 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010957 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010958
Dale Johannesene4d209d2009-02-03 20:21:25 +000010959 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010960 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010961 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010962}
10963
Michael Liao6c0e04c2012-10-15 22:39:43 +000010964SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10965 SelectionDAG &DAG) const {
10966 DebugLoc DL = Op.getDebugLoc();
10967 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10968 DAG.getVTList(MVT::i32, MVT::Other),
10969 Op.getOperand(0), Op.getOperand(1));
10970}
10971
10972SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10973 SelectionDAG &DAG) const {
10974 DebugLoc DL = Op.getDebugLoc();
10975 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10976 Op.getOperand(0), Op.getOperand(1));
10977}
10978
Craig Topper55b24052012-09-11 06:15:32 +000010979static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010980 return Op.getOperand(0);
10981}
10982
10983SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10984 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010985 SDValue Root = Op.getOperand(0);
10986 SDValue Trmp = Op.getOperand(1); // trampoline
10987 SDValue FPtr = Op.getOperand(2); // nested function
10988 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010989 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010990
Dan Gohman69de1932008-02-06 22:27:42 +000010991 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010992 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010993
10994 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010995 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010996
10997 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010998 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10999 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011000
Michael Liao7abf67a2012-10-04 19:50:43 +000011001 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11002 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011003
11004 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11005
11006 // Load the pointer to the nested function into R11.
11007 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011008 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011009 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011010 Addr, MachinePointerInfo(TrmpAddr),
11011 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011012
Owen Anderson825b72b2009-08-11 20:47:22 +000011013 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11014 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011015 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11016 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011017 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011018
11019 // Load the 'nest' parameter value into R10.
11020 // R10 is specified in X86CallingConv.td
11021 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011022 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11023 DAG.getConstant(10, MVT::i64));
11024 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011025 Addr, MachinePointerInfo(TrmpAddr, 10),
11026 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011027
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11029 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011030 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11031 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011032 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011033
11034 // Jump to the nested function.
11035 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011036 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11037 DAG.getConstant(20, MVT::i64));
11038 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011039 Addr, MachinePointerInfo(TrmpAddr, 20),
11040 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011041
11042 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011043 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11044 DAG.getConstant(22, MVT::i64));
11045 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011046 MachinePointerInfo(TrmpAddr, 22),
11047 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011048
Duncan Sands4a544a72011-09-06 13:37:06 +000011049 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011050 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011051 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011052 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011053 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011054 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011055
11056 switch (CC) {
11057 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011058 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011059 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011060 case CallingConv::X86_StdCall: {
11061 // Pass 'nest' parameter in ECX.
11062 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011063 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011064
11065 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011066 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011067 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011068
Chris Lattner58d74912008-03-12 17:45:29 +000011069 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011070 unsigned InRegCount = 0;
11071 unsigned Idx = 1;
11072
11073 for (FunctionType::param_iterator I = FTy->param_begin(),
11074 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011075 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011076 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011077 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011078
11079 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011080 report_fatal_error("Nest register in use - reduce number of inreg"
11081 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011082 }
11083 }
11084 break;
11085 }
11086 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011087 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011088 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011089 // Pass 'nest' parameter in EAX.
11090 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011091 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011092 break;
11093 }
11094
Dan Gohman475871a2008-07-27 21:46:04 +000011095 SDValue OutChains[4];
11096 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011097
Owen Anderson825b72b2009-08-11 20:47:22 +000011098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11099 DAG.getConstant(10, MVT::i32));
11100 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011101
Chris Lattnera62fe662010-02-05 19:20:30 +000011102 // This is storing the opcode for MOV32ri.
11103 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011104 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011105 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011106 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011107 Trmp, MachinePointerInfo(TrmpAddr),
11108 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011109
Owen Anderson825b72b2009-08-11 20:47:22 +000011110 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11111 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011112 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11113 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011114 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011115
Chris Lattnera62fe662010-02-05 19:20:30 +000011116 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011117 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11118 DAG.getConstant(5, MVT::i32));
11119 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011120 MachinePointerInfo(TrmpAddr, 5),
11121 false, false, 1);
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(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011125 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11126 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011127 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011128
Duncan Sands4a544a72011-09-06 13:37:06 +000011129 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011130 }
11131}
11132
Dan Gohmand858e902010-04-17 15:26:15 +000011133SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11134 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011135 /*
11136 The rounding mode is in bits 11:10 of FPSR, and has the following
11137 settings:
11138 00 Round to nearest
11139 01 Round to -inf
11140 10 Round to +inf
11141 11 Round to 0
11142
11143 FLT_ROUNDS, on the other hand, expects the following:
11144 -1 Undefined
11145 0 Round to 0
11146 1 Round to nearest
11147 2 Round to +inf
11148 3 Round to -inf
11149
11150 To perform the conversion, we do:
11151 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11152 */
11153
11154 MachineFunction &MF = DAG.getMachineFunction();
11155 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011156 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011157 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011158 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011159 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011160
11161 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011162 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011163 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011164
Chris Lattner2156b792010-09-22 01:11:26 +000011165 MachineMemOperand *MMO =
11166 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11167 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011168
Chris Lattner2156b792010-09-22 01:11:26 +000011169 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11170 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11171 DAG.getVTList(MVT::Other),
11172 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011173
11174 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011175 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011176 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011177
11178 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011179 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011180 DAG.getNode(ISD::SRL, DL, MVT::i16,
11181 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011182 CWD, DAG.getConstant(0x800, MVT::i16)),
11183 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011184 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011185 DAG.getNode(ISD::SRL, DL, MVT::i16,
11186 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011187 CWD, DAG.getConstant(0x400, MVT::i16)),
11188 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011189
Dan Gohman475871a2008-07-27 21:46:04 +000011190 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011191 DAG.getNode(ISD::AND, DL, MVT::i16,
11192 DAG.getNode(ISD::ADD, DL, MVT::i16,
11193 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011194 DAG.getConstant(1, MVT::i16)),
11195 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011196
Duncan Sands83ec4b62008-06-06 12:08:01 +000011197 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011198 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011199}
11200
Craig Topper55b24052012-09-11 06:15:32 +000011201static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011202 EVT VT = Op.getValueType();
11203 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011204 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011205 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011206
11207 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011208 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011209 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011210 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011211 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011212 }
Evan Cheng18efe262007-12-14 02:13:44 +000011213
Evan Cheng152804e2007-12-14 08:30:15 +000011214 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011215 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011216 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011217
11218 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011219 SDValue Ops[] = {
11220 Op,
11221 DAG.getConstant(NumBits+NumBits-1, OpVT),
11222 DAG.getConstant(X86::COND_E, MVT::i8),
11223 Op.getValue(1)
11224 };
11225 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011226
11227 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011228 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011229
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 if (VT == MVT::i8)
11231 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011232 return Op;
11233}
11234
Craig Topper55b24052012-09-11 06:15:32 +000011235static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011236 EVT VT = Op.getValueType();
11237 EVT OpVT = VT;
11238 unsigned NumBits = VT.getSizeInBits();
11239 DebugLoc dl = Op.getDebugLoc();
11240
11241 Op = Op.getOperand(0);
11242 if (VT == MVT::i8) {
11243 // Zero extend to i32 since there is not an i8 bsr.
11244 OpVT = MVT::i32;
11245 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11246 }
11247
11248 // Issue a bsr (scan bits in reverse).
11249 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11250 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11251
11252 // And xor with NumBits-1.
11253 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11254
11255 if (VT == MVT::i8)
11256 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11257 return Op;
11258}
11259
Craig Topper55b24052012-09-11 06:15:32 +000011260static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011261 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011262 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011263 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011264 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011265
11266 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011267 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011268 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011269
11270 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011271 SDValue Ops[] = {
11272 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011273 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011274 DAG.getConstant(X86::COND_E, MVT::i8),
11275 Op.getValue(1)
11276 };
Chandler Carruth77821022011-12-24 12:12:34 +000011277 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011278}
11279
Craig Topper13894fa2011-08-24 06:14:18 +000011280// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11281// ones, and then concatenate the result back.
11282static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011283 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011284
Craig Topper7a9a28b2012-08-12 02:23:29 +000011285 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011286 "Unsupported value type for operation");
11287
Craig Topper66ddd152012-04-27 22:54:43 +000011288 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011289 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011290
11291 // Extract the LHS vectors
11292 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011293 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11294 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011295
11296 // Extract the RHS vectors
11297 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011298 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11299 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011300
11301 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11302 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11303
11304 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11305 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11306 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11307}
11308
Craig Topper55b24052012-09-11 06:15:32 +000011309static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011310 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011311 Op.getValueType().isInteger() &&
11312 "Only handle AVX 256-bit vector integer operation");
11313 return Lower256IntArith(Op, DAG);
11314}
11315
Craig Topper55b24052012-09-11 06:15:32 +000011316static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011317 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011318 Op.getValueType().isInteger() &&
11319 "Only handle AVX 256-bit vector integer operation");
11320 return Lower256IntArith(Op, DAG);
11321}
11322
Craig Topper55b24052012-09-11 06:15:32 +000011323static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11324 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011325 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011326 EVT VT = Op.getValueType();
11327
11328 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011329 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011330 return Lower256IntArith(Op, DAG);
11331
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011332 SDValue A = Op.getOperand(0);
11333 SDValue B = Op.getOperand(1);
11334
11335 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11336 if (VT == MVT::v4i32) {
11337 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11338 "Should not custom lower when pmuldq is available!");
11339
11340 // Extract the odd parts.
11341 const int UnpackMask[] = { 1, -1, 3, -1 };
11342 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11343 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11344
11345 // Multiply the even parts.
11346 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11347 // Now multiply odd parts.
11348 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11349
11350 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11351 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11352
11353 // Merge the two vectors back together with a shuffle. This expands into 2
11354 // shuffles.
11355 const int ShufMask[] = { 0, 4, 2, 6 };
11356 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11357 }
11358
Craig Topper5b209e82012-02-05 03:14:49 +000011359 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11360 "Only know how to lower V2I64/V4I64 multiply");
11361
Craig Topper5b209e82012-02-05 03:14:49 +000011362 // Ahi = psrlqi(a, 32);
11363 // Bhi = psrlqi(b, 32);
11364 //
11365 // AloBlo = pmuludq(a, b);
11366 // AloBhi = pmuludq(a, Bhi);
11367 // AhiBlo = pmuludq(Ahi, b);
11368
11369 // AloBhi = psllqi(AloBhi, 32);
11370 // AhiBlo = psllqi(AhiBlo, 32);
11371 // return AloBlo + AloBhi + AhiBlo;
11372
Craig Topper5b209e82012-02-05 03:14:49 +000011373 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011374
Craig Topper5b209e82012-02-05 03:14:49 +000011375 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11376 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011377
Craig Topper5b209e82012-02-05 03:14:49 +000011378 // Bit cast to 32-bit vectors for MULUDQ
11379 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11380 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11381 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11382 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11383 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011384
Craig Topper5b209e82012-02-05 03:14:49 +000011385 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11386 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11387 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011388
Craig Topper5b209e82012-02-05 03:14:49 +000011389 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11390 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011391
Dale Johannesene4d209d2009-02-03 20:21:25 +000011392 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011393 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011394}
11395
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011396SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11397 EVT VT = Op.getValueType();
11398 EVT EltTy = VT.getVectorElementType();
11399 unsigned NumElts = VT.getVectorNumElements();
11400 SDValue N0 = Op.getOperand(0);
11401 DebugLoc dl = Op.getDebugLoc();
11402
11403 // Lower sdiv X, pow2-const.
11404 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11405 if (!C)
11406 return SDValue();
11407
11408 APInt SplatValue, SplatUndef;
11409 unsigned MinSplatBits;
11410 bool HasAnyUndefs;
11411 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11412 return SDValue();
11413
11414 if ((SplatValue != 0) &&
11415 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11416 unsigned lg2 = SplatValue.countTrailingZeros();
11417 // Splat the sign bit.
11418 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11419 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11420 // Add (N0 < 0) ? abs2 - 1 : 0;
11421 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11422 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11423 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11424 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11425 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11426
11427 // If we're dividing by a positive value, we're done. Otherwise, we must
11428 // negate the result.
11429 if (SplatValue.isNonNegative())
11430 return SRA;
11431
11432 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11433 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11434 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11435 }
11436 return SDValue();
11437}
11438
Nadav Rotem43012222011-05-11 08:12:09 +000011439SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11440
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011441 EVT VT = Op.getValueType();
11442 DebugLoc dl = Op.getDebugLoc();
11443 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011444 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011445 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011446
Craig Topper1accb7e2012-01-10 06:54:16 +000011447 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011448 return SDValue();
11449
Nadav Rotem43012222011-05-11 08:12:09 +000011450 // Optimize shl/srl/sra with constant shift amount.
11451 if (isSplatVector(Amt.getNode())) {
11452 SDValue SclrAmt = Amt->getOperand(0);
11453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11454 uint64_t ShiftAmt = C->getZExtValue();
11455
Craig Toppered2e13d2012-01-22 19:15:14 +000011456 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011457 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011458 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11459 if (Op.getOpcode() == ISD::SHL)
11460 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11461 DAG.getConstant(ShiftAmt, MVT::i32));
11462 if (Op.getOpcode() == ISD::SRL)
11463 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11464 DAG.getConstant(ShiftAmt, MVT::i32));
11465 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11466 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11467 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011468 }
11469
Craig Toppered2e13d2012-01-22 19:15:14 +000011470 if (VT == MVT::v16i8) {
11471 if (Op.getOpcode() == ISD::SHL) {
11472 // Make a large shift.
11473 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11474 DAG.getConstant(ShiftAmt, MVT::i32));
11475 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11476 // Zero out the rightmost bits.
11477 SmallVector<SDValue, 16> V(16,
11478 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11479 MVT::i8));
11480 return DAG.getNode(ISD::AND, dl, VT, SHL,
11481 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011482 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011483 if (Op.getOpcode() == ISD::SRL) {
11484 // Make a large shift.
11485 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11486 DAG.getConstant(ShiftAmt, MVT::i32));
11487 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11488 // Zero out the leftmost bits.
11489 SmallVector<SDValue, 16> V(16,
11490 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11491 MVT::i8));
11492 return DAG.getNode(ISD::AND, dl, VT, SRL,
11493 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11494 }
11495 if (Op.getOpcode() == ISD::SRA) {
11496 if (ShiftAmt == 7) {
11497 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011498 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011499 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011500 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011501
Craig Toppered2e13d2012-01-22 19:15:14 +000011502 // R s>> a === ((R u>> a) ^ m) - m
11503 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11504 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11505 MVT::i8));
11506 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11507 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11508 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11509 return Res;
11510 }
Craig Topper731dfd02012-04-23 03:42:40 +000011511 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011512 }
Craig Topper46154eb2011-11-11 07:39:23 +000011513
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011514 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011515 if (Op.getOpcode() == ISD::SHL) {
11516 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011517 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11518 DAG.getConstant(ShiftAmt, MVT::i32));
11519 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011520 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011521 SmallVector<SDValue, 32> V(32,
11522 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11523 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011524 return DAG.getNode(ISD::AND, dl, VT, SHL,
11525 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011526 }
Craig Topper0d86d462011-11-20 00:12:05 +000011527 if (Op.getOpcode() == ISD::SRL) {
11528 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011529 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11530 DAG.getConstant(ShiftAmt, MVT::i32));
11531 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011532 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011533 SmallVector<SDValue, 32> V(32,
11534 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11535 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011536 return DAG.getNode(ISD::AND, dl, VT, SRL,
11537 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11538 }
11539 if (Op.getOpcode() == ISD::SRA) {
11540 if (ShiftAmt == 7) {
11541 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011542 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011543 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011544 }
11545
11546 // R s>> a === ((R u>> a) ^ m) - m
11547 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11548 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11549 MVT::i8));
11550 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11551 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11552 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11553 return Res;
11554 }
Craig Topper731dfd02012-04-23 03:42:40 +000011555 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011556 }
Nadav Rotem43012222011-05-11 08:12:09 +000011557 }
11558 }
11559
11560 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011561 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011562 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11563 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011564
Chris Lattner7302d802012-02-06 21:56:39 +000011565 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11566 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011567 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11568 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011569 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011570 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011571
11572 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011573 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011574 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11575 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11576 }
Nadav Rotem43012222011-05-11 08:12:09 +000011577 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011578 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011579
Nate Begeman51409212010-07-28 00:21:48 +000011580 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011581 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11582 DAG.getConstant(5, MVT::i32));
11583 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011584
Lang Hames8b99c1e2011-12-17 01:08:46 +000011585 // Turn 'a' into a mask suitable for VSELECT
11586 SDValue VSelM = DAG.getConstant(0x80, VT);
11587 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011588 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011589
Lang Hames8b99c1e2011-12-17 01:08:46 +000011590 SDValue CM1 = DAG.getConstant(0x0f, VT);
11591 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011592
Lang Hames8b99c1e2011-12-17 01:08:46 +000011593 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11594 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011595 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11596 DAG.getConstant(4, MVT::i32), DAG);
11597 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011598 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11599
Nate Begeman51409212010-07-28 00:21:48 +000011600 // a += a
11601 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011602 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);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011604
Lang Hames8b99c1e2011-12-17 01:08:46 +000011605 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11606 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011607 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11608 DAG.getConstant(2, MVT::i32), DAG);
11609 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011610 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11611
Nate Begeman51409212010-07-28 00:21:48 +000011612 // a += a
11613 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011614 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011615 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011616
Lang Hames8b99c1e2011-12-17 01:08:46 +000011617 // return VSELECT(r, r+r, a);
11618 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011619 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011620 return R;
11621 }
Craig Topper46154eb2011-11-11 07:39:23 +000011622
11623 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011624 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011625 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011626 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11627 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11628
11629 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011630 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11631 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011632
11633 // Recreate the shift amount vectors
11634 SDValue Amt1, Amt2;
11635 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11636 // Constant shift amount
11637 SmallVector<SDValue, 4> Amt1Csts;
11638 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011639 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011640 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011641 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011642 Amt2Csts.push_back(Amt->getOperand(i));
11643
11644 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11645 &Amt1Csts[0], NumElems/2);
11646 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11647 &Amt2Csts[0], NumElems/2);
11648 } else {
11649 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011650 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11651 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011652 }
11653
11654 // Issue new vector shifts for the smaller types
11655 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11656 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11657
11658 // Concatenate the result back
11659 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11660 }
11661
Nate Begeman51409212010-07-28 00:21:48 +000011662 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011663}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011664
Craig Topper55b24052012-09-11 06:15:32 +000011665static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011666 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11667 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011668 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11669 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011670 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011671 SDValue LHS = N->getOperand(0);
11672 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011673 unsigned BaseOp = 0;
11674 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011675 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011676 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011677 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011678 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011679 // A subtract of one will be selected as a INC. Note that INC doesn't
11680 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011681 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11682 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011683 BaseOp = X86ISD::INC;
11684 Cond = X86::COND_O;
11685 break;
11686 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011687 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011688 Cond = X86::COND_O;
11689 break;
11690 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011691 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011692 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011693 break;
11694 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011695 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11696 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011697 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11698 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011699 BaseOp = X86ISD::DEC;
11700 Cond = X86::COND_O;
11701 break;
11702 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011703 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011704 Cond = X86::COND_O;
11705 break;
11706 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011707 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011708 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011709 break;
11710 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011711 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011712 Cond = X86::COND_O;
11713 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011714 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11715 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11716 MVT::i32);
11717 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011718
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011719 SDValue SetCC =
11720 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11721 DAG.getConstant(X86::COND_O, MVT::i32),
11722 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011723
Dan Gohman6e5fda22011-07-22 18:45:15 +000011724 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011725 }
Bill Wendling74c37652008-12-09 22:08:41 +000011726 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011727
Bill Wendling61edeb52008-12-02 01:06:39 +000011728 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011729 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011730 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011731
Bill Wendling61edeb52008-12-02 01:06:39 +000011732 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011733 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11734 DAG.getConstant(Cond, MVT::i32),
11735 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011736
Dan Gohman6e5fda22011-07-22 18:45:15 +000011737 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011738}
11739
Chad Rosier30450e82011-12-22 22:35:21 +000011740SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11741 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011742 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011743 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11744 EVT VT = Op.getValueType();
11745
Craig Toppered2e13d2012-01-22 19:15:14 +000011746 if (!Subtarget->hasSSE2() || !VT.isVector())
11747 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011748
Craig Toppered2e13d2012-01-22 19:15:14 +000011749 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11750 ExtraVT.getScalarType().getSizeInBits();
11751 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11752
11753 switch (VT.getSimpleVT().SimpleTy) {
11754 default: return SDValue();
11755 case MVT::v8i32:
11756 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011757 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011758 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011759 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011760 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011761 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011762
Craig Toppered2e13d2012-01-22 19:15:14 +000011763 // Extract the LHS vectors
11764 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011765 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11766 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011767
Craig Toppered2e13d2012-01-22 19:15:14 +000011768 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11769 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011770
Craig Toppered2e13d2012-01-22 19:15:14 +000011771 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011772 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011773 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11774 ExtraNumElems/2);
11775 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011776
Craig Toppered2e13d2012-01-22 19:15:14 +000011777 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11778 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011779
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011780 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011781 }
11782 // fall through
11783 case MVT::v4i32:
11784 case MVT::v8i16: {
11785 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11786 Op.getOperand(0), ShAmt, DAG);
11787 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011788 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011789 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011790}
11791
Craig Topper55b24052012-09-11 06:15:32 +000011792static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11793 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011794 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011795
Eric Christopher77ed1352011-07-08 00:04:56 +000011796 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11797 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011798 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011799 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011800 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011801 SDValue Ops[] = {
11802 DAG.getRegister(X86::ESP, MVT::i32), // Base
11803 DAG.getTargetConstant(1, MVT::i8), // Scale
11804 DAG.getRegister(0, MVT::i32), // Index
11805 DAG.getTargetConstant(0, MVT::i32), // Disp
11806 DAG.getRegister(0, MVT::i32), // Segment.
11807 Zero,
11808 Chain
11809 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011810 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011811 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11812 array_lengthof(Ops));
11813 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011814 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011815
Eric Christopher9a9d2752010-07-22 02:48:34 +000011816 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011817 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011818 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011819
Chris Lattner132929a2010-08-14 17:26:09 +000011820 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11821 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11822 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11823 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011824
Chris Lattner132929a2010-08-14 17:26:09 +000011825 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11826 if (!Op1 && !Op2 && !Op3 && Op4)
11827 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011828
Chris Lattner132929a2010-08-14 17:26:09 +000011829 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11830 if (Op1 && !Op2 && !Op3 && !Op4)
11831 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011832
11833 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011834 // (MFENCE)>;
11835 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011836}
11837
Craig Topper55b24052012-09-11 06:15:32 +000011838static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11839 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011840 DebugLoc dl = Op.getDebugLoc();
11841 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11842 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11843 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11844 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11845
11846 // The only fence that needs an instruction is a sequentially-consistent
11847 // cross-thread fence.
11848 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11849 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11850 // no-sse2). There isn't any reason to disable it if the target processor
11851 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011852 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011853 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11854
11855 SDValue Chain = Op.getOperand(0);
11856 SDValue Zero = DAG.getConstant(0, MVT::i32);
11857 SDValue Ops[] = {
11858 DAG.getRegister(X86::ESP, MVT::i32), // Base
11859 DAG.getTargetConstant(1, MVT::i8), // Scale
11860 DAG.getRegister(0, MVT::i32), // Index
11861 DAG.getTargetConstant(0, MVT::i32), // Disp
11862 DAG.getRegister(0, MVT::i32), // Segment.
11863 Zero,
11864 Chain
11865 };
11866 SDNode *Res =
11867 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11868 array_lengthof(Ops));
11869 return SDValue(Res, 0);
11870 }
11871
11872 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11873 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11874}
11875
Craig Topper55b24052012-09-11 06:15:32 +000011876static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11877 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011878 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011879 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011880 unsigned Reg = 0;
11881 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011882 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011883 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011884 case MVT::i8: Reg = X86::AL; size = 1; break;
11885 case MVT::i16: Reg = X86::AX; size = 2; break;
11886 case MVT::i32: Reg = X86::EAX; size = 4; break;
11887 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011888 assert(Subtarget->is64Bit() && "Node not type legal!");
11889 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011890 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011891 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011892 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011893 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011894 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011895 Op.getOperand(1),
11896 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011897 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011898 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011899 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011900 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11901 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11902 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011903 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011904 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011905 return cpOut;
11906}
11907
Craig Topper55b24052012-09-11 06:15:32 +000011908static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11909 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011910 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011911 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011912 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011913 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011914 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011915 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11916 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011917 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011918 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11919 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011920 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011921 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011922 rdx.getValue(1)
11923 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011924 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011925}
11926
Craig Topper55b24052012-09-11 06:15:32 +000011927SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011928 EVT SrcVT = Op.getOperand(0).getValueType();
11929 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011930 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011931 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011932 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011933 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011934 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011935 // i64 <=> MMX conversions are Legal.
11936 if (SrcVT==MVT::i64 && DstVT.isVector())
11937 return Op;
11938 if (DstVT==MVT::i64 && SrcVT.isVector())
11939 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011940 // MMX <=> MMX conversions are Legal.
11941 if (SrcVT.isVector() && DstVT.isVector())
11942 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011943 // All other conversions need to be expanded.
11944 return SDValue();
11945}
Chris Lattner5b856542010-12-20 00:59:46 +000011946
Craig Topper55b24052012-09-11 06:15:32 +000011947static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011948 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011949 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011950 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011951 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011952 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011953 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011954 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011955 Node->getOperand(0),
11956 Node->getOperand(1), negOp,
11957 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011958 cast<AtomicSDNode>(Node)->getAlignment(),
11959 cast<AtomicSDNode>(Node)->getOrdering(),
11960 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011961}
11962
Eli Friedman327236c2011-08-24 20:50:09 +000011963static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11964 SDNode *Node = Op.getNode();
11965 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011966 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011967
11968 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011969 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11970 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11971 // (The only way to get a 16-byte store is cmpxchg16b)
11972 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11973 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11974 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011975 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11976 cast<AtomicSDNode>(Node)->getMemoryVT(),
11977 Node->getOperand(0),
11978 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011979 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011980 cast<AtomicSDNode>(Node)->getOrdering(),
11981 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011982 return Swap.getValue(1);
11983 }
11984 // Other atomic stores have a simple pattern.
11985 return Op;
11986}
11987
Chris Lattner5b856542010-12-20 00:59:46 +000011988static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11989 EVT VT = Op.getNode()->getValueType(0);
11990
11991 // Let legalize expand this if it isn't a legal type yet.
11992 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11993 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011994
Chris Lattner5b856542010-12-20 00:59:46 +000011995 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011996
Chris Lattner5b856542010-12-20 00:59:46 +000011997 unsigned Opc;
11998 bool ExtraOp = false;
11999 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012000 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012001 case ISD::ADDC: Opc = X86ISD::ADD; break;
12002 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12003 case ISD::SUBC: Opc = X86ISD::SUB; break;
12004 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12005 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012006
Chris Lattner5b856542010-12-20 00:59:46 +000012007 if (!ExtraOp)
12008 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12009 Op.getOperand(1));
12010 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12011 Op.getOperand(1), Op.getOperand(2));
12012}
12013
Evan Cheng0db9fe62006-04-25 20:13:52 +000012014/// LowerOperation - Provide custom lowering hooks for some operations.
12015///
Dan Gohmand858e902010-04-17 15:26:15 +000012016SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012017 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012018 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012019 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012020 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12021 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12022 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012023 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012024 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012025 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012026 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012027 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12028 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12029 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012030 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12031 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012032 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12033 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12034 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012035 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012036 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012037 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012038 case ISD::SHL_PARTS:
12039 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012040 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012041 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012042 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012043 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012044 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12045 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12046 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012047 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012048 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012049 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012050 case ISD::FABS: return LowerFABS(Op, DAG);
12051 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012052 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012053 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012054 case ISD::SETCC: return LowerSETCC(Op, DAG);
12055 case ISD::SELECT: return LowerSELECT(Op, DAG);
12056 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012057 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012058 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012059 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012060 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012061 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012062 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012063 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12064 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012065 case ISD::FRAME_TO_ARGS_OFFSET:
12066 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012067 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012068 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012069 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12070 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012071 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12072 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012073 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012074 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012075 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012076 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012077 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012078 case ISD::SRA:
12079 case ISD::SRL:
12080 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012081 case ISD::SADDO:
12082 case ISD::UADDO:
12083 case ISD::SSUBO:
12084 case ISD::USUBO:
12085 case ISD::SMULO:
12086 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012087 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012088 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012089 case ISD::ADDC:
12090 case ISD::ADDE:
12091 case ISD::SUBC:
12092 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012093 case ISD::ADD: return LowerADD(Op, DAG);
12094 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012095 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012096 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012097}
12098
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012099static void ReplaceATOMIC_LOAD(SDNode *Node,
12100 SmallVectorImpl<SDValue> &Results,
12101 SelectionDAG &DAG) {
12102 DebugLoc dl = Node->getDebugLoc();
12103 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12104
12105 // Convert wide load -> cmpxchg8b/cmpxchg16b
12106 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12107 // (The only way to get a 16-byte load is cmpxchg16b)
12108 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012109 SDValue Zero = DAG.getConstant(0, VT);
12110 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012111 Node->getOperand(0),
12112 Node->getOperand(1), Zero, Zero,
12113 cast<AtomicSDNode>(Node)->getMemOperand(),
12114 cast<AtomicSDNode>(Node)->getOrdering(),
12115 cast<AtomicSDNode>(Node)->getSynchScope());
12116 Results.push_back(Swap.getValue(0));
12117 Results.push_back(Swap.getValue(1));
12118}
12119
Craig Topperc0878702012-08-17 06:55:11 +000012120static void
Duncan Sands1607f052008-12-01 11:39:25 +000012121ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012122 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012123 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012124 assert (Node->getValueType(0) == MVT::i64 &&
12125 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012126
12127 SDValue Chain = Node->getOperand(0);
12128 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012129 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012130 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012131 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012132 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012133 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012134 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012135 SDValue Result =
12136 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12137 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012138 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012139 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012140 Results.push_back(Result.getValue(2));
12141}
12142
Duncan Sands126d9072008-07-04 11:47:58 +000012143/// ReplaceNodeResults - Replace a node with an illegal result type
12144/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012145void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12146 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012147 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012148 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012149 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012150 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012151 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012152 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012153 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012154 case ISD::ADDC:
12155 case ISD::ADDE:
12156 case ISD::SUBC:
12157 case ISD::SUBE:
12158 // We don't want to expand or promote these.
12159 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012160 case ISD::FP_TO_SINT:
12161 case ISD::FP_TO_UINT: {
12162 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12163
12164 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12165 return;
12166
Eli Friedman948e95a2009-05-23 09:59:16 +000012167 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012168 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012169 SDValue FIST = Vals.first, StackSlot = Vals.second;
12170 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012171 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012172 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012173 if (StackSlot.getNode() != 0)
12174 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12175 MachinePointerInfo(),
12176 false, false, false, 0));
12177 else
12178 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012179 }
12180 return;
12181 }
Michael Liao991b6a22012-10-24 04:09:32 +000012182 case ISD::UINT_TO_FP: {
12183 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12184 N->getValueType(0) != MVT::v2f32)
12185 return;
12186 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12187 N->getOperand(0));
12188 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12189 MVT::f64);
12190 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12191 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12192 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12193 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12194 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12195 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12196 return;
12197 }
Michael Liao44c2d612012-10-10 16:53:28 +000012198 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012199 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12200 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012201 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12202 Results.push_back(V);
12203 return;
12204 }
Duncan Sands1607f052008-12-01 11:39:25 +000012205 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012206 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012207 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012208 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012209 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012210 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012211 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012212 eax.getValue(2));
12213 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12214 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012215 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012216 Results.push_back(edx.getValue(1));
12217 return;
12218 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012219 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012220 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012221 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012222 bool Regs64bit = T == MVT::i128;
12223 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012224 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012225 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12226 DAG.getConstant(0, HalfT));
12227 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12228 DAG.getConstant(1, HalfT));
12229 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12230 Regs64bit ? X86::RAX : X86::EAX,
12231 cpInL, SDValue());
12232 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12233 Regs64bit ? X86::RDX : X86::EDX,
12234 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012235 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012236 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12237 DAG.getConstant(0, HalfT));
12238 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12239 DAG.getConstant(1, HalfT));
12240 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12241 Regs64bit ? X86::RBX : X86::EBX,
12242 swapInL, cpInH.getValue(1));
12243 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012244 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012245 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012246 SDValue Ops[] = { swapInH.getValue(0),
12247 N->getOperand(1),
12248 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012249 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012250 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012251 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12252 X86ISD::LCMPXCHG8_DAG;
12253 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012254 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012255 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12256 Regs64bit ? X86::RAX : X86::EAX,
12257 HalfT, Result.getValue(1));
12258 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12259 Regs64bit ? X86::RDX : X86::EDX,
12260 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012261 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012262 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012263 Results.push_back(cpOutH.getValue(1));
12264 return;
12265 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012266 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012267 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012268 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012269 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012270 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012271 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012272 case ISD::ATOMIC_LOAD_MAX:
12273 case ISD::ATOMIC_LOAD_MIN:
12274 case ISD::ATOMIC_LOAD_UMAX:
12275 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012276 case ISD::ATOMIC_SWAP: {
12277 unsigned Opc;
12278 switch (N->getOpcode()) {
12279 default: llvm_unreachable("Unexpected opcode");
12280 case ISD::ATOMIC_LOAD_ADD:
12281 Opc = X86ISD::ATOMADD64_DAG;
12282 break;
12283 case ISD::ATOMIC_LOAD_AND:
12284 Opc = X86ISD::ATOMAND64_DAG;
12285 break;
12286 case ISD::ATOMIC_LOAD_NAND:
12287 Opc = X86ISD::ATOMNAND64_DAG;
12288 break;
12289 case ISD::ATOMIC_LOAD_OR:
12290 Opc = X86ISD::ATOMOR64_DAG;
12291 break;
12292 case ISD::ATOMIC_LOAD_SUB:
12293 Opc = X86ISD::ATOMSUB64_DAG;
12294 break;
12295 case ISD::ATOMIC_LOAD_XOR:
12296 Opc = X86ISD::ATOMXOR64_DAG;
12297 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012298 case ISD::ATOMIC_LOAD_MAX:
12299 Opc = X86ISD::ATOMMAX64_DAG;
12300 break;
12301 case ISD::ATOMIC_LOAD_MIN:
12302 Opc = X86ISD::ATOMMIN64_DAG;
12303 break;
12304 case ISD::ATOMIC_LOAD_UMAX:
12305 Opc = X86ISD::ATOMUMAX64_DAG;
12306 break;
12307 case ISD::ATOMIC_LOAD_UMIN:
12308 Opc = X86ISD::ATOMUMIN64_DAG;
12309 break;
Craig Topperc0878702012-08-17 06:55:11 +000012310 case ISD::ATOMIC_SWAP:
12311 Opc = X86ISD::ATOMSWAP64_DAG;
12312 break;
12313 }
12314 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012315 return;
Craig Topperc0878702012-08-17 06:55:11 +000012316 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012317 case ISD::ATOMIC_LOAD:
12318 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012319 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012320}
12321
Evan Cheng72261582005-12-20 06:22:03 +000012322const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12323 switch (Opcode) {
12324 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012325 case X86ISD::BSF: return "X86ISD::BSF";
12326 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012327 case X86ISD::SHLD: return "X86ISD::SHLD";
12328 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012329 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012330 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012331 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012332 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012333 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012334 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012335 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12336 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12337 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012338 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012339 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012340 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012341 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012342 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012343 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012344 case X86ISD::COMI: return "X86ISD::COMI";
12345 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012346 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012347 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012348 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12349 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012350 case X86ISD::CMOV: return "X86ISD::CMOV";
12351 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012352 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012353 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12354 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012355 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012356 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012357 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012358 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012359 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012360 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12361 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012362 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012363 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012364 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012365 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012366 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012367 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012368 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012369 case X86ISD::HADD: return "X86ISD::HADD";
12370 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012371 case X86ISD::FHADD: return "X86ISD::FHADD";
12372 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012373 case X86ISD::UMAX: return "X86ISD::UMAX";
12374 case X86ISD::UMIN: return "X86ISD::UMIN";
12375 case X86ISD::SMAX: return "X86ISD::SMAX";
12376 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012377 case X86ISD::FMAX: return "X86ISD::FMAX";
12378 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012379 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12380 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012381 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12382 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012383 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012384 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012385 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012386 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12387 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012388 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012389 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012390 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012391 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012392 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12393 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012394 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12395 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12396 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12397 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12398 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12399 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012400 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012401 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012402 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012403 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12404 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012405 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012406 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012407 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12408 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012409 case X86ISD::VSHL: return "X86ISD::VSHL";
12410 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012411 case X86ISD::VSRA: return "X86ISD::VSRA";
12412 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12413 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12414 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012415 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012416 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12417 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012418 case X86ISD::ADD: return "X86ISD::ADD";
12419 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012420 case X86ISD::ADC: return "X86ISD::ADC";
12421 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012422 case X86ISD::SMUL: return "X86ISD::SMUL";
12423 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012424 case X86ISD::INC: return "X86ISD::INC";
12425 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012426 case X86ISD::OR: return "X86ISD::OR";
12427 case X86ISD::XOR: return "X86ISD::XOR";
12428 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012429 case X86ISD::BLSI: return "X86ISD::BLSI";
12430 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12431 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012432 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012433 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012434 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012435 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12436 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12437 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012438 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012439 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012440 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012441 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012442 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012443 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12444 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012445 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12446 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12447 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012448 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12449 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012450 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12451 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012452 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012453 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012454 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012455 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12456 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012457 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012458 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012459 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012460 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012461 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012462 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012463 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012464 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012465 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012466 case X86ISD::FMADD: return "X86ISD::FMADD";
12467 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12468 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12469 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12470 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12471 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012472 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12473 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012474 }
12475}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012476
Chris Lattnerc9addb72007-03-30 23:15:24 +000012477// isLegalAddressingMode - Return true if the addressing mode represented
12478// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012479bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012480 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012481 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012482 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012483 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012484
Chris Lattnerc9addb72007-03-30 23:15:24 +000012485 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012486 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012487 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012488
Chris Lattnerc9addb72007-03-30 23:15:24 +000012489 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012490 unsigned GVFlags =
12491 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012492
Chris Lattnerdfed4132009-07-10 07:38:24 +000012493 // If a reference to this global requires an extra load, we can't fold it.
12494 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012495 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012496
Chris Lattnerdfed4132009-07-10 07:38:24 +000012497 // If BaseGV requires a register for the PIC base, we cannot also have a
12498 // BaseReg specified.
12499 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012500 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012501
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012502 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012503 if ((M != CodeModel::Small || R != Reloc::Static) &&
12504 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012505 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012506 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012507
Chris Lattnerc9addb72007-03-30 23:15:24 +000012508 switch (AM.Scale) {
12509 case 0:
12510 case 1:
12511 case 2:
12512 case 4:
12513 case 8:
12514 // These scales always work.
12515 break;
12516 case 3:
12517 case 5:
12518 case 9:
12519 // These scales are formed with basereg+scalereg. Only accept if there is
12520 // no basereg yet.
12521 if (AM.HasBaseReg)
12522 return false;
12523 break;
12524 default: // Other stuff never works.
12525 return false;
12526 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012527
Chris Lattnerc9addb72007-03-30 23:15:24 +000012528 return true;
12529}
12530
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012531bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012532 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012533 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012534 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12535 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012536 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012537}
12538
Evan Cheng70e10d32012-07-17 06:53:39 +000012539bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012540 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012541}
12542
12543bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012544 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012545 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012546}
12547
Owen Andersone50ed302009-08-10 22:56:29 +000012548bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012549 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012550 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012551 unsigned NumBits1 = VT1.getSizeInBits();
12552 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012553 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012554}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012555
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012556bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012557 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012558 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012559}
12560
Owen Andersone50ed302009-08-10 22:56:29 +000012561bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012562 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012563 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012564}
12565
Evan Cheng2766a472012-12-06 19:13:27 +000012566bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12567 EVT VT1 = Val.getValueType();
12568 if (isZExtFree(VT1, VT2))
12569 return true;
12570
12571 if (Val.getOpcode() != ISD::LOAD)
12572 return false;
12573
12574 if (!VT1.isSimple() || !VT1.isInteger() ||
12575 !VT2.isSimple() || !VT2.isInteger())
12576 return false;
12577
12578 switch (VT1.getSimpleVT().SimpleTy) {
12579 default: break;
12580 case MVT::i8:
12581 case MVT::i16:
12582 case MVT::i32:
12583 // X86 has 8, 16, and 32-bit zero-extending loads.
12584 return true;
12585 }
12586
12587 return false;
12588}
12589
Owen Andersone50ed302009-08-10 22:56:29 +000012590bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012591 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012592 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012593}
12594
Evan Cheng60c07e12006-07-05 22:17:51 +000012595/// isShuffleMaskLegal - Targets can use this to indicate that they only
12596/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12597/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12598/// are assumed to be legal.
12599bool
Eric Christopherfd179292009-08-27 18:07:15 +000012600X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012601 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012602 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012603 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012604 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012605
Nate Begemana09008b2009-10-19 02:17:23 +000012606 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012607 return (VT.getVectorNumElements() == 2 ||
12608 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12609 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012610 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012611 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012612 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12613 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012614 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012615 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12616 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12617 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12618 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012619}
12620
Dan Gohman7d8143f2008-04-09 20:09:42 +000012621bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012622X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012623 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012624 unsigned NumElts = VT.getVectorNumElements();
12625 // FIXME: This collection of masks seems suspect.
12626 if (NumElts == 2)
12627 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012628 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012629 return (isMOVLMask(Mask, VT) ||
12630 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012631 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12632 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012633 }
12634 return false;
12635}
12636
12637//===----------------------------------------------------------------------===//
12638// X86 Scheduler Hooks
12639//===----------------------------------------------------------------------===//
12640
Michael Liaobe02a902012-11-08 07:28:54 +000012641/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012642static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12643 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012644 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012645
12646 const BasicBlock *BB = MBB->getBasicBlock();
12647 MachineFunction::iterator I = MBB;
12648 ++I;
12649
12650 // For the v = xbegin(), we generate
12651 //
12652 // thisMBB:
12653 // xbegin sinkMBB
12654 //
12655 // mainMBB:
12656 // eax = -1
12657 //
12658 // sinkMBB:
12659 // v = eax
12660
12661 MachineBasicBlock *thisMBB = MBB;
12662 MachineFunction *MF = MBB->getParent();
12663 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12664 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12665 MF->insert(I, mainMBB);
12666 MF->insert(I, sinkMBB);
12667
12668 // Transfer the remainder of BB and its successor edges to sinkMBB.
12669 sinkMBB->splice(sinkMBB->begin(), MBB,
12670 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12671 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12672
12673 // thisMBB:
12674 // xbegin sinkMBB
12675 // # fallthrough to mainMBB
12676 // # abortion to sinkMBB
12677 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12678 thisMBB->addSuccessor(mainMBB);
12679 thisMBB->addSuccessor(sinkMBB);
12680
12681 // mainMBB:
12682 // EAX = -1
12683 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12684 mainMBB->addSuccessor(sinkMBB);
12685
12686 // sinkMBB:
12687 // EAX is live into the sinkMBB
12688 sinkMBB->addLiveIn(X86::EAX);
12689 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12690 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12691 .addReg(X86::EAX);
12692
12693 MI->eraseFromParent();
12694 return sinkMBB;
12695}
12696
Michael Liaob118a072012-09-20 03:06:15 +000012697// Get CMPXCHG opcode for the specified data type.
12698static unsigned getCmpXChgOpcode(EVT VT) {
12699 switch (VT.getSimpleVT().SimpleTy) {
12700 case MVT::i8: return X86::LCMPXCHG8;
12701 case MVT::i16: return X86::LCMPXCHG16;
12702 case MVT::i32: return X86::LCMPXCHG32;
12703 case MVT::i64: return X86::LCMPXCHG64;
12704 default:
12705 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012706 }
Michael Liaob118a072012-09-20 03:06:15 +000012707 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012708}
12709
Michael Liaob118a072012-09-20 03:06:15 +000012710// Get LOAD opcode for the specified data type.
12711static unsigned getLoadOpcode(EVT VT) {
12712 switch (VT.getSimpleVT().SimpleTy) {
12713 case MVT::i8: return X86::MOV8rm;
12714 case MVT::i16: return X86::MOV16rm;
12715 case MVT::i32: return X86::MOV32rm;
12716 case MVT::i64: return X86::MOV64rm;
12717 default:
12718 break;
12719 }
12720 llvm_unreachable("Invalid operand size!");
12721}
12722
12723// Get opcode of the non-atomic one from the specified atomic instruction.
12724static unsigned getNonAtomicOpcode(unsigned Opc) {
12725 switch (Opc) {
12726 case X86::ATOMAND8: return X86::AND8rr;
12727 case X86::ATOMAND16: return X86::AND16rr;
12728 case X86::ATOMAND32: return X86::AND32rr;
12729 case X86::ATOMAND64: return X86::AND64rr;
12730 case X86::ATOMOR8: return X86::OR8rr;
12731 case X86::ATOMOR16: return X86::OR16rr;
12732 case X86::ATOMOR32: return X86::OR32rr;
12733 case X86::ATOMOR64: return X86::OR64rr;
12734 case X86::ATOMXOR8: return X86::XOR8rr;
12735 case X86::ATOMXOR16: return X86::XOR16rr;
12736 case X86::ATOMXOR32: return X86::XOR32rr;
12737 case X86::ATOMXOR64: return X86::XOR64rr;
12738 }
12739 llvm_unreachable("Unhandled atomic-load-op opcode!");
12740}
12741
12742// Get opcode of the non-atomic one from the specified atomic instruction with
12743// extra opcode.
12744static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12745 unsigned &ExtraOpc) {
12746 switch (Opc) {
12747 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12748 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12749 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12750 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012751 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012752 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12753 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12754 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012755 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012756 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12757 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12758 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012759 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012760 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12761 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12762 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012763 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012764 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12765 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12766 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12767 }
12768 llvm_unreachable("Unhandled atomic-load-op opcode!");
12769}
12770
12771// Get opcode of the non-atomic one from the specified atomic instruction for
12772// 64-bit data type on 32-bit target.
12773static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12774 switch (Opc) {
12775 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12776 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12777 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12778 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12779 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12780 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012781 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12782 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12783 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12784 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012785 }
12786 llvm_unreachable("Unhandled atomic-load-op opcode!");
12787}
12788
12789// Get opcode of the non-atomic one from the specified atomic instruction for
12790// 64-bit data type on 32-bit target with extra opcode.
12791static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12792 unsigned &HiOpc,
12793 unsigned &ExtraOpc) {
12794 switch (Opc) {
12795 case X86::ATOMNAND6432:
12796 ExtraOpc = X86::NOT32r;
12797 HiOpc = X86::AND32rr;
12798 return X86::AND32rr;
12799 }
12800 llvm_unreachable("Unhandled atomic-load-op opcode!");
12801}
12802
12803// Get pseudo CMOV opcode from the specified data type.
12804static unsigned getPseudoCMOVOpc(EVT VT) {
12805 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012806 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012807 case MVT::i16: return X86::CMOV_GR16;
12808 case MVT::i32: return X86::CMOV_GR32;
12809 default:
12810 break;
12811 }
12812 llvm_unreachable("Unknown CMOV opcode!");
12813}
12814
12815// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12816// They will be translated into a spin-loop or compare-exchange loop from
12817//
12818// ...
12819// dst = atomic-fetch-op MI.addr, MI.val
12820// ...
12821//
12822// to
12823//
12824// ...
12825// EAX = LOAD MI.addr
12826// loop:
12827// t1 = OP MI.val, EAX
12828// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12829// JNE loop
12830// sink:
12831// dst = EAX
12832// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012833MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012834X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12835 MachineBasicBlock *MBB) const {
12836 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12837 DebugLoc DL = MI->getDebugLoc();
12838
12839 MachineFunction *MF = MBB->getParent();
12840 MachineRegisterInfo &MRI = MF->getRegInfo();
12841
12842 const BasicBlock *BB = MBB->getBasicBlock();
12843 MachineFunction::iterator I = MBB;
12844 ++I;
12845
Michael Liao13d08bf2013-01-22 21:47:38 +000012846 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000012847 "Unexpected number of operands");
12848
12849 assert(MI->hasOneMemOperand() &&
12850 "Expected atomic-load-op to have one memoperand");
12851
12852 // Memory Reference
12853 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12854 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12855
12856 unsigned DstReg, SrcReg;
12857 unsigned MemOpndSlot;
12858
12859 unsigned CurOp = 0;
12860
12861 DstReg = MI->getOperand(CurOp++).getReg();
12862 MemOpndSlot = CurOp;
12863 CurOp += X86::AddrNumOperands;
12864 SrcReg = MI->getOperand(CurOp++).getReg();
12865
12866 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012867 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012868 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12869
12870 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12871 unsigned LOADOpc = getLoadOpcode(VT);
12872
12873 // For the atomic load-arith operator, we generate
12874 //
12875 // thisMBB:
12876 // EAX = LOAD [MI.addr]
12877 // mainMBB:
12878 // t1 = OP MI.val, EAX
12879 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12880 // JNE mainMBB
12881 // sinkMBB:
12882
12883 MachineBasicBlock *thisMBB = MBB;
12884 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12885 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12886 MF->insert(I, mainMBB);
12887 MF->insert(I, sinkMBB);
12888
12889 MachineInstrBuilder MIB;
12890
12891 // Transfer the remainder of BB and its successor edges to sinkMBB.
12892 sinkMBB->splice(sinkMBB->begin(), MBB,
12893 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12894 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12895
12896 // thisMBB:
12897 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12898 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12899 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12900 MIB.setMemRefs(MMOBegin, MMOEnd);
12901
12902 thisMBB->addSuccessor(mainMBB);
12903
12904 // mainMBB:
12905 MachineBasicBlock *origMainMBB = mainMBB;
12906 mainMBB->addLiveIn(AccPhyReg);
12907
12908 // Copy AccPhyReg as it is used more than once.
12909 unsigned AccReg = MRI.createVirtualRegister(RC);
12910 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12911 .addReg(AccPhyReg);
12912
12913 unsigned t1 = MRI.createVirtualRegister(RC);
12914 unsigned Opc = MI->getOpcode();
12915 switch (Opc) {
12916 default:
12917 llvm_unreachable("Unhandled atomic-load-op opcode!");
12918 case X86::ATOMAND8:
12919 case X86::ATOMAND16:
12920 case X86::ATOMAND32:
12921 case X86::ATOMAND64:
12922 case X86::ATOMOR8:
12923 case X86::ATOMOR16:
12924 case X86::ATOMOR32:
12925 case X86::ATOMOR64:
12926 case X86::ATOMXOR8:
12927 case X86::ATOMXOR16:
12928 case X86::ATOMXOR32:
12929 case X86::ATOMXOR64: {
12930 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12931 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12932 .addReg(AccReg);
12933 break;
12934 }
12935 case X86::ATOMNAND8:
12936 case X86::ATOMNAND16:
12937 case X86::ATOMNAND32:
12938 case X86::ATOMNAND64: {
12939 unsigned t2 = MRI.createVirtualRegister(RC);
12940 unsigned NOTOpc;
12941 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12942 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12943 .addReg(AccReg);
12944 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12945 break;
12946 }
Michael Liao08382492012-09-21 03:00:17 +000012947 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012948 case X86::ATOMMAX16:
12949 case X86::ATOMMAX32:
12950 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012951 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012952 case X86::ATOMMIN16:
12953 case X86::ATOMMIN32:
12954 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012955 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012956 case X86::ATOMUMAX16:
12957 case X86::ATOMUMAX32:
12958 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012959 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012960 case X86::ATOMUMIN16:
12961 case X86::ATOMUMIN32:
12962 case X86::ATOMUMIN64: {
12963 unsigned CMPOpc;
12964 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12965
12966 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12967 .addReg(SrcReg)
12968 .addReg(AccReg);
12969
12970 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012971 if (VT != MVT::i8) {
12972 // Native support
12973 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12974 .addReg(SrcReg)
12975 .addReg(AccReg);
12976 } else {
12977 // Promote i8 to i32 to use CMOV32
12978 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12979 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12980 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12981 unsigned t2 = MRI.createVirtualRegister(RC32);
12982
12983 unsigned Undef = MRI.createVirtualRegister(RC32);
12984 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12985
12986 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12987 .addReg(Undef)
12988 .addReg(SrcReg)
12989 .addImm(X86::sub_8bit);
12990 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12991 .addReg(Undef)
12992 .addReg(AccReg)
12993 .addImm(X86::sub_8bit);
12994
12995 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12996 .addReg(SrcReg32)
12997 .addReg(AccReg32);
12998
12999 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
13000 .addReg(t2, 0, X86::sub_8bit);
13001 }
Michael Liaob118a072012-09-20 03:06:15 +000013002 } else {
13003 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013004 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013005 "Invalid atomic-load-op transformation!");
13006 unsigned SelOpc = getPseudoCMOVOpc(VT);
13007 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13008 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
13009 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
13010 .addReg(SrcReg).addReg(AccReg)
13011 .addImm(CC);
13012 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13013 }
13014 break;
13015 }
13016 }
13017
13018 // Copy AccPhyReg back from virtual register.
13019 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
13020 .addReg(AccReg);
13021
13022 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13023 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13024 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13025 MIB.addReg(t1);
13026 MIB.setMemRefs(MMOBegin, MMOEnd);
13027
13028 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13029
13030 mainMBB->addSuccessor(origMainMBB);
13031 mainMBB->addSuccessor(sinkMBB);
13032
13033 // sinkMBB:
13034 sinkMBB->addLiveIn(AccPhyReg);
13035
13036 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13037 TII->get(TargetOpcode::COPY), DstReg)
13038 .addReg(AccPhyReg);
13039
13040 MI->eraseFromParent();
13041 return sinkMBB;
13042}
13043
13044// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13045// instructions. They will be translated into a spin-loop or compare-exchange
13046// loop from
13047//
13048// ...
13049// dst = atomic-fetch-op MI.addr, MI.val
13050// ...
13051//
13052// to
13053//
13054// ...
13055// EAX = LOAD [MI.addr + 0]
13056// EDX = LOAD [MI.addr + 4]
13057// loop:
13058// EBX = OP MI.val.lo, EAX
13059// ECX = OP MI.val.hi, EDX
13060// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13061// JNE loop
13062// sink:
13063// dst = EDX:EAX
13064// ...
13065MachineBasicBlock *
13066X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13067 MachineBasicBlock *MBB) const {
13068 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13069 DebugLoc DL = MI->getDebugLoc();
13070
13071 MachineFunction *MF = MBB->getParent();
13072 MachineRegisterInfo &MRI = MF->getRegInfo();
13073
13074 const BasicBlock *BB = MBB->getBasicBlock();
13075 MachineFunction::iterator I = MBB;
13076 ++I;
13077
Michael Liao13d08bf2013-01-22 21:47:38 +000013078 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013079 "Unexpected number of operands");
13080
13081 assert(MI->hasOneMemOperand() &&
13082 "Expected atomic-load-op32 to have one memoperand");
13083
13084 // Memory Reference
13085 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13086 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13087
13088 unsigned DstLoReg, DstHiReg;
13089 unsigned SrcLoReg, SrcHiReg;
13090 unsigned MemOpndSlot;
13091
13092 unsigned CurOp = 0;
13093
13094 DstLoReg = MI->getOperand(CurOp++).getReg();
13095 DstHiReg = MI->getOperand(CurOp++).getReg();
13096 MemOpndSlot = CurOp;
13097 CurOp += X86::AddrNumOperands;
13098 SrcLoReg = MI->getOperand(CurOp++).getReg();
13099 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013100
Craig Topperc9099502012-04-20 06:31:50 +000013101 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013102 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013103
Michael Liaob118a072012-09-20 03:06:15 +000013104 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13105 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013106
Michael Liaob118a072012-09-20 03:06:15 +000013107 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013108 //
Michael Liaob118a072012-09-20 03:06:15 +000013109 // thisMBB:
13110 // EAX = LOAD [MI.addr + 0]
13111 // EDX = LOAD [MI.addr + 4]
13112 // mainMBB:
13113 // EBX = OP MI.vallo, EAX
13114 // ECX = OP MI.valhi, EDX
13115 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13116 // JNE mainMBB
13117 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013118
Mon P Wang63307c32008-05-05 19:05:59 +000013119 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013120 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13121 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13122 MF->insert(I, mainMBB);
13123 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013124
Michael Liaob118a072012-09-20 03:06:15 +000013125 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013126
Michael Liaob118a072012-09-20 03:06:15 +000013127 // Transfer the remainder of BB and its successor edges to sinkMBB.
13128 sinkMBB->splice(sinkMBB->begin(), MBB,
13129 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13130 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013131
Michael Liaob118a072012-09-20 03:06:15 +000013132 // thisMBB:
13133 // Lo
13134 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13135 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13136 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13137 MIB.setMemRefs(MMOBegin, MMOEnd);
13138 // Hi
13139 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13140 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013141 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013142 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013143 else
Michael Liaob118a072012-09-20 03:06:15 +000013144 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13145 }
13146 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013147
Michael Liaob118a072012-09-20 03:06:15 +000013148 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013149
Michael Liaob118a072012-09-20 03:06:15 +000013150 // mainMBB:
13151 MachineBasicBlock *origMainMBB = mainMBB;
13152 mainMBB->addLiveIn(X86::EAX);
13153 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013154
Michael Liaob118a072012-09-20 03:06:15 +000013155 // Copy EDX:EAX as they are used more than once.
13156 unsigned LoReg = MRI.createVirtualRegister(RC);
13157 unsigned HiReg = MRI.createVirtualRegister(RC);
13158 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13159 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013160
Michael Liaob118a072012-09-20 03:06:15 +000013161 unsigned t1L = MRI.createVirtualRegister(RC);
13162 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013163
Michael Liaob118a072012-09-20 03:06:15 +000013164 unsigned Opc = MI->getOpcode();
13165 switch (Opc) {
13166 default:
13167 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13168 case X86::ATOMAND6432:
13169 case X86::ATOMOR6432:
13170 case X86::ATOMXOR6432:
13171 case X86::ATOMADD6432:
13172 case X86::ATOMSUB6432: {
13173 unsigned HiOpc;
13174 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013175 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13176 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013177 break;
13178 }
13179 case X86::ATOMNAND6432: {
13180 unsigned HiOpc, NOTOpc;
13181 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13182 unsigned t2L = MRI.createVirtualRegister(RC);
13183 unsigned t2H = MRI.createVirtualRegister(RC);
13184 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13185 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13186 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13187 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13188 break;
13189 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013190 case X86::ATOMMAX6432:
13191 case X86::ATOMMIN6432:
13192 case X86::ATOMUMAX6432:
13193 case X86::ATOMUMIN6432: {
13194 unsigned HiOpc;
13195 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13196 unsigned cL = MRI.createVirtualRegister(RC8);
13197 unsigned cH = MRI.createVirtualRegister(RC8);
13198 unsigned cL32 = MRI.createVirtualRegister(RC);
13199 unsigned cH32 = MRI.createVirtualRegister(RC);
13200 unsigned cc = MRI.createVirtualRegister(RC);
13201 // cl := cmp src_lo, lo
13202 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13203 .addReg(SrcLoReg).addReg(LoReg);
13204 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13205 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13206 // ch := cmp src_hi, hi
13207 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13208 .addReg(SrcHiReg).addReg(HiReg);
13209 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13210 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13211 // cc := if (src_hi == hi) ? cl : ch;
13212 if (Subtarget->hasCMov()) {
13213 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13214 .addReg(cH32).addReg(cL32);
13215 } else {
13216 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13217 .addReg(cH32).addReg(cL32)
13218 .addImm(X86::COND_E);
13219 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13220 }
13221 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13222 if (Subtarget->hasCMov()) {
13223 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13224 .addReg(SrcLoReg).addReg(LoReg);
13225 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13226 .addReg(SrcHiReg).addReg(HiReg);
13227 } else {
13228 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13229 .addReg(SrcLoReg).addReg(LoReg)
13230 .addImm(X86::COND_NE);
13231 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13232 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13233 .addReg(SrcHiReg).addReg(HiReg)
13234 .addImm(X86::COND_NE);
13235 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13236 }
13237 break;
13238 }
Michael Liaob118a072012-09-20 03:06:15 +000013239 case X86::ATOMSWAP6432: {
13240 unsigned HiOpc;
13241 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13242 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13243 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13244 break;
13245 }
13246 }
Mon P Wang63307c32008-05-05 19:05:59 +000013247
Michael Liaob118a072012-09-20 03:06:15 +000013248 // Copy EDX:EAX back from HiReg:LoReg
13249 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13250 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13251 // Copy ECX:EBX from t1H:t1L
13252 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13253 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013254
Michael Liaob118a072012-09-20 03:06:15 +000013255 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13256 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13257 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13258 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013259
Michael Liaob118a072012-09-20 03:06:15 +000013260 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013261
Michael Liaob118a072012-09-20 03:06:15 +000013262 mainMBB->addSuccessor(origMainMBB);
13263 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013264
Michael Liaob118a072012-09-20 03:06:15 +000013265 // sinkMBB:
13266 sinkMBB->addLiveIn(X86::EAX);
13267 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013268
Michael Liaob118a072012-09-20 03:06:15 +000013269 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13270 TII->get(TargetOpcode::COPY), DstLoReg)
13271 .addReg(X86::EAX);
13272 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13273 TII->get(TargetOpcode::COPY), DstHiReg)
13274 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013275
Michael Liaob118a072012-09-20 03:06:15 +000013276 MI->eraseFromParent();
13277 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013278}
13279
Eric Christopherf83a5de2009-08-27 18:08:16 +000013280// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013281// or XMM0_V32I8 in AVX all of this code can be replaced with that
13282// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013283static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13284 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013285 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013286 switch (MI->getOpcode()) {
13287 default: llvm_unreachable("illegal opcode!");
13288 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13289 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13290 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13291 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13292 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13293 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13294 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13295 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013296 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013297
Craig Topper8aae8dd2012-11-10 08:57:41 +000013298 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013299 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013300
Craig Topper52ea2452012-11-10 09:25:36 +000013301 unsigned NumArgs = MI->getNumOperands();
13302 for (unsigned i = 1; i < NumArgs; ++i) {
13303 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013304 if (!(Op.isReg() && Op.isImplicit()))
13305 MIB.addOperand(Op);
13306 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013307 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013308 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13309
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013310 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013311 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013312 .addReg(X86::XMM0);
13313
Dan Gohman14152b42010-07-06 20:24:04 +000013314 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013315 return BB;
13316}
13317
Craig Topper9c7ae012012-11-10 01:23:36 +000013318// FIXME: Custom handling because TableGen doesn't support multiple implicit
13319// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013320static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13321 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013322 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013323 switch (MI->getOpcode()) {
13324 default: llvm_unreachable("illegal opcode!");
13325 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13326 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13327 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13328 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13329 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13330 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13331 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13332 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013333 }
13334
Craig Topper8aae8dd2012-11-10 08:57:41 +000013335 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013336 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013337
Craig Topper52ea2452012-11-10 09:25:36 +000013338 unsigned NumArgs = MI->getNumOperands(); // remove the results
13339 for (unsigned i = 1; i < NumArgs; ++i) {
13340 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013341 if (!(Op.isReg() && Op.isImplicit()))
13342 MIB.addOperand(Op);
13343 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013344 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013345 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13346
13347 BuildMI(*BB, MI, dl,
13348 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13349 .addReg(X86::ECX);
13350
13351 MI->eraseFromParent();
13352 return BB;
13353}
13354
Craig Topper2da36912012-11-11 22:45:02 +000013355static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13356 const TargetInstrInfo *TII,
13357 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013358 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013359
Eric Christopher228232b2010-11-30 07:20:12 +000013360 // Address into RAX/EAX, other two args into ECX, EDX.
13361 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13362 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13363 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13364 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013365 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013366
Eric Christopher228232b2010-11-30 07:20:12 +000013367 unsigned ValOps = X86::AddrNumOperands;
13368 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13369 .addReg(MI->getOperand(ValOps).getReg());
13370 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13371 .addReg(MI->getOperand(ValOps+1).getReg());
13372
13373 // The instruction doesn't actually take any operands though.
13374 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013375
Eric Christopher228232b2010-11-30 07:20:12 +000013376 MI->eraseFromParent(); // The pseudo is gone now.
13377 return BB;
13378}
13379
13380MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013381X86TargetLowering::EmitVAARG64WithCustomInserter(
13382 MachineInstr *MI,
13383 MachineBasicBlock *MBB) const {
13384 // Emit va_arg instruction on X86-64.
13385
13386 // Operands to this pseudo-instruction:
13387 // 0 ) Output : destination address (reg)
13388 // 1-5) Input : va_list address (addr, i64mem)
13389 // 6 ) ArgSize : Size (in bytes) of vararg type
13390 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13391 // 8 ) Align : Alignment of type
13392 // 9 ) EFLAGS (implicit-def)
13393
13394 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13395 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13396
13397 unsigned DestReg = MI->getOperand(0).getReg();
13398 MachineOperand &Base = MI->getOperand(1);
13399 MachineOperand &Scale = MI->getOperand(2);
13400 MachineOperand &Index = MI->getOperand(3);
13401 MachineOperand &Disp = MI->getOperand(4);
13402 MachineOperand &Segment = MI->getOperand(5);
13403 unsigned ArgSize = MI->getOperand(6).getImm();
13404 unsigned ArgMode = MI->getOperand(7).getImm();
13405 unsigned Align = MI->getOperand(8).getImm();
13406
13407 // Memory Reference
13408 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13409 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13410 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13411
13412 // Machine Information
13413 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13414 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13415 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13416 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13417 DebugLoc DL = MI->getDebugLoc();
13418
13419 // struct va_list {
13420 // i32 gp_offset
13421 // i32 fp_offset
13422 // i64 overflow_area (address)
13423 // i64 reg_save_area (address)
13424 // }
13425 // sizeof(va_list) = 24
13426 // alignment(va_list) = 8
13427
13428 unsigned TotalNumIntRegs = 6;
13429 unsigned TotalNumXMMRegs = 8;
13430 bool UseGPOffset = (ArgMode == 1);
13431 bool UseFPOffset = (ArgMode == 2);
13432 unsigned MaxOffset = TotalNumIntRegs * 8 +
13433 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13434
13435 /* Align ArgSize to a multiple of 8 */
13436 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13437 bool NeedsAlign = (Align > 8);
13438
13439 MachineBasicBlock *thisMBB = MBB;
13440 MachineBasicBlock *overflowMBB;
13441 MachineBasicBlock *offsetMBB;
13442 MachineBasicBlock *endMBB;
13443
13444 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13445 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13446 unsigned OffsetReg = 0;
13447
13448 if (!UseGPOffset && !UseFPOffset) {
13449 // If we only pull from the overflow region, we don't create a branch.
13450 // We don't need to alter control flow.
13451 OffsetDestReg = 0; // unused
13452 OverflowDestReg = DestReg;
13453
13454 offsetMBB = NULL;
13455 overflowMBB = thisMBB;
13456 endMBB = thisMBB;
13457 } else {
13458 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13459 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13460 // If not, pull from overflow_area. (branch to overflowMBB)
13461 //
13462 // thisMBB
13463 // | .
13464 // | .
13465 // offsetMBB overflowMBB
13466 // | .
13467 // | .
13468 // endMBB
13469
13470 // Registers for the PHI in endMBB
13471 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13472 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13473
13474 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13475 MachineFunction *MF = MBB->getParent();
13476 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13477 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13478 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13479
13480 MachineFunction::iterator MBBIter = MBB;
13481 ++MBBIter;
13482
13483 // Insert the new basic blocks
13484 MF->insert(MBBIter, offsetMBB);
13485 MF->insert(MBBIter, overflowMBB);
13486 MF->insert(MBBIter, endMBB);
13487
13488 // Transfer the remainder of MBB and its successor edges to endMBB.
13489 endMBB->splice(endMBB->begin(), thisMBB,
13490 llvm::next(MachineBasicBlock::iterator(MI)),
13491 thisMBB->end());
13492 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13493
13494 // Make offsetMBB and overflowMBB successors of thisMBB
13495 thisMBB->addSuccessor(offsetMBB);
13496 thisMBB->addSuccessor(overflowMBB);
13497
13498 // endMBB is a successor of both offsetMBB and overflowMBB
13499 offsetMBB->addSuccessor(endMBB);
13500 overflowMBB->addSuccessor(endMBB);
13501
13502 // Load the offset value into a register
13503 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13504 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13505 .addOperand(Base)
13506 .addOperand(Scale)
13507 .addOperand(Index)
13508 .addDisp(Disp, UseFPOffset ? 4 : 0)
13509 .addOperand(Segment)
13510 .setMemRefs(MMOBegin, MMOEnd);
13511
13512 // Check if there is enough room left to pull this argument.
13513 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13514 .addReg(OffsetReg)
13515 .addImm(MaxOffset + 8 - ArgSizeA8);
13516
13517 // Branch to "overflowMBB" if offset >= max
13518 // Fall through to "offsetMBB" otherwise
13519 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13520 .addMBB(overflowMBB);
13521 }
13522
13523 // In offsetMBB, emit code to use the reg_save_area.
13524 if (offsetMBB) {
13525 assert(OffsetReg != 0);
13526
13527 // Read the reg_save_area address.
13528 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13529 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13530 .addOperand(Base)
13531 .addOperand(Scale)
13532 .addOperand(Index)
13533 .addDisp(Disp, 16)
13534 .addOperand(Segment)
13535 .setMemRefs(MMOBegin, MMOEnd);
13536
13537 // Zero-extend the offset
13538 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13539 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13540 .addImm(0)
13541 .addReg(OffsetReg)
13542 .addImm(X86::sub_32bit);
13543
13544 // Add the offset to the reg_save_area to get the final address.
13545 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13546 .addReg(OffsetReg64)
13547 .addReg(RegSaveReg);
13548
13549 // Compute the offset for the next argument
13550 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13551 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13552 .addReg(OffsetReg)
13553 .addImm(UseFPOffset ? 16 : 8);
13554
13555 // Store it back into the va_list.
13556 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13557 .addOperand(Base)
13558 .addOperand(Scale)
13559 .addOperand(Index)
13560 .addDisp(Disp, UseFPOffset ? 4 : 0)
13561 .addOperand(Segment)
13562 .addReg(NextOffsetReg)
13563 .setMemRefs(MMOBegin, MMOEnd);
13564
13565 // Jump to endMBB
13566 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13567 .addMBB(endMBB);
13568 }
13569
13570 //
13571 // Emit code to use overflow area
13572 //
13573
13574 // Load the overflow_area address into a register.
13575 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13576 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13577 .addOperand(Base)
13578 .addOperand(Scale)
13579 .addOperand(Index)
13580 .addDisp(Disp, 8)
13581 .addOperand(Segment)
13582 .setMemRefs(MMOBegin, MMOEnd);
13583
13584 // If we need to align it, do so. Otherwise, just copy the address
13585 // to OverflowDestReg.
13586 if (NeedsAlign) {
13587 // Align the overflow address
13588 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13589 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13590
13591 // aligned_addr = (addr + (align-1)) & ~(align-1)
13592 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13593 .addReg(OverflowAddrReg)
13594 .addImm(Align-1);
13595
13596 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13597 .addReg(TmpReg)
13598 .addImm(~(uint64_t)(Align-1));
13599 } else {
13600 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13601 .addReg(OverflowAddrReg);
13602 }
13603
13604 // Compute the next overflow address after this argument.
13605 // (the overflow address should be kept 8-byte aligned)
13606 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13607 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13608 .addReg(OverflowDestReg)
13609 .addImm(ArgSizeA8);
13610
13611 // Store the new overflow address.
13612 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13613 .addOperand(Base)
13614 .addOperand(Scale)
13615 .addOperand(Index)
13616 .addDisp(Disp, 8)
13617 .addOperand(Segment)
13618 .addReg(NextAddrReg)
13619 .setMemRefs(MMOBegin, MMOEnd);
13620
13621 // If we branched, emit the PHI to the front of endMBB.
13622 if (offsetMBB) {
13623 BuildMI(*endMBB, endMBB->begin(), DL,
13624 TII->get(X86::PHI), DestReg)
13625 .addReg(OffsetDestReg).addMBB(offsetMBB)
13626 .addReg(OverflowDestReg).addMBB(overflowMBB);
13627 }
13628
13629 // Erase the pseudo instruction
13630 MI->eraseFromParent();
13631
13632 return endMBB;
13633}
13634
13635MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013636X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13637 MachineInstr *MI,
13638 MachineBasicBlock *MBB) const {
13639 // Emit code to save XMM registers to the stack. The ABI says that the
13640 // number of registers to save is given in %al, so it's theoretically
13641 // possible to do an indirect jump trick to avoid saving all of them,
13642 // however this code takes a simpler approach and just executes all
13643 // of the stores if %al is non-zero. It's less code, and it's probably
13644 // easier on the hardware branch predictor, and stores aren't all that
13645 // expensive anyway.
13646
13647 // Create the new basic blocks. One block contains all the XMM stores,
13648 // and one block is the final destination regardless of whether any
13649 // stores were performed.
13650 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13651 MachineFunction *F = MBB->getParent();
13652 MachineFunction::iterator MBBIter = MBB;
13653 ++MBBIter;
13654 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13655 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13656 F->insert(MBBIter, XMMSaveMBB);
13657 F->insert(MBBIter, EndMBB);
13658
Dan Gohman14152b42010-07-06 20:24:04 +000013659 // Transfer the remainder of MBB and its successor edges to EndMBB.
13660 EndMBB->splice(EndMBB->begin(), MBB,
13661 llvm::next(MachineBasicBlock::iterator(MI)),
13662 MBB->end());
13663 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13664
Dan Gohmand6708ea2009-08-15 01:38:56 +000013665 // The original block will now fall through to the XMM save block.
13666 MBB->addSuccessor(XMMSaveMBB);
13667 // The XMMSaveMBB will fall through to the end block.
13668 XMMSaveMBB->addSuccessor(EndMBB);
13669
13670 // Now add the instructions.
13671 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13672 DebugLoc DL = MI->getDebugLoc();
13673
13674 unsigned CountReg = MI->getOperand(0).getReg();
13675 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13676 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13677
13678 if (!Subtarget->isTargetWin64()) {
13679 // If %al is 0, branch around the XMM save block.
13680 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013681 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013682 MBB->addSuccessor(EndMBB);
13683 }
13684
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013685 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013686 // In the XMM save block, save all the XMM argument registers.
13687 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13688 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013689 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013690 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013691 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013692 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013693 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013694 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013695 .addFrameIndex(RegSaveFrameIndex)
13696 .addImm(/*Scale=*/1)
13697 .addReg(/*IndexReg=*/0)
13698 .addImm(/*Disp=*/Offset)
13699 .addReg(/*Segment=*/0)
13700 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013701 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013702 }
13703
Dan Gohman14152b42010-07-06 20:24:04 +000013704 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013705
13706 return EndMBB;
13707}
Mon P Wang63307c32008-05-05 19:05:59 +000013708
Lang Hames6e3f7e42012-02-03 01:13:49 +000013709// The EFLAGS operand of SelectItr might be missing a kill marker
13710// because there were multiple uses of EFLAGS, and ISel didn't know
13711// which to mark. Figure out whether SelectItr should have had a
13712// kill marker, and set it if it should. Returns the correct kill
13713// marker value.
13714static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13715 MachineBasicBlock* BB,
13716 const TargetRegisterInfo* TRI) {
13717 // Scan forward through BB for a use/def of EFLAGS.
13718 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13719 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013720 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013721 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013722 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013723 if (mi.definesRegister(X86::EFLAGS))
13724 break; // Should have kill-flag - update below.
13725 }
13726
13727 // If we hit the end of the block, check whether EFLAGS is live into a
13728 // successor.
13729 if (miI == BB->end()) {
13730 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13731 sEnd = BB->succ_end();
13732 sItr != sEnd; ++sItr) {
13733 MachineBasicBlock* succ = *sItr;
13734 if (succ->isLiveIn(X86::EFLAGS))
13735 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013736 }
13737 }
13738
Lang Hames6e3f7e42012-02-03 01:13:49 +000013739 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13740 // out. SelectMI should have a kill flag on EFLAGS.
13741 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013742 return true;
13743}
13744
Evan Cheng60c07e12006-07-05 22:17:51 +000013745MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013746X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013747 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013748 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13749 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013750
Chris Lattner52600972009-09-02 05:57:00 +000013751 // To "insert" a SELECT_CC instruction, we actually have to insert the
13752 // diamond control-flow pattern. The incoming instruction knows the
13753 // destination vreg to set, the condition code register to branch on, the
13754 // true/false values to select between, and a branch opcode to use.
13755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13756 MachineFunction::iterator It = BB;
13757 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013758
Chris Lattner52600972009-09-02 05:57:00 +000013759 // thisMBB:
13760 // ...
13761 // TrueVal = ...
13762 // cmpTY ccX, r1, r2
13763 // bCC copy1MBB
13764 // fallthrough --> copy0MBB
13765 MachineBasicBlock *thisMBB = BB;
13766 MachineFunction *F = BB->getParent();
13767 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13768 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013769 F->insert(It, copy0MBB);
13770 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013771
Bill Wendling730c07e2010-06-25 20:48:10 +000013772 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13773 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013774 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13775 if (!MI->killsRegister(X86::EFLAGS) &&
13776 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13777 copy0MBB->addLiveIn(X86::EFLAGS);
13778 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013779 }
13780
Dan Gohman14152b42010-07-06 20:24:04 +000013781 // Transfer the remainder of BB and its successor edges to sinkMBB.
13782 sinkMBB->splice(sinkMBB->begin(), BB,
13783 llvm::next(MachineBasicBlock::iterator(MI)),
13784 BB->end());
13785 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13786
13787 // Add the true and fallthrough blocks as its successors.
13788 BB->addSuccessor(copy0MBB);
13789 BB->addSuccessor(sinkMBB);
13790
13791 // Create the conditional branch instruction.
13792 unsigned Opc =
13793 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13794 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13795
Chris Lattner52600972009-09-02 05:57:00 +000013796 // copy0MBB:
13797 // %FalseValue = ...
13798 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013799 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013800
Chris Lattner52600972009-09-02 05:57:00 +000013801 // sinkMBB:
13802 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13803 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013804 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13805 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013806 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13807 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13808
Dan Gohman14152b42010-07-06 20:24:04 +000013809 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013810 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013811}
13812
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013813MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013814X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13815 bool Is64Bit) const {
13816 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13817 DebugLoc DL = MI->getDebugLoc();
13818 MachineFunction *MF = BB->getParent();
13819 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13820
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013821 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013822
13823 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13824 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13825
13826 // BB:
13827 // ... [Till the alloca]
13828 // If stacklet is not large enough, jump to mallocMBB
13829 //
13830 // bumpMBB:
13831 // Allocate by subtracting from RSP
13832 // Jump to continueMBB
13833 //
13834 // mallocMBB:
13835 // Allocate by call to runtime
13836 //
13837 // continueMBB:
13838 // ...
13839 // [rest of original BB]
13840 //
13841
13842 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13843 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13844 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13845
13846 MachineRegisterInfo &MRI = MF->getRegInfo();
13847 const TargetRegisterClass *AddrRegClass =
13848 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13849
13850 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13851 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13852 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013853 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013854 sizeVReg = MI->getOperand(1).getReg(),
13855 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13856
13857 MachineFunction::iterator MBBIter = BB;
13858 ++MBBIter;
13859
13860 MF->insert(MBBIter, bumpMBB);
13861 MF->insert(MBBIter, mallocMBB);
13862 MF->insert(MBBIter, continueMBB);
13863
13864 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13865 (MachineBasicBlock::iterator(MI)), BB->end());
13866 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13867
13868 // Add code to the main basic block to check if the stack limit has been hit,
13869 // and if so, jump to mallocMBB otherwise to bumpMBB.
13870 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013871 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013872 .addReg(tmpSPVReg).addReg(sizeVReg);
13873 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013874 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013875 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013876 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13877
13878 // bumpMBB simply decreases the stack pointer, since we know the current
13879 // stacklet has enough space.
13880 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013881 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013882 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013883 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013884 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13885
13886 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013887 const uint32_t *RegMask =
13888 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013889 if (Is64Bit) {
13890 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13891 .addReg(sizeVReg);
13892 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013893 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013894 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013895 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013896 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013897 } else {
13898 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13899 .addImm(12);
13900 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13901 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013902 .addExternalSymbol("__morestack_allocate_stack_space")
13903 .addRegMask(RegMask)
13904 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013905 }
13906
13907 if (!Is64Bit)
13908 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13909 .addImm(16);
13910
13911 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13912 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13913 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13914
13915 // Set up the CFG correctly.
13916 BB->addSuccessor(bumpMBB);
13917 BB->addSuccessor(mallocMBB);
13918 mallocMBB->addSuccessor(continueMBB);
13919 bumpMBB->addSuccessor(continueMBB);
13920
13921 // Take care of the PHI nodes.
13922 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13923 MI->getOperand(0).getReg())
13924 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13925 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13926
13927 // Delete the original pseudo instruction.
13928 MI->eraseFromParent();
13929
13930 // And we're done.
13931 return continueMBB;
13932}
13933
13934MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013935X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013936 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013937 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13938 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013939
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013940 assert(!Subtarget->isTargetEnvMacho());
13941
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013942 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13943 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013944
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013945 if (Subtarget->isTargetWin64()) {
13946 if (Subtarget->isTargetCygMing()) {
13947 // ___chkstk(Mingw64):
13948 // Clobbers R10, R11, RAX and EFLAGS.
13949 // Updates RSP.
13950 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13951 .addExternalSymbol("___chkstk")
13952 .addReg(X86::RAX, RegState::Implicit)
13953 .addReg(X86::RSP, RegState::Implicit)
13954 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13955 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13956 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13957 } else {
13958 // __chkstk(MSVCRT): does not update stack pointer.
13959 // Clobbers R10, R11 and EFLAGS.
13960 // FIXME: RAX(allocated size) might be reused and not killed.
13961 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13962 .addExternalSymbol("__chkstk")
13963 .addReg(X86::RAX, RegState::Implicit)
13964 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13965 // RAX has the offset to subtracted from RSP.
13966 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13967 .addReg(X86::RSP)
13968 .addReg(X86::RAX);
13969 }
13970 } else {
13971 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013972 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13973
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013974 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13975 .addExternalSymbol(StackProbeSymbol)
13976 .addReg(X86::EAX, RegState::Implicit)
13977 .addReg(X86::ESP, RegState::Implicit)
13978 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13979 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13980 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13981 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013982
Dan Gohman14152b42010-07-06 20:24:04 +000013983 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013984 return BB;
13985}
Chris Lattner52600972009-09-02 05:57:00 +000013986
13987MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013988X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13989 MachineBasicBlock *BB) const {
13990 // This is pretty easy. We're taking the value that we received from
13991 // our load from the relocation, sticking it in either RDI (x86-64)
13992 // or EAX and doing an indirect call. The return value will then
13993 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013994 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013995 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013996 DebugLoc DL = MI->getDebugLoc();
13997 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013998
13999 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014000 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014001
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014002 // Get a register mask for the lowered call.
14003 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14004 // proper register mask.
14005 const uint32_t *RegMask =
14006 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014007 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014008 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14009 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014010 .addReg(X86::RIP)
14011 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014012 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014013 MI->getOperand(3).getTargetFlags())
14014 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014015 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014016 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014017 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014018 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014019 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14020 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014021 .addReg(0)
14022 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014023 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014024 MI->getOperand(3).getTargetFlags())
14025 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014026 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014027 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014028 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014029 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014030 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14031 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014032 .addReg(TII->getGlobalBaseReg(F))
14033 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014034 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014035 MI->getOperand(3).getTargetFlags())
14036 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014037 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014038 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014039 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014040 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014041
Dan Gohman14152b42010-07-06 20:24:04 +000014042 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014043 return BB;
14044}
14045
14046MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014047X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14048 MachineBasicBlock *MBB) const {
14049 DebugLoc DL = MI->getDebugLoc();
14050 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14051
14052 MachineFunction *MF = MBB->getParent();
14053 MachineRegisterInfo &MRI = MF->getRegInfo();
14054
14055 const BasicBlock *BB = MBB->getBasicBlock();
14056 MachineFunction::iterator I = MBB;
14057 ++I;
14058
14059 // Memory Reference
14060 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14061 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14062
14063 unsigned DstReg;
14064 unsigned MemOpndSlot = 0;
14065
14066 unsigned CurOp = 0;
14067
14068 DstReg = MI->getOperand(CurOp++).getReg();
14069 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14070 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14071 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14072 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14073
14074 MemOpndSlot = CurOp;
14075
14076 MVT PVT = getPointerTy();
14077 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14078 "Invalid Pointer Size!");
14079
14080 // For v = setjmp(buf), we generate
14081 //
14082 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014083 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014084 // SjLjSetup restoreMBB
14085 //
14086 // mainMBB:
14087 // v_main = 0
14088 //
14089 // sinkMBB:
14090 // v = phi(main, restore)
14091 //
14092 // restoreMBB:
14093 // v_restore = 1
14094
14095 MachineBasicBlock *thisMBB = MBB;
14096 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14097 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14098 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14099 MF->insert(I, mainMBB);
14100 MF->insert(I, sinkMBB);
14101 MF->push_back(restoreMBB);
14102
14103 MachineInstrBuilder MIB;
14104
14105 // Transfer the remainder of BB and its successor edges to sinkMBB.
14106 sinkMBB->splice(sinkMBB->begin(), MBB,
14107 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14108 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14109
14110 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014111 unsigned PtrStoreOpc = 0;
14112 unsigned LabelReg = 0;
14113 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14114 Reloc::Model RM = getTargetMachine().getRelocationModel();
14115 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14116 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014117
Michael Liao281ae5a2012-10-17 02:22:27 +000014118 // Prepare IP either in reg or imm.
14119 if (!UseImmLabel) {
14120 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14121 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14122 LabelReg = MRI.createVirtualRegister(PtrRC);
14123 if (Subtarget->is64Bit()) {
14124 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14125 .addReg(X86::RIP)
14126 .addImm(0)
14127 .addReg(0)
14128 .addMBB(restoreMBB)
14129 .addReg(0);
14130 } else {
14131 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14132 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14133 .addReg(XII->getGlobalBaseReg(MF))
14134 .addImm(0)
14135 .addReg(0)
14136 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14137 .addReg(0);
14138 }
14139 } else
14140 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014141 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014142 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014143 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14144 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014145 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014146 else
14147 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14148 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014149 if (!UseImmLabel)
14150 MIB.addReg(LabelReg);
14151 else
14152 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014153 MIB.setMemRefs(MMOBegin, MMOEnd);
14154 // Setup
14155 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14156 .addMBB(restoreMBB);
14157 MIB.addRegMask(RegInfo->getNoPreservedMask());
14158 thisMBB->addSuccessor(mainMBB);
14159 thisMBB->addSuccessor(restoreMBB);
14160
14161 // mainMBB:
14162 // EAX = 0
14163 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14164 mainMBB->addSuccessor(sinkMBB);
14165
14166 // sinkMBB:
14167 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14168 TII->get(X86::PHI), DstReg)
14169 .addReg(mainDstReg).addMBB(mainMBB)
14170 .addReg(restoreDstReg).addMBB(restoreMBB);
14171
14172 // restoreMBB:
14173 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14174 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14175 restoreMBB->addSuccessor(sinkMBB);
14176
14177 MI->eraseFromParent();
14178 return sinkMBB;
14179}
14180
14181MachineBasicBlock *
14182X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14183 MachineBasicBlock *MBB) const {
14184 DebugLoc DL = MI->getDebugLoc();
14185 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14186
14187 MachineFunction *MF = MBB->getParent();
14188 MachineRegisterInfo &MRI = MF->getRegInfo();
14189
14190 // Memory Reference
14191 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14192 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14193
14194 MVT PVT = getPointerTy();
14195 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14196 "Invalid Pointer Size!");
14197
14198 const TargetRegisterClass *RC =
14199 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14200 unsigned Tmp = MRI.createVirtualRegister(RC);
14201 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14202 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14203 unsigned SP = RegInfo->getStackRegister();
14204
14205 MachineInstrBuilder MIB;
14206
Michael Liao281ae5a2012-10-17 02:22:27 +000014207 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14208 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014209
14210 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14211 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14212
14213 // Reload FP
14214 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14215 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14216 MIB.addOperand(MI->getOperand(i));
14217 MIB.setMemRefs(MMOBegin, MMOEnd);
14218 // Reload IP
14219 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14220 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14221 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014222 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014223 else
14224 MIB.addOperand(MI->getOperand(i));
14225 }
14226 MIB.setMemRefs(MMOBegin, MMOEnd);
14227 // Reload SP
14228 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14229 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14230 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014231 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014232 else
14233 MIB.addOperand(MI->getOperand(i));
14234 }
14235 MIB.setMemRefs(MMOBegin, MMOEnd);
14236 // Jump
14237 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14238
14239 MI->eraseFromParent();
14240 return MBB;
14241}
14242
14243MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014244X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014245 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014246 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014247 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014248 case X86::TAILJMPd64:
14249 case X86::TAILJMPr64:
14250 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014251 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014252 case X86::TCRETURNdi64:
14253 case X86::TCRETURNri64:
14254 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014255 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014256 case X86::WIN_ALLOCA:
14257 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014258 case X86::SEG_ALLOCA_32:
14259 return EmitLoweredSegAlloca(MI, BB, false);
14260 case X86::SEG_ALLOCA_64:
14261 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014262 case X86::TLSCall_32:
14263 case X86::TLSCall_64:
14264 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014265 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014266 case X86::CMOV_FR32:
14267 case X86::CMOV_FR64:
14268 case X86::CMOV_V4F32:
14269 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014270 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014271 case X86::CMOV_V8F32:
14272 case X86::CMOV_V4F64:
14273 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014274 case X86::CMOV_GR16:
14275 case X86::CMOV_GR32:
14276 case X86::CMOV_RFP32:
14277 case X86::CMOV_RFP64:
14278 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014279 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014280
Dale Johannesen849f2142007-07-03 00:53:03 +000014281 case X86::FP32_TO_INT16_IN_MEM:
14282 case X86::FP32_TO_INT32_IN_MEM:
14283 case X86::FP32_TO_INT64_IN_MEM:
14284 case X86::FP64_TO_INT16_IN_MEM:
14285 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014286 case X86::FP64_TO_INT64_IN_MEM:
14287 case X86::FP80_TO_INT16_IN_MEM:
14288 case X86::FP80_TO_INT32_IN_MEM:
14289 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014290 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14291 DebugLoc DL = MI->getDebugLoc();
14292
Evan Cheng60c07e12006-07-05 22:17:51 +000014293 // Change the floating point control register to use "round towards zero"
14294 // mode when truncating to an integer value.
14295 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014296 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014297 addFrameReference(BuildMI(*BB, MI, DL,
14298 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014299
14300 // Load the old value of the high byte of the control word...
14301 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014302 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014303 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014304 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014305
14306 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014307 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014308 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014309
14310 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014311 addFrameReference(BuildMI(*BB, MI, DL,
14312 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014313
14314 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014315 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014316 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014317
14318 // Get the X86 opcode to use.
14319 unsigned Opc;
14320 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014321 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014322 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14323 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14324 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14325 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14326 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14327 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014328 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14329 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14330 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014331 }
14332
14333 X86AddressMode AM;
14334 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014335 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014336 AM.BaseType = X86AddressMode::RegBase;
14337 AM.Base.Reg = Op.getReg();
14338 } else {
14339 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014340 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014341 }
14342 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014343 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014344 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014345 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014346 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014347 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014348 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014349 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014350 AM.GV = Op.getGlobal();
14351 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014352 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014353 }
Dan Gohman14152b42010-07-06 20:24:04 +000014354 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014355 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014356
14357 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014358 addFrameReference(BuildMI(*BB, MI, DL,
14359 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014360
Dan Gohman14152b42010-07-06 20:24:04 +000014361 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014362 return BB;
14363 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014364 // String/text processing lowering.
14365 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014366 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014367 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014368 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014369 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014370 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014371 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014372 case X86::VPCMPESTRM128MEM:
14373 assert(Subtarget->hasSSE42() &&
14374 "Target must have SSE4.2 or AVX features enabled");
14375 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014376
14377 // String/text processing lowering.
14378 case X86::PCMPISTRIREG:
14379 case X86::VPCMPISTRIREG:
14380 case X86::PCMPISTRIMEM:
14381 case X86::VPCMPISTRIMEM:
14382 case X86::PCMPESTRIREG:
14383 case X86::VPCMPESTRIREG:
14384 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014385 case X86::VPCMPESTRIMEM:
14386 assert(Subtarget->hasSSE42() &&
14387 "Target must have SSE4.2 or AVX features enabled");
14388 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014389
Craig Topper8aae8dd2012-11-10 08:57:41 +000014390 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014391 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014392 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014393
Michael Liaobe02a902012-11-08 07:28:54 +000014394 // xbegin
14395 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014396 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014397
Craig Topper8aae8dd2012-11-10 08:57:41 +000014398 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014399 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014400 case X86::ATOMAND16:
14401 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014402 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014403 // Fall through
14404 case X86::ATOMOR8:
14405 case X86::ATOMOR16:
14406 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014407 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014408 // Fall through
14409 case X86::ATOMXOR16:
14410 case X86::ATOMXOR8:
14411 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014412 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014413 // Fall through
14414 case X86::ATOMNAND8:
14415 case X86::ATOMNAND16:
14416 case X86::ATOMNAND32:
14417 case X86::ATOMNAND64:
14418 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014419 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014420 case X86::ATOMMAX16:
14421 case X86::ATOMMAX32:
14422 case X86::ATOMMAX64:
14423 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014424 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014425 case X86::ATOMMIN16:
14426 case X86::ATOMMIN32:
14427 case X86::ATOMMIN64:
14428 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014429 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014430 case X86::ATOMUMAX16:
14431 case X86::ATOMUMAX32:
14432 case X86::ATOMUMAX64:
14433 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014434 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014435 case X86::ATOMUMIN16:
14436 case X86::ATOMUMIN32:
14437 case X86::ATOMUMIN64:
14438 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014439
14440 // This group does 64-bit operations on a 32-bit host.
14441 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014442 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014443 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014444 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014445 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014446 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014447 case X86::ATOMMAX6432:
14448 case X86::ATOMMIN6432:
14449 case X86::ATOMUMAX6432:
14450 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014451 case X86::ATOMSWAP6432:
14452 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014453
Dan Gohmand6708ea2009-08-15 01:38:56 +000014454 case X86::VASTART_SAVE_XMM_REGS:
14455 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014456
14457 case X86::VAARG_64:
14458 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014459
14460 case X86::EH_SjLj_SetJmp32:
14461 case X86::EH_SjLj_SetJmp64:
14462 return emitEHSjLjSetJmp(MI, BB);
14463
14464 case X86::EH_SjLj_LongJmp32:
14465 case X86::EH_SjLj_LongJmp64:
14466 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014467 }
14468}
14469
14470//===----------------------------------------------------------------------===//
14471// X86 Optimization Hooks
14472//===----------------------------------------------------------------------===//
14473
Dan Gohman475871a2008-07-27 21:46:04 +000014474void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014475 APInt &KnownZero,
14476 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014477 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014478 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014479 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014480 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014481 assert((Opc >= ISD::BUILTIN_OP_END ||
14482 Opc == ISD::INTRINSIC_WO_CHAIN ||
14483 Opc == ISD::INTRINSIC_W_CHAIN ||
14484 Opc == ISD::INTRINSIC_VOID) &&
14485 "Should use MaskedValueIsZero if you don't know whether Op"
14486 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014487
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014488 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014489 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014490 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014491 case X86ISD::ADD:
14492 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014493 case X86ISD::ADC:
14494 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014495 case X86ISD::SMUL:
14496 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014497 case X86ISD::INC:
14498 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014499 case X86ISD::OR:
14500 case X86ISD::XOR:
14501 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014502 // These nodes' second result is a boolean.
14503 if (Op.getResNo() == 0)
14504 break;
14505 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014506 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014507 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014508 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014509 case ISD::INTRINSIC_WO_CHAIN: {
14510 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14511 unsigned NumLoBits = 0;
14512 switch (IntId) {
14513 default: break;
14514 case Intrinsic::x86_sse_movmsk_ps:
14515 case Intrinsic::x86_avx_movmsk_ps_256:
14516 case Intrinsic::x86_sse2_movmsk_pd:
14517 case Intrinsic::x86_avx_movmsk_pd_256:
14518 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014519 case Intrinsic::x86_sse2_pmovmskb_128:
14520 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014521 // High bits of movmskp{s|d}, pmovmskb are known zero.
14522 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014523 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014524 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14525 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14526 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14527 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14528 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14529 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014530 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014531 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014532 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014533 break;
14534 }
14535 }
14536 break;
14537 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014538 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014539}
Chris Lattner259e97c2006-01-31 19:43:35 +000014540
Owen Andersonbc146b02010-09-21 20:42:50 +000014541unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14542 unsigned Depth) const {
14543 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14544 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14545 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014546
Owen Andersonbc146b02010-09-21 20:42:50 +000014547 // Fallback case.
14548 return 1;
14549}
14550
Evan Cheng206ee9d2006-07-07 08:33:52 +000014551/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014552/// node is a GlobalAddress + offset.
14553bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014554 const GlobalValue* &GA,
14555 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014556 if (N->getOpcode() == X86ISD::Wrapper) {
14557 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014558 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014559 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014560 return true;
14561 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014562 }
Evan Chengad4196b2008-05-12 19:56:52 +000014563 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014564}
14565
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014566/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14567/// same as extracting the high 128-bit part of 256-bit vector and then
14568/// inserting the result into the low part of a new 256-bit vector
14569static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14570 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014571 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014572
14573 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014574 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014575 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14576 SVOp->getMaskElt(j) >= 0)
14577 return false;
14578
14579 return true;
14580}
14581
14582/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14583/// same as extracting the low 128-bit part of 256-bit vector and then
14584/// inserting the result into the high part of a new 256-bit vector
14585static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14586 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014587 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014588
14589 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014590 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014591 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14592 SVOp->getMaskElt(j) >= 0)
14593 return false;
14594
14595 return true;
14596}
14597
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014598/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14599static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014600 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014601 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014602 DebugLoc dl = N->getDebugLoc();
14603 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14604 SDValue V1 = SVOp->getOperand(0);
14605 SDValue V2 = SVOp->getOperand(1);
14606 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014607 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014608
14609 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14610 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14611 //
14612 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014613 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014614 // V UNDEF BUILD_VECTOR UNDEF
14615 // \ / \ /
14616 // CONCAT_VECTOR CONCAT_VECTOR
14617 // \ /
14618 // \ /
14619 // RESULT: V + zero extended
14620 //
14621 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14622 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14623 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14624 return SDValue();
14625
14626 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14627 return SDValue();
14628
14629 // To match the shuffle mask, the first half of the mask should
14630 // be exactly the first vector, and all the rest a splat with the
14631 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014632 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014633 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14634 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14635 return SDValue();
14636
Chad Rosier3d1161e2012-01-03 21:05:52 +000014637 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14638 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014639 if (Ld->hasNUsesOfValue(1, 0)) {
14640 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14641 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14642 SDValue ResNode =
14643 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14644 Ld->getMemoryVT(),
14645 Ld->getPointerInfo(),
14646 Ld->getAlignment(),
14647 false/*isVolatile*/, true/*ReadMem*/,
14648 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014649
14650 // Make sure the newly-created LOAD is in the same position as Ld in
14651 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14652 // and update uses of Ld's output chain to use the TokenFactor.
14653 if (Ld->hasAnyUseOfValue(1)) {
14654 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14655 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14656 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14657 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14658 SDValue(ResNode.getNode(), 1));
14659 }
14660
Chad Rosier42726832012-05-07 18:47:44 +000014661 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14662 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014663 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014664
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014665 // Emit a zeroed vector and insert the desired subvector on its
14666 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014667 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014668 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014669 return DCI.CombineTo(N, InsV);
14670 }
14671
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014672 //===--------------------------------------------------------------------===//
14673 // Combine some shuffles into subvector extracts and inserts:
14674 //
14675
14676 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14677 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014678 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14679 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014680 return DCI.CombineTo(N, InsV);
14681 }
14682
14683 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14684 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014685 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14686 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014687 return DCI.CombineTo(N, InsV);
14688 }
14689
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014690 return SDValue();
14691}
14692
14693/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014694static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014695 TargetLowering::DAGCombinerInfo &DCI,
14696 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014697 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014698 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014699
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014700 // Don't create instructions with illegal types after legalize types has run.
14701 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14702 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14703 return SDValue();
14704
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014705 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014706 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014707 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014708 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014709
14710 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014711 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014712 return SDValue();
14713
14714 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14715 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14716 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014717 SmallVector<SDValue, 16> Elts;
14718 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014719 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014720
Nate Begemanfdea31a2010-03-24 20:49:50 +000014721 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014722}
Evan Chengd880b972008-05-09 21:53:03 +000014723
Nadav Roteme12bf182013-01-04 17:35:21 +000014724/// PerformTruncateCombine - Converts truncate operation to
14725/// a sequence of vector shuffle operations.
14726/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014727static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14728 TargetLowering::DAGCombinerInfo &DCI,
14729 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014730 return SDValue();
14731}
14732
Craig Topper89f4e662012-03-20 07:17:59 +000014733/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14734/// specific shuffle of a load can be folded into a single element load.
14735/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14736/// shuffles have been customed lowered so we need to handle those here.
14737static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14738 TargetLowering::DAGCombinerInfo &DCI) {
14739 if (DCI.isBeforeLegalizeOps())
14740 return SDValue();
14741
14742 SDValue InVec = N->getOperand(0);
14743 SDValue EltNo = N->getOperand(1);
14744
14745 if (!isa<ConstantSDNode>(EltNo))
14746 return SDValue();
14747
14748 EVT VT = InVec.getValueType();
14749
14750 bool HasShuffleIntoBitcast = false;
14751 if (InVec.getOpcode() == ISD::BITCAST) {
14752 // Don't duplicate a load with other uses.
14753 if (!InVec.hasOneUse())
14754 return SDValue();
14755 EVT BCVT = InVec.getOperand(0).getValueType();
14756 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14757 return SDValue();
14758 InVec = InVec.getOperand(0);
14759 HasShuffleIntoBitcast = true;
14760 }
14761
14762 if (!isTargetShuffle(InVec.getOpcode()))
14763 return SDValue();
14764
14765 // Don't duplicate a load with other uses.
14766 if (!InVec.hasOneUse())
14767 return SDValue();
14768
14769 SmallVector<int, 16> ShuffleMask;
14770 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014771 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14772 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014773 return SDValue();
14774
14775 // Select the input vector, guarding against out of range extract vector.
14776 unsigned NumElems = VT.getVectorNumElements();
14777 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14778 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14779 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14780 : InVec.getOperand(1);
14781
14782 // If inputs to shuffle are the same for both ops, then allow 2 uses
14783 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14784
14785 if (LdNode.getOpcode() == ISD::BITCAST) {
14786 // Don't duplicate a load with other uses.
14787 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14788 return SDValue();
14789
14790 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14791 LdNode = LdNode.getOperand(0);
14792 }
14793
14794 if (!ISD::isNormalLoad(LdNode.getNode()))
14795 return SDValue();
14796
14797 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14798
14799 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14800 return SDValue();
14801
14802 if (HasShuffleIntoBitcast) {
14803 // If there's a bitcast before the shuffle, check if the load type and
14804 // alignment is valid.
14805 unsigned Align = LN0->getAlignment();
14806 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014807 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014808 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14809
14810 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14811 return SDValue();
14812 }
14813
14814 // All checks match so transform back to vector_shuffle so that DAG combiner
14815 // can finish the job
14816 DebugLoc dl = N->getDebugLoc();
14817
14818 // Create shuffle node taking into account the case that its a unary shuffle
14819 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14820 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14821 InVec.getOperand(0), Shuffle,
14822 &ShuffleMask[0]);
14823 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14824 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14825 EltNo);
14826}
14827
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014828/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14829/// generation and convert it from being a bunch of shuffles and extracts
14830/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014831static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014832 TargetLowering::DAGCombinerInfo &DCI) {
14833 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14834 if (NewOp.getNode())
14835 return NewOp;
14836
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014837 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014838 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14839 // from mmx to v2i32 has a single usage.
14840 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14841 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14842 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14843 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14844 N->getValueType(0),
14845 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014846
14847 // Only operate on vectors of 4 elements, where the alternative shuffling
14848 // gets to be more expensive.
14849 if (InputVector.getValueType() != MVT::v4i32)
14850 return SDValue();
14851
14852 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14853 // single use which is a sign-extend or zero-extend, and all elements are
14854 // used.
14855 SmallVector<SDNode *, 4> Uses;
14856 unsigned ExtractedElements = 0;
14857 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14858 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14859 if (UI.getUse().getResNo() != InputVector.getResNo())
14860 return SDValue();
14861
14862 SDNode *Extract = *UI;
14863 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14864 return SDValue();
14865
14866 if (Extract->getValueType(0) != MVT::i32)
14867 return SDValue();
14868 if (!Extract->hasOneUse())
14869 return SDValue();
14870 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14871 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14872 return SDValue();
14873 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14874 return SDValue();
14875
14876 // Record which element was extracted.
14877 ExtractedElements |=
14878 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14879
14880 Uses.push_back(Extract);
14881 }
14882
14883 // If not all the elements were used, this may not be worthwhile.
14884 if (ExtractedElements != 15)
14885 return SDValue();
14886
14887 // Ok, we've now decided to do the transformation.
14888 DebugLoc dl = InputVector.getDebugLoc();
14889
14890 // Store the value to a temporary stack slot.
14891 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014892 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14893 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014894
14895 // Replace each use (extract) with a load of the appropriate element.
14896 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14897 UE = Uses.end(); UI != UE; ++UI) {
14898 SDNode *Extract = *UI;
14899
Nadav Rotem86694292011-05-17 08:31:57 +000014900 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014901 SDValue Idx = Extract->getOperand(1);
14902 unsigned EltSize =
14903 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14904 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014905 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014906 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14907
Nadav Rotem86694292011-05-17 08:31:57 +000014908 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014909 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014910
14911 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014912 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014913 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014914 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014915
14916 // Replace the exact with the load.
14917 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14918 }
14919
14920 // The replacement was made in place; don't return anything.
14921 return SDValue();
14922}
14923
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014924/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14925static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14926 SDValue RHS, SelectionDAG &DAG,
14927 const X86Subtarget *Subtarget) {
14928 if (!VT.isVector())
14929 return 0;
14930
14931 switch (VT.getSimpleVT().SimpleTy) {
14932 default: return 0;
14933 case MVT::v32i8:
14934 case MVT::v16i16:
14935 case MVT::v8i32:
14936 if (!Subtarget->hasAVX2())
14937 return 0;
14938 case MVT::v16i8:
14939 case MVT::v8i16:
14940 case MVT::v4i32:
14941 if (!Subtarget->hasSSE2())
14942 return 0;
14943 }
14944
14945 // SSE2 has only a small subset of the operations.
14946 bool hasUnsigned = Subtarget->hasSSE41() ||
14947 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14948 bool hasSigned = Subtarget->hasSSE41() ||
14949 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14950
14951 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14952
14953 // Check for x CC y ? x : y.
14954 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14955 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14956 switch (CC) {
14957 default: break;
14958 case ISD::SETULT:
14959 case ISD::SETULE:
14960 return hasUnsigned ? X86ISD::UMIN : 0;
14961 case ISD::SETUGT:
14962 case ISD::SETUGE:
14963 return hasUnsigned ? X86ISD::UMAX : 0;
14964 case ISD::SETLT:
14965 case ISD::SETLE:
14966 return hasSigned ? X86ISD::SMIN : 0;
14967 case ISD::SETGT:
14968 case ISD::SETGE:
14969 return hasSigned ? X86ISD::SMAX : 0;
14970 }
14971 // Check for x CC y ? y : x -- a min/max with reversed arms.
14972 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14973 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14974 switch (CC) {
14975 default: break;
14976 case ISD::SETULT:
14977 case ISD::SETULE:
14978 return hasUnsigned ? X86ISD::UMAX : 0;
14979 case ISD::SETUGT:
14980 case ISD::SETUGE:
14981 return hasUnsigned ? X86ISD::UMIN : 0;
14982 case ISD::SETLT:
14983 case ISD::SETLE:
14984 return hasSigned ? X86ISD::SMAX : 0;
14985 case ISD::SETGT:
14986 case ISD::SETGE:
14987 return hasSigned ? X86ISD::SMIN : 0;
14988 }
14989 }
14990
14991 return 0;
14992}
14993
Duncan Sands6bcd2192011-09-17 16:49:39 +000014994/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14995/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014996static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014997 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014998 const X86Subtarget *Subtarget) {
14999 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015000 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015001 // Get the LHS/RHS of the select.
15002 SDValue LHS = N->getOperand(1);
15003 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015004 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015005
Dan Gohman670e5392009-09-21 18:03:22 +000015006 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015007 // instructions match the semantics of the common C idiom x<y?x:y but not
15008 // x<=y?x:y, because of how they handle negative zero (which can be
15009 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015010 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15011 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015012 (Subtarget->hasSSE2() ||
15013 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015014 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015015
Chris Lattner47b4ce82009-03-11 05:48:52 +000015016 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015017 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015018 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15019 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015020 switch (CC) {
15021 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015022 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015023 // Converting this to a min would handle NaNs incorrectly, and swapping
15024 // the operands would cause it to handle comparisons between positive
15025 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015026 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015027 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015028 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15029 break;
15030 std::swap(LHS, RHS);
15031 }
Dan Gohman670e5392009-09-21 18:03:22 +000015032 Opcode = X86ISD::FMIN;
15033 break;
15034 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015035 // Converting this to a min would handle comparisons between positive
15036 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015037 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015038 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15039 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015040 Opcode = X86ISD::FMIN;
15041 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015042 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015043 // Converting this to a min would handle both negative zeros and NaNs
15044 // incorrectly, but we can swap the operands to fix both.
15045 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015046 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015047 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015048 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015049 Opcode = X86ISD::FMIN;
15050 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015051
Dan Gohman670e5392009-09-21 18:03:22 +000015052 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015053 // Converting this to a max would handle comparisons between positive
15054 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015055 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015056 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015057 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015058 Opcode = X86ISD::FMAX;
15059 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015060 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015061 // Converting this to a max would handle NaNs incorrectly, and swapping
15062 // the operands would cause it to handle comparisons between positive
15063 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015064 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015065 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015066 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15067 break;
15068 std::swap(LHS, RHS);
15069 }
Dan Gohman670e5392009-09-21 18:03:22 +000015070 Opcode = X86ISD::FMAX;
15071 break;
15072 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015073 // Converting this to a max would handle both negative zeros and NaNs
15074 // incorrectly, but we can swap the operands to fix both.
15075 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015076 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015077 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015078 case ISD::SETGE:
15079 Opcode = X86ISD::FMAX;
15080 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015081 }
Dan Gohman670e5392009-09-21 18:03:22 +000015082 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015083 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15084 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015085 switch (CC) {
15086 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015087 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015088 // Converting this to a min would handle comparisons between positive
15089 // and negative zero incorrectly, and swapping the operands would
15090 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015091 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015092 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015093 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015094 break;
15095 std::swap(LHS, RHS);
15096 }
Dan Gohman670e5392009-09-21 18:03:22 +000015097 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015098 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015099 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015100 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015101 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015102 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15103 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015104 Opcode = X86ISD::FMIN;
15105 break;
15106 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015107 // Converting this to a min would handle both negative zeros and NaNs
15108 // incorrectly, but we can swap the operands to fix both.
15109 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015110 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015111 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015112 case ISD::SETGE:
15113 Opcode = X86ISD::FMIN;
15114 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015115
Dan Gohman670e5392009-09-21 18:03:22 +000015116 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015117 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015118 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015119 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015120 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015121 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015122 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015123 // Converting this to a max would handle comparisons between positive
15124 // and negative zero incorrectly, and swapping the operands would
15125 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015126 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015127 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015128 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015129 break;
15130 std::swap(LHS, RHS);
15131 }
Dan Gohman670e5392009-09-21 18:03:22 +000015132 Opcode = X86ISD::FMAX;
15133 break;
15134 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015135 // Converting this to a max would handle both negative zeros and NaNs
15136 // incorrectly, but we can swap the operands to fix both.
15137 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015138 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015139 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015140 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015141 Opcode = X86ISD::FMAX;
15142 break;
15143 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015144 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015145
Chris Lattner47b4ce82009-03-11 05:48:52 +000015146 if (Opcode)
15147 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015148 }
Eric Christopherfd179292009-08-27 18:07:15 +000015149
Chris Lattnerd1980a52009-03-12 06:52:53 +000015150 // If this is a select between two integer constants, try to do some
15151 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015152 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15153 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015154 // Don't do this for crazy integer types.
15155 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15156 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015157 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015158 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015159
Chris Lattnercee56e72009-03-13 05:53:31 +000015160 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015161 // Efficiently invertible.
15162 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15163 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15164 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15165 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015166 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015167 }
Eric Christopherfd179292009-08-27 18:07:15 +000015168
Chris Lattnerd1980a52009-03-12 06:52:53 +000015169 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015170 if (FalseC->getAPIntValue() == 0 &&
15171 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015172 if (NeedsCondInvert) // Invert the condition if needed.
15173 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15174 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015175
Chris Lattnerd1980a52009-03-12 06:52:53 +000015176 // Zero extend the condition if needed.
15177 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015178
Chris Lattnercee56e72009-03-13 05:53:31 +000015179 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015180 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015181 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015182 }
Eric Christopherfd179292009-08-27 18:07:15 +000015183
Chris Lattner97a29a52009-03-13 05:22:11 +000015184 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015185 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015186 if (NeedsCondInvert) // Invert the condition if needed.
15187 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15188 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015189
Chris Lattner97a29a52009-03-13 05:22:11 +000015190 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015191 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15192 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015193 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015194 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015195 }
Eric Christopherfd179292009-08-27 18:07:15 +000015196
Chris Lattnercee56e72009-03-13 05:53:31 +000015197 // Optimize cases that will turn into an LEA instruction. This requires
15198 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015199 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015200 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015201 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015202
Chris Lattnercee56e72009-03-13 05:53:31 +000015203 bool isFastMultiplier = false;
15204 if (Diff < 10) {
15205 switch ((unsigned char)Diff) {
15206 default: break;
15207 case 1: // result = add base, cond
15208 case 2: // result = lea base( , cond*2)
15209 case 3: // result = lea base(cond, cond*2)
15210 case 4: // result = lea base( , cond*4)
15211 case 5: // result = lea base(cond, cond*4)
15212 case 8: // result = lea base( , cond*8)
15213 case 9: // result = lea base(cond, cond*8)
15214 isFastMultiplier = true;
15215 break;
15216 }
15217 }
Eric Christopherfd179292009-08-27 18:07:15 +000015218
Chris Lattnercee56e72009-03-13 05:53:31 +000015219 if (isFastMultiplier) {
15220 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15221 if (NeedsCondInvert) // Invert the condition if needed.
15222 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15223 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015224
Chris Lattnercee56e72009-03-13 05:53:31 +000015225 // Zero extend the condition if needed.
15226 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15227 Cond);
15228 // Scale the condition by the difference.
15229 if (Diff != 1)
15230 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15231 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015232
Chris Lattnercee56e72009-03-13 05:53:31 +000015233 // Add the base if non-zero.
15234 if (FalseC->getAPIntValue() != 0)
15235 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15236 SDValue(FalseC, 0));
15237 return Cond;
15238 }
Eric Christopherfd179292009-08-27 18:07:15 +000015239 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015240 }
15241 }
Eric Christopherfd179292009-08-27 18:07:15 +000015242
Evan Cheng56f582d2012-01-04 01:41:39 +000015243 // Canonicalize max and min:
15244 // (x > y) ? x : y -> (x >= y) ? x : y
15245 // (x < y) ? x : y -> (x <= y) ? x : y
15246 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15247 // the need for an extra compare
15248 // against zero. e.g.
15249 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15250 // subl %esi, %edi
15251 // testl %edi, %edi
15252 // movl $0, %eax
15253 // cmovgl %edi, %eax
15254 // =>
15255 // xorl %eax, %eax
15256 // subl %esi, $edi
15257 // cmovsl %eax, %edi
15258 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15259 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15260 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15261 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15262 switch (CC) {
15263 default: break;
15264 case ISD::SETLT:
15265 case ISD::SETGT: {
15266 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15267 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15268 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15269 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15270 }
15271 }
15272 }
15273
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015274 // Match VSELECTs into subs with unsigned saturation.
15275 if (!DCI.isBeforeLegalize() &&
15276 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15277 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15278 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15279 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15280 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15281
15282 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15283 // left side invert the predicate to simplify logic below.
15284 SDValue Other;
15285 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15286 Other = RHS;
15287 CC = ISD::getSetCCInverse(CC, true);
15288 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15289 Other = LHS;
15290 }
15291
15292 if (Other.getNode() && Other->getNumOperands() == 2 &&
15293 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15294 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15295 SDValue CondRHS = Cond->getOperand(1);
15296
15297 // Look for a general sub with unsigned saturation first.
15298 // x >= y ? x-y : 0 --> subus x, y
15299 // x > y ? x-y : 0 --> subus x, y
15300 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15301 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15302 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15303
15304 // If the RHS is a constant we have to reverse the const canonicalization.
15305 // x > C-1 ? x+-C : 0 --> subus x, C
15306 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15307 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15308 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15309 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15310 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15311 DAG.getConstant(-A, VT.getScalarType()));
15312 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15313 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15314 V.data(), V.size()));
15315 }
15316 }
15317
15318 // Another special case: If C was a sign bit, the sub has been
15319 // canonicalized into a xor.
15320 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15321 // it's safe to decanonicalize the xor?
15322 // x s< 0 ? x^C : 0 --> subus x, C
15323 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15324 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15325 isSplatVector(OpRHS.getNode())) {
15326 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15327 if (A.isSignBit())
15328 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15329 }
15330 }
15331 }
15332
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015333 // Try to match a min/max vector operation.
15334 if (!DCI.isBeforeLegalize() &&
15335 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15336 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15337 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15338
Nadav Rotemcc616562012-01-15 19:27:55 +000015339 // If we know that this node is legal then we know that it is going to be
15340 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15341 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15342 // to simplify previous instructions.
15343 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15344 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015345 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015346 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015347
15348 // Don't optimize vector selects that map to mask-registers.
15349 if (BitWidth == 1)
15350 return SDValue();
15351
Nadav Rotemcc616562012-01-15 19:27:55 +000015352 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15353 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15354
15355 APInt KnownZero, KnownOne;
15356 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15357 DCI.isBeforeLegalizeOps());
15358 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15359 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15360 DCI.CommitTargetLoweringOpt(TLO);
15361 }
15362
Dan Gohman475871a2008-07-27 21:46:04 +000015363 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015364}
15365
Michael Liao2a33cec2012-08-10 19:58:13 +000015366// Check whether a boolean test is testing a boolean value generated by
15367// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15368// code.
15369//
15370// Simplify the following patterns:
15371// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15372// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15373// to (Op EFLAGS Cond)
15374//
15375// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15376// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15377// to (Op EFLAGS !Cond)
15378//
15379// where Op could be BRCOND or CMOV.
15380//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015381static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015382 // Quit if not CMP and SUB with its value result used.
15383 if (Cmp.getOpcode() != X86ISD::CMP &&
15384 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15385 return SDValue();
15386
15387 // Quit if not used as a boolean value.
15388 if (CC != X86::COND_E && CC != X86::COND_NE)
15389 return SDValue();
15390
15391 // Check CMP operands. One of them should be 0 or 1 and the other should be
15392 // an SetCC or extended from it.
15393 SDValue Op1 = Cmp.getOperand(0);
15394 SDValue Op2 = Cmp.getOperand(1);
15395
15396 SDValue SetCC;
15397 const ConstantSDNode* C = 0;
15398 bool needOppositeCond = (CC == X86::COND_E);
15399
15400 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15401 SetCC = Op2;
15402 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15403 SetCC = Op1;
15404 else // Quit if all operands are not constants.
15405 return SDValue();
15406
15407 if (C->getZExtValue() == 1)
15408 needOppositeCond = !needOppositeCond;
15409 else if (C->getZExtValue() != 0)
15410 // Quit if the constant is neither 0 or 1.
15411 return SDValue();
15412
15413 // Skip 'zext' node.
15414 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15415 SetCC = SetCC.getOperand(0);
15416
Michael Liao7fdc66b2012-09-10 16:36:16 +000015417 switch (SetCC.getOpcode()) {
15418 case X86ISD::SETCC:
15419 // Set the condition code or opposite one if necessary.
15420 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15421 if (needOppositeCond)
15422 CC = X86::GetOppositeBranchCondition(CC);
15423 return SetCC.getOperand(1);
15424 case X86ISD::CMOV: {
15425 // Check whether false/true value has canonical one, i.e. 0 or 1.
15426 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15427 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15428 // Quit if true value is not a constant.
15429 if (!TVal)
15430 return SDValue();
15431 // Quit if false value is not a constant.
15432 if (!FVal) {
15433 // A special case for rdrand, where 0 is set if false cond is found.
15434 SDValue Op = SetCC.getOperand(0);
15435 if (Op.getOpcode() != X86ISD::RDRAND)
15436 return SDValue();
15437 }
15438 // Quit if false value is not the constant 0 or 1.
15439 bool FValIsFalse = true;
15440 if (FVal && FVal->getZExtValue() != 0) {
15441 if (FVal->getZExtValue() != 1)
15442 return SDValue();
15443 // If FVal is 1, opposite cond is needed.
15444 needOppositeCond = !needOppositeCond;
15445 FValIsFalse = false;
15446 }
15447 // Quit if TVal is not the constant opposite of FVal.
15448 if (FValIsFalse && TVal->getZExtValue() != 1)
15449 return SDValue();
15450 if (!FValIsFalse && TVal->getZExtValue() != 0)
15451 return SDValue();
15452 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15453 if (needOppositeCond)
15454 CC = X86::GetOppositeBranchCondition(CC);
15455 return SetCC.getOperand(3);
15456 }
15457 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015458
Michael Liao7fdc66b2012-09-10 16:36:16 +000015459 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015460}
15461
Chris Lattnerd1980a52009-03-12 06:52:53 +000015462/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15463static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015464 TargetLowering::DAGCombinerInfo &DCI,
15465 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015466 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015467
Chris Lattnerd1980a52009-03-12 06:52:53 +000015468 // If the flag operand isn't dead, don't touch this CMOV.
15469 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15470 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015471
Evan Chengb5a55d92011-05-24 01:48:22 +000015472 SDValue FalseOp = N->getOperand(0);
15473 SDValue TrueOp = N->getOperand(1);
15474 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15475 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015476
Evan Chengb5a55d92011-05-24 01:48:22 +000015477 if (CC == X86::COND_E || CC == X86::COND_NE) {
15478 switch (Cond.getOpcode()) {
15479 default: break;
15480 case X86ISD::BSR:
15481 case X86ISD::BSF:
15482 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15483 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15484 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15485 }
15486 }
15487
Michael Liao2a33cec2012-08-10 19:58:13 +000015488 SDValue Flags;
15489
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015490 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015491 if (Flags.getNode() &&
15492 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015493 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015494 SDValue Ops[] = { FalseOp, TrueOp,
15495 DAG.getConstant(CC, MVT::i8), Flags };
15496 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15497 Ops, array_lengthof(Ops));
15498 }
15499
Chris Lattnerd1980a52009-03-12 06:52:53 +000015500 // If this is a select between two integer constants, try to do some
15501 // optimizations. Note that the operands are ordered the opposite of SELECT
15502 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015503 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15504 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015505 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15506 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015507 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15508 CC = X86::GetOppositeBranchCondition(CC);
15509 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015510 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015511 }
Eric Christopherfd179292009-08-27 18:07:15 +000015512
Chris Lattnerd1980a52009-03-12 06:52:53 +000015513 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015514 // This is efficient for any integer data type (including i8/i16) and
15515 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015516 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015517 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15518 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015519
Chris Lattnerd1980a52009-03-12 06:52:53 +000015520 // Zero extend the condition if needed.
15521 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015522
Chris Lattnerd1980a52009-03-12 06:52:53 +000015523 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15524 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015525 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015526 if (N->getNumValues() == 2) // Dead flag value?
15527 return DCI.CombineTo(N, Cond, SDValue());
15528 return Cond;
15529 }
Eric Christopherfd179292009-08-27 18:07:15 +000015530
Chris Lattnercee56e72009-03-13 05:53:31 +000015531 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15532 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015533 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015534 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15535 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015536
Chris Lattner97a29a52009-03-13 05:22:11 +000015537 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015538 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15539 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015540 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15541 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015542
Chris Lattner97a29a52009-03-13 05:22:11 +000015543 if (N->getNumValues() == 2) // Dead flag value?
15544 return DCI.CombineTo(N, Cond, SDValue());
15545 return Cond;
15546 }
Eric Christopherfd179292009-08-27 18:07:15 +000015547
Chris Lattnercee56e72009-03-13 05:53:31 +000015548 // Optimize cases that will turn into an LEA instruction. This requires
15549 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015550 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015551 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015552 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015553
Chris Lattnercee56e72009-03-13 05:53:31 +000015554 bool isFastMultiplier = false;
15555 if (Diff < 10) {
15556 switch ((unsigned char)Diff) {
15557 default: break;
15558 case 1: // result = add base, cond
15559 case 2: // result = lea base( , cond*2)
15560 case 3: // result = lea base(cond, cond*2)
15561 case 4: // result = lea base( , cond*4)
15562 case 5: // result = lea base(cond, cond*4)
15563 case 8: // result = lea base( , cond*8)
15564 case 9: // result = lea base(cond, cond*8)
15565 isFastMultiplier = true;
15566 break;
15567 }
15568 }
Eric Christopherfd179292009-08-27 18:07:15 +000015569
Chris Lattnercee56e72009-03-13 05:53:31 +000015570 if (isFastMultiplier) {
15571 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015572 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15573 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015574 // Zero extend the condition if needed.
15575 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15576 Cond);
15577 // Scale the condition by the difference.
15578 if (Diff != 1)
15579 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15580 DAG.getConstant(Diff, Cond.getValueType()));
15581
15582 // Add the base if non-zero.
15583 if (FalseC->getAPIntValue() != 0)
15584 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15585 SDValue(FalseC, 0));
15586 if (N->getNumValues() == 2) // Dead flag value?
15587 return DCI.CombineTo(N, Cond, SDValue());
15588 return Cond;
15589 }
Eric Christopherfd179292009-08-27 18:07:15 +000015590 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015591 }
15592 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015593
15594 // Handle these cases:
15595 // (select (x != c), e, c) -> select (x != c), e, x),
15596 // (select (x == c), c, e) -> select (x == c), x, e)
15597 // where the c is an integer constant, and the "select" is the combination
15598 // of CMOV and CMP.
15599 //
15600 // The rationale for this change is that the conditional-move from a constant
15601 // needs two instructions, however, conditional-move from a register needs
15602 // only one instruction.
15603 //
15604 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15605 // some instruction-combining opportunities. This opt needs to be
15606 // postponed as late as possible.
15607 //
15608 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15609 // the DCI.xxxx conditions are provided to postpone the optimization as
15610 // late as possible.
15611
15612 ConstantSDNode *CmpAgainst = 0;
15613 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15614 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15615 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15616
15617 if (CC == X86::COND_NE &&
15618 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15619 CC = X86::GetOppositeBranchCondition(CC);
15620 std::swap(TrueOp, FalseOp);
15621 }
15622
15623 if (CC == X86::COND_E &&
15624 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15625 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15626 DAG.getConstant(CC, MVT::i8), Cond };
15627 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15628 array_lengthof(Ops));
15629 }
15630 }
15631 }
15632
Chris Lattnerd1980a52009-03-12 06:52:53 +000015633 return SDValue();
15634}
15635
Evan Cheng0b0cd912009-03-28 05:57:29 +000015636/// PerformMulCombine - Optimize a single multiply with constant into two
15637/// in order to implement it with two cheaper instructions, e.g.
15638/// LEA + SHL, LEA + LEA.
15639static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15640 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015641 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15642 return SDValue();
15643
Owen Andersone50ed302009-08-10 22:56:29 +000015644 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015645 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015646 return SDValue();
15647
15648 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15649 if (!C)
15650 return SDValue();
15651 uint64_t MulAmt = C->getZExtValue();
15652 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15653 return SDValue();
15654
15655 uint64_t MulAmt1 = 0;
15656 uint64_t MulAmt2 = 0;
15657 if ((MulAmt % 9) == 0) {
15658 MulAmt1 = 9;
15659 MulAmt2 = MulAmt / 9;
15660 } else if ((MulAmt % 5) == 0) {
15661 MulAmt1 = 5;
15662 MulAmt2 = MulAmt / 5;
15663 } else if ((MulAmt % 3) == 0) {
15664 MulAmt1 = 3;
15665 MulAmt2 = MulAmt / 3;
15666 }
15667 if (MulAmt2 &&
15668 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15669 DebugLoc DL = N->getDebugLoc();
15670
15671 if (isPowerOf2_64(MulAmt2) &&
15672 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15673 // If second multiplifer is pow2, issue it first. We want the multiply by
15674 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15675 // is an add.
15676 std::swap(MulAmt1, MulAmt2);
15677
15678 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015679 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015680 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015681 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015682 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015683 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015684 DAG.getConstant(MulAmt1, VT));
15685
Eric Christopherfd179292009-08-27 18:07:15 +000015686 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015687 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015688 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015689 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015690 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015691 DAG.getConstant(MulAmt2, VT));
15692
15693 // Do not add new nodes to DAG combiner worklist.
15694 DCI.CombineTo(N, NewMul, false);
15695 }
15696 return SDValue();
15697}
15698
Evan Chengad9c0a32009-12-15 00:53:42 +000015699static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15700 SDValue N0 = N->getOperand(0);
15701 SDValue N1 = N->getOperand(1);
15702 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15703 EVT VT = N0.getValueType();
15704
15705 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15706 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015707 if (VT.isInteger() && !VT.isVector() &&
15708 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015709 N0.getOperand(1).getOpcode() == ISD::Constant) {
15710 SDValue N00 = N0.getOperand(0);
15711 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15712 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15713 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15714 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15715 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15716 APInt ShAmt = N1C->getAPIntValue();
15717 Mask = Mask.shl(ShAmt);
15718 if (Mask != 0)
15719 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15720 N00, DAG.getConstant(Mask, VT));
15721 }
15722 }
15723
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015724 // Hardware support for vector shifts is sparse which makes us scalarize the
15725 // vector operations in many cases. Also, on sandybridge ADD is faster than
15726 // shl.
15727 // (shl V, 1) -> add V,V
15728 if (isSplatVector(N1.getNode())) {
15729 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15730 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15731 // We shift all of the values by one. In many cases we do not have
15732 // hardware support for this operation. This is better expressed as an ADD
15733 // of two values.
15734 if (N1C && (1 == N1C->getZExtValue())) {
15735 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15736 }
15737 }
15738
Evan Chengad9c0a32009-12-15 00:53:42 +000015739 return SDValue();
15740}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015741
Nate Begeman740ab032009-01-26 00:52:55 +000015742/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15743/// when possible.
15744static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015745 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015746 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015747 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015748 if (N->getOpcode() == ISD::SHL) {
15749 SDValue V = PerformSHLCombine(N, DAG);
15750 if (V.getNode()) return V;
15751 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015752
Nate Begeman740ab032009-01-26 00:52:55 +000015753 // On X86 with SSE2 support, we can transform this to a vector shift if
15754 // all elements are shifted by the same amount. We can't do this in legalize
15755 // because the a constant vector is typically transformed to a constant pool
15756 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015757 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015758 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015759
Craig Topper7be5dfd2011-11-12 09:58:49 +000015760 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015761 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015762 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015763 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015764
Mon P Wang3becd092009-01-28 08:12:05 +000015765 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015766 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015767 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015768 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015769 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15770 unsigned NumElts = VT.getVectorNumElements();
15771 unsigned i = 0;
15772 for (; i != NumElts; ++i) {
15773 SDValue Arg = ShAmtOp.getOperand(i);
15774 if (Arg.getOpcode() == ISD::UNDEF) continue;
15775 BaseShAmt = Arg;
15776 break;
15777 }
Craig Topper37c26772012-01-17 04:44:50 +000015778 // Handle the case where the build_vector is all undef
15779 // FIXME: Should DAG allow this?
15780 if (i == NumElts)
15781 return SDValue();
15782
Mon P Wang3becd092009-01-28 08:12:05 +000015783 for (; i != NumElts; ++i) {
15784 SDValue Arg = ShAmtOp.getOperand(i);
15785 if (Arg.getOpcode() == ISD::UNDEF) continue;
15786 if (Arg != BaseShAmt) {
15787 return SDValue();
15788 }
15789 }
15790 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015791 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015792 SDValue InVec = ShAmtOp.getOperand(0);
15793 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15794 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15795 unsigned i = 0;
15796 for (; i != NumElts; ++i) {
15797 SDValue Arg = InVec.getOperand(i);
15798 if (Arg.getOpcode() == ISD::UNDEF) continue;
15799 BaseShAmt = Arg;
15800 break;
15801 }
15802 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15803 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015804 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015805 if (C->getZExtValue() == SplatIdx)
15806 BaseShAmt = InVec.getOperand(1);
15807 }
15808 }
Mon P Wang845b1892012-02-01 22:15:20 +000015809 if (BaseShAmt.getNode() == 0) {
15810 // Don't create instructions with illegal types after legalize
15811 // types has run.
15812 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15813 !DCI.isBeforeLegalize())
15814 return SDValue();
15815
Mon P Wangefa42202009-09-03 19:56:25 +000015816 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15817 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015818 }
Mon P Wang3becd092009-01-28 08:12:05 +000015819 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015820 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015821
Mon P Wangefa42202009-09-03 19:56:25 +000015822 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015823 if (EltVT.bitsGT(MVT::i32))
15824 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15825 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015826 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015827
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015828 // The shift amount is identical so we can do a vector shift.
15829 SDValue ValOp = N->getOperand(0);
15830 switch (N->getOpcode()) {
15831 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015832 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015833 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015834 switch (VT.getSimpleVT().SimpleTy) {
15835 default: return SDValue();
15836 case MVT::v2i64:
15837 case MVT::v4i32:
15838 case MVT::v8i16:
15839 case MVT::v4i64:
15840 case MVT::v8i32:
15841 case MVT::v16i16:
15842 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15843 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015844 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015845 switch (VT.getSimpleVT().SimpleTy) {
15846 default: return SDValue();
15847 case MVT::v4i32:
15848 case MVT::v8i16:
15849 case MVT::v8i32:
15850 case MVT::v16i16:
15851 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15852 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015853 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015854 switch (VT.getSimpleVT().SimpleTy) {
15855 default: return SDValue();
15856 case MVT::v2i64:
15857 case MVT::v4i32:
15858 case MVT::v8i16:
15859 case MVT::v4i64:
15860 case MVT::v8i32:
15861 case MVT::v16i16:
15862 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15863 }
Nate Begeman740ab032009-01-26 00:52:55 +000015864 }
Nate Begeman740ab032009-01-26 00:52:55 +000015865}
15866
Stuart Hastings865f0932011-06-03 23:53:54 +000015867// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15868// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15869// and friends. Likewise for OR -> CMPNEQSS.
15870static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15871 TargetLowering::DAGCombinerInfo &DCI,
15872 const X86Subtarget *Subtarget) {
15873 unsigned opcode;
15874
15875 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15876 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015877 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015878 SDValue N0 = N->getOperand(0);
15879 SDValue N1 = N->getOperand(1);
15880 SDValue CMP0 = N0->getOperand(1);
15881 SDValue CMP1 = N1->getOperand(1);
15882 DebugLoc DL = N->getDebugLoc();
15883
15884 // The SETCCs should both refer to the same CMP.
15885 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15886 return SDValue();
15887
15888 SDValue CMP00 = CMP0->getOperand(0);
15889 SDValue CMP01 = CMP0->getOperand(1);
15890 EVT VT = CMP00.getValueType();
15891
15892 if (VT == MVT::f32 || VT == MVT::f64) {
15893 bool ExpectingFlags = false;
15894 // Check for any users that want flags:
15895 for (SDNode::use_iterator UI = N->use_begin(),
15896 UE = N->use_end();
15897 !ExpectingFlags && UI != UE; ++UI)
15898 switch (UI->getOpcode()) {
15899 default:
15900 case ISD::BR_CC:
15901 case ISD::BRCOND:
15902 case ISD::SELECT:
15903 ExpectingFlags = true;
15904 break;
15905 case ISD::CopyToReg:
15906 case ISD::SIGN_EXTEND:
15907 case ISD::ZERO_EXTEND:
15908 case ISD::ANY_EXTEND:
15909 break;
15910 }
15911
15912 if (!ExpectingFlags) {
15913 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15914 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15915
15916 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15917 X86::CondCode tmp = cc0;
15918 cc0 = cc1;
15919 cc1 = tmp;
15920 }
15921
15922 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15923 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15924 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15925 X86ISD::NodeType NTOperator = is64BitFP ?
15926 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15927 // FIXME: need symbolic constants for these magic numbers.
15928 // See X86ATTInstPrinter.cpp:printSSECC().
15929 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15930 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15931 DAG.getConstant(x86cc, MVT::i8));
15932 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15933 OnesOrZeroesF);
15934 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15935 DAG.getConstant(1, MVT::i32));
15936 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15937 return OneBitOfTruth;
15938 }
15939 }
15940 }
15941 }
15942 return SDValue();
15943}
15944
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015945/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15946/// so it can be folded inside ANDNP.
15947static bool CanFoldXORWithAllOnes(const SDNode *N) {
15948 EVT VT = N->getValueType(0);
15949
15950 // Match direct AllOnes for 128 and 256-bit vectors
15951 if (ISD::isBuildVectorAllOnes(N))
15952 return true;
15953
15954 // Look through a bit convert.
15955 if (N->getOpcode() == ISD::BITCAST)
15956 N = N->getOperand(0).getNode();
15957
15958 // Sometimes the operand may come from a insert_subvector building a 256-bit
15959 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015960 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015961 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15962 SDValue V1 = N->getOperand(0);
15963 SDValue V2 = N->getOperand(1);
15964
15965 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15966 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15967 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15968 ISD::isBuildVectorAllOnes(V2.getNode()))
15969 return true;
15970 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015971
15972 return false;
15973}
15974
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015975// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
15976// register. In most cases we actually compare or select YMM-sized registers
15977// and mixing the two types creates horrible code. This method optimizes
15978// some of the transition sequences.
15979static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
15980 TargetLowering::DAGCombinerInfo &DCI,
15981 const X86Subtarget *Subtarget) {
15982 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000015983 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015984 return SDValue();
15985
15986 assert((N->getOpcode() == ISD::ANY_EXTEND ||
15987 N->getOpcode() == ISD::ZERO_EXTEND ||
15988 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
15989
15990 SDValue Narrow = N->getOperand(0);
15991 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000015992 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015993 return SDValue();
15994
15995 if (Narrow->getOpcode() != ISD::XOR &&
15996 Narrow->getOpcode() != ISD::AND &&
15997 Narrow->getOpcode() != ISD::OR)
15998 return SDValue();
15999
16000 SDValue N0 = Narrow->getOperand(0);
16001 SDValue N1 = Narrow->getOperand(1);
16002 DebugLoc DL = Narrow->getDebugLoc();
16003
16004 // The Left side has to be a trunc.
16005 if (N0.getOpcode() != ISD::TRUNCATE)
16006 return SDValue();
16007
16008 // The type of the truncated inputs.
16009 EVT WideVT = N0->getOperand(0)->getValueType(0);
16010 if (WideVT != VT)
16011 return SDValue();
16012
16013 // The right side has to be a 'trunc' or a constant vector.
16014 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16015 bool RHSConst = (isSplatVector(N1.getNode()) &&
16016 isa<ConstantSDNode>(N1->getOperand(0)));
16017 if (!RHSTrunc && !RHSConst)
16018 return SDValue();
16019
16020 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16021
16022 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16023 return SDValue();
16024
16025 // Set N0 and N1 to hold the inputs to the new wide operation.
16026 N0 = N0->getOperand(0);
16027 if (RHSConst) {
16028 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16029 N1->getOperand(0));
16030 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16031 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16032 } else if (RHSTrunc) {
16033 N1 = N1->getOperand(0);
16034 }
16035
16036 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016037 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016038 unsigned Opcode = N->getOpcode();
16039 switch (Opcode) {
16040 case ISD::ANY_EXTEND:
16041 return Op;
16042 case ISD::ZERO_EXTEND: {
16043 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16044 APInt Mask = APInt::getAllOnesValue(InBits);
16045 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16046 return DAG.getNode(ISD::AND, DL, VT,
16047 Op, DAG.getConstant(Mask, VT));
16048 }
16049 case ISD::SIGN_EXTEND:
16050 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16051 Op, DAG.getValueType(NarrowVT));
16052 default:
16053 llvm_unreachable("Unexpected opcode");
16054 }
16055}
16056
Nate Begemanb65c1752010-12-17 22:55:37 +000016057static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16058 TargetLowering::DAGCombinerInfo &DCI,
16059 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016060 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016061 if (DCI.isBeforeLegalizeOps())
16062 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016063
Stuart Hastings865f0932011-06-03 23:53:54 +000016064 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16065 if (R.getNode())
16066 return R;
16067
Craig Topperb926afc2012-12-17 05:12:30 +000016068 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016069 // BLSI is X & (-X)
16070 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016071 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16072 SDValue N0 = N->getOperand(0);
16073 SDValue N1 = N->getOperand(1);
16074 DebugLoc DL = N->getDebugLoc();
16075
Craig Topperb4c94572011-10-21 06:55:01 +000016076 // Check LHS for neg
16077 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16078 isZero(N0.getOperand(0)))
16079 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16080
16081 // Check RHS for neg
16082 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16083 isZero(N1.getOperand(0)))
16084 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16085
16086 // Check LHS for X-1
16087 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16088 isAllOnes(N0.getOperand(1)))
16089 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16090
16091 // Check RHS for X-1
16092 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16093 isAllOnes(N1.getOperand(1)))
16094 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16095
Craig Topper54a11172011-10-14 07:06:56 +000016096 return SDValue();
16097 }
16098
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016099 // Want to form ANDNP nodes:
16100 // 1) In the hopes of then easily combining them with OR and AND nodes
16101 // to form PBLEND/PSIGN.
16102 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016103 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016104 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016105
Nate Begemanb65c1752010-12-17 22:55:37 +000016106 SDValue N0 = N->getOperand(0);
16107 SDValue N1 = N->getOperand(1);
16108 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016109
Nate Begemanb65c1752010-12-17 22:55:37 +000016110 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016111 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016112 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16113 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016114 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016115
16116 // Check RHS for vnot
16117 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016118 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16119 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016120 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016121
Nate Begemanb65c1752010-12-17 22:55:37 +000016122 return SDValue();
16123}
16124
Evan Cheng760d1942010-01-04 21:22:48 +000016125static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016126 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016127 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016128 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016129 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016130 return SDValue();
16131
Stuart Hastings865f0932011-06-03 23:53:54 +000016132 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16133 if (R.getNode())
16134 return R;
16135
Evan Cheng760d1942010-01-04 21:22:48 +000016136 SDValue N0 = N->getOperand(0);
16137 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016138
Nate Begemanb65c1752010-12-17 22:55:37 +000016139 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016140 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016141 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016142 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016143 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016144
Craig Topper1666cb62011-11-19 07:07:26 +000016145 // Canonicalize pandn to RHS
16146 if (N0.getOpcode() == X86ISD::ANDNP)
16147 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016148 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016149 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16150 SDValue Mask = N1.getOperand(0);
16151 SDValue X = N1.getOperand(1);
16152 SDValue Y;
16153 if (N0.getOperand(0) == Mask)
16154 Y = N0.getOperand(1);
16155 if (N0.getOperand(1) == Mask)
16156 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016157
Craig Topper1666cb62011-11-19 07:07:26 +000016158 // Check to see if the mask appeared in both the AND and ANDNP and
16159 if (!Y.getNode())
16160 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016161
Craig Topper1666cb62011-11-19 07:07:26 +000016162 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016163 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016164 if (Mask.getOpcode() == ISD::BITCAST)
16165 Mask = Mask.getOperand(0);
16166 if (X.getOpcode() == ISD::BITCAST)
16167 X = X.getOperand(0);
16168 if (Y.getOpcode() == ISD::BITCAST)
16169 Y = Y.getOperand(0);
16170
Craig Topper1666cb62011-11-19 07:07:26 +000016171 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016172
Craig Toppered2e13d2012-01-22 19:15:14 +000016173 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016174 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16175 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016176 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016177 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016178
16179 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016180 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016181 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16182 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16183 if ((SraAmt + 1) != EltBits)
16184 return SDValue();
16185
16186 DebugLoc DL = N->getDebugLoc();
16187
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016188 // We are going to replace the AND, OR, NAND with either BLEND
16189 // or PSIGN, which only look at the MSB. The VSRAI instruction
16190 // does not affect the highest bit, so we can get rid of it.
16191 Mask = Mask.getOperand(0);
16192
Craig Topper1666cb62011-11-19 07:07:26 +000016193 // Now we know we at least have a plendvb with the mask val. See if
16194 // we can form a psignb/w/d.
16195 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016196 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16197 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016198 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16199 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16200 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016201 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016202 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016203 }
16204 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016205 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016206 return SDValue();
16207
16208 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16209
16210 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16211 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16212 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016213 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016214 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016215 }
16216 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016217
Craig Topper1666cb62011-11-19 07:07:26 +000016218 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16219 return SDValue();
16220
Nate Begemanb65c1752010-12-17 22:55:37 +000016221 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016222 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16223 std::swap(N0, N1);
16224 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16225 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016226 if (!N0.hasOneUse() || !N1.hasOneUse())
16227 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016228
16229 SDValue ShAmt0 = N0.getOperand(1);
16230 if (ShAmt0.getValueType() != MVT::i8)
16231 return SDValue();
16232 SDValue ShAmt1 = N1.getOperand(1);
16233 if (ShAmt1.getValueType() != MVT::i8)
16234 return SDValue();
16235 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16236 ShAmt0 = ShAmt0.getOperand(0);
16237 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16238 ShAmt1 = ShAmt1.getOperand(0);
16239
16240 DebugLoc DL = N->getDebugLoc();
16241 unsigned Opc = X86ISD::SHLD;
16242 SDValue Op0 = N0.getOperand(0);
16243 SDValue Op1 = N1.getOperand(0);
16244 if (ShAmt0.getOpcode() == ISD::SUB) {
16245 Opc = X86ISD::SHRD;
16246 std::swap(Op0, Op1);
16247 std::swap(ShAmt0, ShAmt1);
16248 }
16249
Evan Cheng8b1190a2010-04-28 01:18:01 +000016250 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016251 if (ShAmt1.getOpcode() == ISD::SUB) {
16252 SDValue Sum = ShAmt1.getOperand(0);
16253 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016254 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16255 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16256 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16257 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016258 return DAG.getNode(Opc, DL, VT,
16259 Op0, Op1,
16260 DAG.getNode(ISD::TRUNCATE, DL,
16261 MVT::i8, ShAmt0));
16262 }
16263 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16264 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16265 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016266 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016267 return DAG.getNode(Opc, DL, VT,
16268 N0.getOperand(0), N1.getOperand(0),
16269 DAG.getNode(ISD::TRUNCATE, DL,
16270 MVT::i8, ShAmt0));
16271 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016272
Evan Cheng760d1942010-01-04 21:22:48 +000016273 return SDValue();
16274}
16275
Manman Ren92363622012-06-07 22:39:10 +000016276// Generate NEG and CMOV for integer abs.
16277static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16278 EVT VT = N->getValueType(0);
16279
16280 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16281 // 8-bit integer abs to NEG and CMOV.
16282 if (VT.isInteger() && VT.getSizeInBits() == 8)
16283 return SDValue();
16284
16285 SDValue N0 = N->getOperand(0);
16286 SDValue N1 = N->getOperand(1);
16287 DebugLoc DL = N->getDebugLoc();
16288
16289 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16290 // and change it to SUB and CMOV.
16291 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16292 N0.getOpcode() == ISD::ADD &&
16293 N0.getOperand(1) == N1 &&
16294 N1.getOpcode() == ISD::SRA &&
16295 N1.getOperand(0) == N0.getOperand(0))
16296 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16297 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16298 // Generate SUB & CMOV.
16299 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16300 DAG.getConstant(0, VT), N0.getOperand(0));
16301
16302 SDValue Ops[] = { N0.getOperand(0), Neg,
16303 DAG.getConstant(X86::COND_GE, MVT::i8),
16304 SDValue(Neg.getNode(), 1) };
16305 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16306 Ops, array_lengthof(Ops));
16307 }
16308 return SDValue();
16309}
16310
Craig Topper3738ccd2011-12-27 06:27:23 +000016311// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016312static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16313 TargetLowering::DAGCombinerInfo &DCI,
16314 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016315 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016316 if (DCI.isBeforeLegalizeOps())
16317 return SDValue();
16318
Manman Ren45d53b82012-06-08 18:58:26 +000016319 if (Subtarget->hasCMov()) {
16320 SDValue RV = performIntegerAbsCombine(N, DAG);
16321 if (RV.getNode())
16322 return RV;
16323 }
Manman Ren92363622012-06-07 22:39:10 +000016324
16325 // Try forming BMI if it is available.
16326 if (!Subtarget->hasBMI())
16327 return SDValue();
16328
Craig Topperb4c94572011-10-21 06:55:01 +000016329 if (VT != MVT::i32 && VT != MVT::i64)
16330 return SDValue();
16331
Craig Topper3738ccd2011-12-27 06:27:23 +000016332 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16333
Craig Topperb4c94572011-10-21 06:55:01 +000016334 // Create BLSMSK instructions by finding X ^ (X-1)
16335 SDValue N0 = N->getOperand(0);
16336 SDValue N1 = N->getOperand(1);
16337 DebugLoc DL = N->getDebugLoc();
16338
16339 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16340 isAllOnes(N0.getOperand(1)))
16341 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16342
16343 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16344 isAllOnes(N1.getOperand(1)))
16345 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16346
16347 return SDValue();
16348}
16349
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016350/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16351static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016352 TargetLowering::DAGCombinerInfo &DCI,
16353 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016354 LoadSDNode *Ld = cast<LoadSDNode>(N);
16355 EVT RegVT = Ld->getValueType(0);
16356 EVT MemVT = Ld->getMemoryVT();
16357 DebugLoc dl = Ld->getDebugLoc();
16358 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016359 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016360
16361 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016362 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016363 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016364
16365 // On Sandybridge unaligned 256bit loads are inefficient.
16366 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016367 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016368 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016369 if (NumElems < 2)
16370 return SDValue();
16371
Nadav Rotem48177ac2013-01-18 23:10:30 +000016372 SDValue Ptr = Ld->getBasePtr();
16373 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16374
16375 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16376 NumElems/2);
16377 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16378 Ld->getPointerInfo(), Ld->isVolatile(),
16379 Ld->isNonTemporal(), Ld->isInvariant(),
16380 Alignment);
16381 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16382 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16383 Ld->getPointerInfo(), Ld->isVolatile(),
16384 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016385 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016386 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16387 Load1.getValue(1),
16388 Load2.getValue(1));
16389
16390 SDValue NewVec = DAG.getUNDEF(RegVT);
16391 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16392 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16393 return DCI.CombineTo(N, NewVec, TF, true);
16394 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016395
Nadav Rotemca6f2962011-09-18 19:00:23 +000016396 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016397 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16398 // expansion is still better than scalar code.
16399 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16400 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016401 // TODO: It is possible to support ZExt by zeroing the undef values
16402 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016403 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16404 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016405 assert(MemVT != RegVT && "Cannot extend to the same type");
16406 assert(MemVT.isVector() && "Must load a vector from memory");
16407
16408 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016409 unsigned MemSz = MemVT.getSizeInBits();
16410 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016411
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016412 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16413 return SDValue();
16414
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016415 // All sizes must be a power of two.
16416 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16417 return SDValue();
16418
16419 // Attempt to load the original value using scalar loads.
16420 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016421 MVT SclrLoadTy = MVT::i8;
16422 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16423 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16424 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016425 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016426 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016427 }
16428 }
16429
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016430 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16431 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16432 (64 <= MemSz))
16433 SclrLoadTy = MVT::f64;
16434
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016435 // Calculate the number of scalar loads that we need to perform
16436 // in order to load our vector from memory.
16437 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016438 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16439 return SDValue();
16440
16441 unsigned loadRegZize = RegSz;
16442 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16443 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016444
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016445 // Represent our vector as a sequence of elements which are the
16446 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016447 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016448 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016449
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016450 // Represent the data using the same element type that is stored in
16451 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016452 EVT WideVecVT =
16453 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16454 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016455
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016456 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16457 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016458
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016459 // We can't shuffle using an illegal type.
16460 if (!TLI.isTypeLegal(WideVecVT))
16461 return SDValue();
16462
16463 SmallVector<SDValue, 8> Chains;
16464 SDValue Ptr = Ld->getBasePtr();
16465 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16466 TLI.getPointerTy());
16467 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16468
16469 for (unsigned i = 0; i < NumLoads; ++i) {
16470 // Perform a single load.
16471 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16472 Ptr, Ld->getPointerInfo(),
16473 Ld->isVolatile(), Ld->isNonTemporal(),
16474 Ld->isInvariant(), Ld->getAlignment());
16475 Chains.push_back(ScalarLoad.getValue(1));
16476 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16477 // another round of DAGCombining.
16478 if (i == 0)
16479 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16480 else
16481 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16482 ScalarLoad, DAG.getIntPtrConstant(i));
16483
16484 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16485 }
16486
16487 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16488 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016489
16490 // Bitcast the loaded value to a vector of the original element type, in
16491 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016492 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016493 unsigned SizeRatio = RegSz/MemSz;
16494
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016495 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016496 // If we have SSE4.1 we can directly emit a VSEXT node.
16497 if (Subtarget->hasSSE41()) {
16498 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16499 return DCI.CombineTo(N, Sext, TF, true);
16500 }
16501
16502 // Otherwise we'll shuffle the small elements in the high bits of the
16503 // larger type and perform an arithmetic shift. If the shift is not legal
16504 // it's better to scalarize.
16505 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16506 return SDValue();
16507
16508 // Redistribute the loaded elements into the different locations.
16509 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16510 for (unsigned i = 0; i != NumElems; ++i)
16511 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16512
16513 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16514 DAG.getUNDEF(WideVecVT),
16515 &ShuffleVec[0]);
16516
16517 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16518
16519 // Build the arithmetic shift.
16520 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16521 MemVT.getVectorElementType().getSizeInBits();
16522 SmallVector<SDValue, 8> C(NumElems,
16523 DAG.getConstant(Amt, RegVT.getScalarType()));
16524 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16525 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16526
16527 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016528 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016529
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016530 // Redistribute the loaded elements into the different locations.
16531 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016532 for (unsigned i = 0; i != NumElems; ++i)
16533 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016534
16535 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016536 DAG.getUNDEF(WideVecVT),
16537 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016538
16539 // Bitcast to the requested type.
16540 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16541 // Replace the original load with the new sequence
16542 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016543 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016544 }
16545
16546 return SDValue();
16547}
16548
Chris Lattner149a4e52008-02-22 02:09:43 +000016549/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016550static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016551 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016552 StoreSDNode *St = cast<StoreSDNode>(N);
16553 EVT VT = St->getValue().getValueType();
16554 EVT StVT = St->getMemoryVT();
16555 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016556 SDValue StoredVal = St->getOperand(1);
16557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016558 unsigned Alignment = St->getAlignment();
16559 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016560
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016561 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016562 // On Sandy Bridge, 256-bit memory operations are executed by two
16563 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16564 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016565 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016566 StVT == VT && !IsAligned) {
16567 unsigned NumElems = VT.getVectorNumElements();
16568 if (NumElems < 2)
16569 return SDValue();
16570
16571 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16572 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016573
16574 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16575 SDValue Ptr0 = St->getBasePtr();
16576 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16577
16578 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16579 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016580 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016581 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16582 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016583 St->isNonTemporal(),
16584 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016585 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16586 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016587
16588 // Optimize trunc store (of multiple scalars) to shuffle and store.
16589 // First, pack all of the elements in one place. Next, store to memory
16590 // in fewer chunks.
16591 if (St->isTruncatingStore() && VT.isVector()) {
16592 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16593 unsigned NumElems = VT.getVectorNumElements();
16594 assert(StVT != VT && "Cannot truncate to the same type");
16595 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16596 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16597
16598 // From, To sizes and ElemCount must be pow of two
16599 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016600 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016601 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016602 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016603
Nadav Rotem614061b2011-08-10 19:30:14 +000016604 unsigned SizeRatio = FromSz / ToSz;
16605
16606 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16607
16608 // Create a type on which we perform the shuffle
16609 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16610 StVT.getScalarType(), NumElems*SizeRatio);
16611
16612 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16613
16614 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16615 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016616 for (unsigned i = 0; i != NumElems; ++i)
16617 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016618
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016619 // Can't shuffle using an illegal type.
16620 if (!TLI.isTypeLegal(WideVecVT))
16621 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016622
16623 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016624 DAG.getUNDEF(WideVecVT),
16625 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016626 // At this point all of the data is stored at the bottom of the
16627 // register. We now need to save it to mem.
16628
16629 // Find the largest store unit
16630 MVT StoreType = MVT::i8;
16631 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16632 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16633 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016634 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016635 StoreType = Tp;
16636 }
16637
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016638 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16639 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16640 (64 <= NumElems * ToSz))
16641 StoreType = MVT::f64;
16642
Nadav Rotem614061b2011-08-10 19:30:14 +000016643 // Bitcast the original vector into a vector of store-size units
16644 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016645 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016646 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16647 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16648 SmallVector<SDValue, 8> Chains;
16649 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16650 TLI.getPointerTy());
16651 SDValue Ptr = St->getBasePtr();
16652
16653 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016654 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016655 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16656 StoreType, ShuffWide,
16657 DAG.getIntPtrConstant(i));
16658 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16659 St->getPointerInfo(), St->isVolatile(),
16660 St->isNonTemporal(), St->getAlignment());
16661 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16662 Chains.push_back(Ch);
16663 }
16664
16665 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16666 Chains.size());
16667 }
16668
Chris Lattner149a4e52008-02-22 02:09:43 +000016669 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16670 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016671 // A preferable solution to the general problem is to figure out the right
16672 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016673
16674 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016675 if (VT.getSizeInBits() != 64)
16676 return SDValue();
16677
Devang Patel578efa92009-06-05 21:57:13 +000016678 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016679 bool NoImplicitFloatOps = F->getAttributes().
16680 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016681 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016682 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016683 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016684 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016685 isa<LoadSDNode>(St->getValue()) &&
16686 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16687 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016688 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016689 LoadSDNode *Ld = 0;
16690 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016691 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016692 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016693 // Must be a store of a load. We currently handle two cases: the load
16694 // is a direct child, and it's under an intervening TokenFactor. It is
16695 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016696 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016697 Ld = cast<LoadSDNode>(St->getChain());
16698 else if (St->getValue().hasOneUse() &&
16699 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016700 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016701 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016702 TokenFactorIndex = i;
16703 Ld = cast<LoadSDNode>(St->getValue());
16704 } else
16705 Ops.push_back(ChainVal->getOperand(i));
16706 }
16707 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016708
Evan Cheng536e6672009-03-12 05:59:15 +000016709 if (!Ld || !ISD::isNormalLoad(Ld))
16710 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016711
Evan Cheng536e6672009-03-12 05:59:15 +000016712 // If this is not the MMX case, i.e. we are just turning i64 load/store
16713 // into f64 load/store, avoid the transformation if there are multiple
16714 // uses of the loaded value.
16715 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16716 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016717
Evan Cheng536e6672009-03-12 05:59:15 +000016718 DebugLoc LdDL = Ld->getDebugLoc();
16719 DebugLoc StDL = N->getDebugLoc();
16720 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16721 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16722 // pair instead.
16723 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016724 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016725 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16726 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016727 Ld->isNonTemporal(), Ld->isInvariant(),
16728 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016729 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016730 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016731 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016732 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016733 Ops.size());
16734 }
Evan Cheng536e6672009-03-12 05:59:15 +000016735 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016736 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016737 St->isVolatile(), St->isNonTemporal(),
16738 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016739 }
Evan Cheng536e6672009-03-12 05:59:15 +000016740
16741 // Otherwise, lower to two pairs of 32-bit loads / stores.
16742 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016743 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16744 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016745
Owen Anderson825b72b2009-08-11 20:47:22 +000016746 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016747 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016748 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016749 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016750 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016751 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016752 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016753 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016754 MinAlign(Ld->getAlignment(), 4));
16755
16756 SDValue NewChain = LoLd.getValue(1);
16757 if (TokenFactorIndex != -1) {
16758 Ops.push_back(LoLd);
16759 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016760 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016761 Ops.size());
16762 }
16763
16764 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016765 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16766 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016767
16768 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016769 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016770 St->isVolatile(), St->isNonTemporal(),
16771 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016772 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016773 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016774 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016775 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016776 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016777 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016778 }
Dan Gohman475871a2008-07-27 21:46:04 +000016779 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016780}
16781
Duncan Sands17470be2011-09-22 20:15:48 +000016782/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16783/// and return the operands for the horizontal operation in LHS and RHS. A
16784/// horizontal operation performs the binary operation on successive elements
16785/// of its first operand, then on successive elements of its second operand,
16786/// returning the resulting values in a vector. For example, if
16787/// A = < float a0, float a1, float a2, float a3 >
16788/// and
16789/// B = < float b0, float b1, float b2, float b3 >
16790/// then the result of doing a horizontal operation on A and B is
16791/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16792/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16793/// A horizontal-op B, for some already available A and B, and if so then LHS is
16794/// set to A, RHS to B, and the routine returns 'true'.
16795/// Note that the binary operation should have the property that if one of the
16796/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016797static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016798 // Look for the following pattern: if
16799 // A = < float a0, float a1, float a2, float a3 >
16800 // B = < float b0, float b1, float b2, float b3 >
16801 // and
16802 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16803 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16804 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16805 // which is A horizontal-op B.
16806
16807 // At least one of the operands should be a vector shuffle.
16808 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16809 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16810 return false;
16811
16812 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016813
16814 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16815 "Unsupported vector type for horizontal add/sub");
16816
16817 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16818 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016819 unsigned NumElts = VT.getVectorNumElements();
16820 unsigned NumLanes = VT.getSizeInBits()/128;
16821 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016822 assert((NumLaneElts % 2 == 0) &&
16823 "Vector type should have an even number of elements in each lane");
16824 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016825
16826 // View LHS in the form
16827 // LHS = VECTOR_SHUFFLE A, B, LMask
16828 // If LHS is not a shuffle then pretend it is the shuffle
16829 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16830 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16831 // type VT.
16832 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016833 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016834 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16835 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16836 A = LHS.getOperand(0);
16837 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16838 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016839 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16840 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016841 } else {
16842 if (LHS.getOpcode() != ISD::UNDEF)
16843 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016844 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016845 LMask[i] = i;
16846 }
16847
16848 // Likewise, view RHS in the form
16849 // RHS = VECTOR_SHUFFLE C, D, RMask
16850 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016851 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016852 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16853 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16854 C = RHS.getOperand(0);
16855 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16856 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016857 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16858 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016859 } else {
16860 if (RHS.getOpcode() != ISD::UNDEF)
16861 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016862 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016863 RMask[i] = i;
16864 }
16865
16866 // Check that the shuffles are both shuffling the same vectors.
16867 if (!(A == C && B == D) && !(A == D && B == C))
16868 return false;
16869
16870 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16871 if (!A.getNode() && !B.getNode())
16872 return false;
16873
16874 // If A and B occur in reverse order in RHS, then "swap" them (which means
16875 // rewriting the mask).
16876 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016877 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016878
16879 // At this point LHS and RHS are equivalent to
16880 // LHS = VECTOR_SHUFFLE A, B, LMask
16881 // RHS = VECTOR_SHUFFLE A, B, RMask
16882 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016883 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016884 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016885
Craig Topperf8363302011-12-02 08:18:41 +000016886 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016887 if (LIdx < 0 || RIdx < 0 ||
16888 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16889 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016890 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016891
Craig Topperf8363302011-12-02 08:18:41 +000016892 // Check that successive elements are being operated on. If not, this is
16893 // not a horizontal operation.
16894 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16895 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016896 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016897 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016898 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016899 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016900 }
16901
16902 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16903 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16904 return true;
16905}
16906
16907/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16908static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16909 const X86Subtarget *Subtarget) {
16910 EVT VT = N->getValueType(0);
16911 SDValue LHS = N->getOperand(0);
16912 SDValue RHS = N->getOperand(1);
16913
16914 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016915 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016916 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016917 isHorizontalBinOp(LHS, RHS, true))
16918 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16919 return SDValue();
16920}
16921
16922/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16923static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16924 const X86Subtarget *Subtarget) {
16925 EVT VT = N->getValueType(0);
16926 SDValue LHS = N->getOperand(0);
16927 SDValue RHS = N->getOperand(1);
16928
16929 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016930 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016931 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016932 isHorizontalBinOp(LHS, RHS, false))
16933 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16934 return SDValue();
16935}
16936
Chris Lattner6cf73262008-01-25 06:14:17 +000016937/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16938/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016939static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016940 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16941 // F[X]OR(0.0, x) -> x
16942 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016943 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16944 if (C->getValueAPF().isPosZero())
16945 return N->getOperand(1);
16946 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16947 if (C->getValueAPF().isPosZero())
16948 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016949 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016950}
16951
Nadav Rotemd60cb112012-08-19 13:06:16 +000016952/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16953/// X86ISD::FMAX nodes.
16954static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16955 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16956
16957 // Only perform optimizations if UnsafeMath is used.
16958 if (!DAG.getTarget().Options.UnsafeFPMath)
16959 return SDValue();
16960
16961 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016962 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016963 unsigned NewOp = 0;
16964 switch (N->getOpcode()) {
16965 default: llvm_unreachable("unknown opcode");
16966 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16967 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16968 }
16969
16970 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16971 N->getOperand(0), N->getOperand(1));
16972}
16973
Chris Lattneraf723b92008-01-25 05:46:26 +000016974/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016975static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016976 // FAND(0.0, x) -> 0.0
16977 // FAND(x, 0.0) -> 0.0
16978 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16979 if (C->getValueAPF().isPosZero())
16980 return N->getOperand(0);
16981 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16982 if (C->getValueAPF().isPosZero())
16983 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016984 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016985}
16986
Dan Gohmane5af2d32009-01-29 01:59:02 +000016987static SDValue PerformBTCombine(SDNode *N,
16988 SelectionDAG &DAG,
16989 TargetLowering::DAGCombinerInfo &DCI) {
16990 // BT ignores high bits in the bit index operand.
16991 SDValue Op1 = N->getOperand(1);
16992 if (Op1.hasOneUse()) {
16993 unsigned BitWidth = Op1.getValueSizeInBits();
16994 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16995 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016996 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16997 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016998 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016999 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17000 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17001 DCI.CommitTargetLoweringOpt(TLO);
17002 }
17003 return SDValue();
17004}
Chris Lattner83e6c992006-10-04 06:57:07 +000017005
Eli Friedman7a5e5552009-06-07 06:52:44 +000017006static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17007 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017008 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017009 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017010 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017011 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017012 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017013 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017014 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017015 }
17016 return SDValue();
17017}
17018
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017019static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17020 TargetLowering::DAGCombinerInfo &DCI,
17021 const X86Subtarget *Subtarget) {
17022 if (!DCI.isBeforeLegalizeOps())
17023 return SDValue();
17024
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017025 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017026 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017027
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017028 EVT VT = N->getValueType(0);
17029 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017030 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17031 if (R.getNode())
17032 return R;
17033 }
17034
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017035 return SDValue();
17036}
17037
Michael Liaof6c24ee2012-08-10 14:39:24 +000017038static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017039 const X86Subtarget* Subtarget) {
17040 DebugLoc dl = N->getDebugLoc();
17041 EVT VT = N->getValueType(0);
17042
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017043 // Let legalize expand this if it isn't a legal type yet.
17044 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17045 return SDValue();
17046
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017047 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017048 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17049 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017050 return SDValue();
17051
17052 SDValue A = N->getOperand(0);
17053 SDValue B = N->getOperand(1);
17054 SDValue C = N->getOperand(2);
17055
17056 bool NegA = (A.getOpcode() == ISD::FNEG);
17057 bool NegB = (B.getOpcode() == ISD::FNEG);
17058 bool NegC = (C.getOpcode() == ISD::FNEG);
17059
Michael Liaof6c24ee2012-08-10 14:39:24 +000017060 // Negative multiplication when NegA xor NegB
17061 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017062 if (NegA)
17063 A = A.getOperand(0);
17064 if (NegB)
17065 B = B.getOperand(0);
17066 if (NegC)
17067 C = C.getOperand(0);
17068
17069 unsigned Opcode;
17070 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017071 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017072 else
Craig Topperbf404372012-08-31 15:40:30 +000017073 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17074
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017075 return DAG.getNode(Opcode, dl, VT, A, B, C);
17076}
17077
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017078static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017079 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017080 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017081 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17082 // (and (i32 x86isd::setcc_carry), 1)
17083 // This eliminates the zext. This transformation is necessary because
17084 // ISD::SETCC is always legalized to i8.
17085 DebugLoc dl = N->getDebugLoc();
17086 SDValue N0 = N->getOperand(0);
17087 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017088
Evan Cheng2e489c42009-12-16 00:53:11 +000017089 if (N0.getOpcode() == ISD::AND &&
17090 N0.hasOneUse() &&
17091 N0.getOperand(0).hasOneUse()) {
17092 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017093 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17094 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17095 if (!C || C->getZExtValue() != 1)
17096 return SDValue();
17097 return DAG.getNode(ISD::AND, dl, VT,
17098 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17099 N00.getOperand(0), N00.getOperand(1)),
17100 DAG.getConstant(1, VT));
17101 }
17102 }
17103
Craig Topper5a529e42013-01-18 06:44:29 +000017104 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017105 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17106 if (R.getNode())
17107 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017108 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017109
Evan Cheng2e489c42009-12-16 00:53:11 +000017110 return SDValue();
17111}
17112
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017113// Optimize x == -y --> x+y == 0
17114// x != -y --> x+y != 0
17115static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17116 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17117 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017118 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017119
17120 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17122 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17123 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17124 LHS.getValueType(), RHS, LHS.getOperand(1));
17125 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17126 addV, DAG.getConstant(0, addV.getValueType()), CC);
17127 }
17128 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17130 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17131 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17132 RHS.getValueType(), LHS, RHS.getOperand(1));
17133 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17134 addV, DAG.getConstant(0, addV.getValueType()), CC);
17135 }
17136 return SDValue();
17137}
17138
Shuxin Yanga5526a92012-10-31 23:11:48 +000017139// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17140// as "sbb reg,reg", since it can be extended without zext and produces
17141// an all-ones bit which is more useful than 0/1 in some cases.
17142static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17143 return DAG.getNode(ISD::AND, DL, MVT::i8,
17144 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17145 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17146 DAG.getConstant(1, MVT::i8));
17147}
17148
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017149// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017150static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17151 TargetLowering::DAGCombinerInfo &DCI,
17152 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017153 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017154 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17155 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017156
Shuxin Yanga5526a92012-10-31 23:11:48 +000017157 if (CC == X86::COND_A) {
17158 // Try to convert COND_A into COND_B in an attempt to facilitate
17159 // materializing "setb reg".
17160 //
17161 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17162 // cannot take an immediate as its first operand.
17163 //
17164 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
17165 EFLAGS.getValueType().isInteger() &&
17166 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17167 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17168 EFLAGS.getNode()->getVTList(),
17169 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17170 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17171 return MaterializeSETB(DL, NewEFLAGS, DAG);
17172 }
17173 }
17174
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017175 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17176 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17177 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017178 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017179 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017180
Michael Liao2a33cec2012-08-10 19:58:13 +000017181 SDValue Flags;
17182
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017183 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17184 if (Flags.getNode()) {
17185 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17186 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17187 }
17188
Michael Liao2a33cec2012-08-10 19:58:13 +000017189 return SDValue();
17190}
17191
17192// Optimize branch condition evaluation.
17193//
17194static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17195 TargetLowering::DAGCombinerInfo &DCI,
17196 const X86Subtarget *Subtarget) {
17197 DebugLoc DL = N->getDebugLoc();
17198 SDValue Chain = N->getOperand(0);
17199 SDValue Dest = N->getOperand(1);
17200 SDValue EFLAGS = N->getOperand(3);
17201 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17202
17203 SDValue Flags;
17204
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017205 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17206 if (Flags.getNode()) {
17207 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17208 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17209 Flags);
17210 }
17211
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017212 return SDValue();
17213}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017214
Benjamin Kramer1396c402011-06-18 11:09:41 +000017215static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17216 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017217 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017218 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017219
17220 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017221 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017222 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017223 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017224 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17225 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17226 }
17227
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017228 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17229 // a 32-bit target where SSE doesn't support i64->FP operations.
17230 if (Op0.getOpcode() == ISD::LOAD) {
17231 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17232 EVT VT = Ld->getValueType(0);
17233 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17234 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17235 !XTLI->getSubtarget()->is64Bit() &&
17236 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017237 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17238 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017239 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17240 return FILDChain;
17241 }
17242 }
17243 return SDValue();
17244}
17245
Chris Lattner23a01992010-12-20 01:37:09 +000017246// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17247static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17248 X86TargetLowering::DAGCombinerInfo &DCI) {
17249 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17250 // the result is either zero or one (depending on the input carry bit).
17251 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17252 if (X86::isZeroNode(N->getOperand(0)) &&
17253 X86::isZeroNode(N->getOperand(1)) &&
17254 // We don't have a good way to replace an EFLAGS use, so only do this when
17255 // dead right now.
17256 SDValue(N, 1).use_empty()) {
17257 DebugLoc DL = N->getDebugLoc();
17258 EVT VT = N->getValueType(0);
17259 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17260 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17261 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17262 DAG.getConstant(X86::COND_B,MVT::i8),
17263 N->getOperand(2)),
17264 DAG.getConstant(1, VT));
17265 return DCI.CombineTo(N, Res1, CarryOut);
17266 }
17267
17268 return SDValue();
17269}
17270
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017271// fold (add Y, (sete X, 0)) -> adc 0, Y
17272// (add Y, (setne X, 0)) -> sbb -1, Y
17273// (sub (sete X, 0), Y) -> sbb 0, Y
17274// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017275static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017276 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017277
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017278 // Look through ZExts.
17279 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17280 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17281 return SDValue();
17282
17283 SDValue SetCC = Ext.getOperand(0);
17284 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17285 return SDValue();
17286
17287 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17288 if (CC != X86::COND_E && CC != X86::COND_NE)
17289 return SDValue();
17290
17291 SDValue Cmp = SetCC.getOperand(1);
17292 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017293 !X86::isZeroNode(Cmp.getOperand(1)) ||
17294 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017295 return SDValue();
17296
17297 SDValue CmpOp0 = Cmp.getOperand(0);
17298 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17299 DAG.getConstant(1, CmpOp0.getValueType()));
17300
17301 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17302 if (CC == X86::COND_NE)
17303 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17304 DL, OtherVal.getValueType(), OtherVal,
17305 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17306 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17307 DL, OtherVal.getValueType(), OtherVal,
17308 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17309}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017310
Craig Topper54f952a2011-11-19 09:02:40 +000017311/// PerformADDCombine - Do target-specific dag combines on integer adds.
17312static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17313 const X86Subtarget *Subtarget) {
17314 EVT VT = N->getValueType(0);
17315 SDValue Op0 = N->getOperand(0);
17316 SDValue Op1 = N->getOperand(1);
17317
17318 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017319 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017320 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017321 isHorizontalBinOp(Op0, Op1, true))
17322 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17323
17324 return OptimizeConditionalInDecrement(N, DAG);
17325}
17326
17327static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17328 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017329 SDValue Op0 = N->getOperand(0);
17330 SDValue Op1 = N->getOperand(1);
17331
17332 // X86 can't encode an immediate LHS of a sub. See if we can push the
17333 // negation into a preceding instruction.
17334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017335 // If the RHS of the sub is a XOR with one use and a constant, invert the
17336 // immediate. Then add one to the LHS of the sub so we can turn
17337 // X-Y -> X+~Y+1, saving one register.
17338 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17339 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017340 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017341 EVT VT = Op0.getValueType();
17342 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17343 Op1.getOperand(0),
17344 DAG.getConstant(~XorC, VT));
17345 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017346 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017347 }
17348 }
17349
Craig Topper54f952a2011-11-19 09:02:40 +000017350 // Try to synthesize horizontal adds from adds of shuffles.
17351 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017352 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017353 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017354 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017355 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17356
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017357 return OptimizeConditionalInDecrement(N, DAG);
17358}
17359
Michael Liaod9d09602012-10-23 17:34:00 +000017360/// performVZEXTCombine - Performs build vector combines
17361static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17362 TargetLowering::DAGCombinerInfo &DCI,
17363 const X86Subtarget *Subtarget) {
17364 // (vzext (bitcast (vzext (x)) -> (vzext x)
17365 SDValue In = N->getOperand(0);
17366 while (In.getOpcode() == ISD::BITCAST)
17367 In = In.getOperand(0);
17368
17369 if (In.getOpcode() != X86ISD::VZEXT)
17370 return SDValue();
17371
17372 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17373}
17374
Dan Gohman475871a2008-07-27 21:46:04 +000017375SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017376 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017377 SelectionDAG &DAG = DCI.DAG;
17378 switch (N->getOpcode()) {
17379 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017380 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017381 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017382 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017383 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017384 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017385 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17386 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017387 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017388 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017389 case ISD::SHL:
17390 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017391 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017392 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017393 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017394 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017395 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017396 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017397 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017398 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17399 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017400 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017401 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017402 case X86ISD::FMIN:
17403 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017404 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017405 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017406 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017407 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017408 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017409 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017410 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017411 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017412 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017413 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017414 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017415 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017416 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017417 case X86ISD::UNPCKH:
17418 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017419 case X86ISD::MOVHLPS:
17420 case X86ISD::MOVLHPS:
17421 case X86ISD::PSHUFD:
17422 case X86ISD::PSHUFHW:
17423 case X86ISD::PSHUFLW:
17424 case X86ISD::MOVSS:
17425 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017426 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017427 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017428 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017429 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017430 }
17431
Dan Gohman475871a2008-07-27 21:46:04 +000017432 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017433}
17434
Evan Chenge5b51ac2010-04-17 06:13:15 +000017435/// isTypeDesirableForOp - Return true if the target has native support for
17436/// the specified value type and it is 'desirable' to use the type for the
17437/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17438/// instruction encodings are longer and some i16 instructions are slow.
17439bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17440 if (!isTypeLegal(VT))
17441 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017442 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017443 return true;
17444
17445 switch (Opc) {
17446 default:
17447 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017448 case ISD::LOAD:
17449 case ISD::SIGN_EXTEND:
17450 case ISD::ZERO_EXTEND:
17451 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017452 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017453 case ISD::SRL:
17454 case ISD::SUB:
17455 case ISD::ADD:
17456 case ISD::MUL:
17457 case ISD::AND:
17458 case ISD::OR:
17459 case ISD::XOR:
17460 return false;
17461 }
17462}
17463
17464/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017465/// beneficial for dag combiner to promote the specified node. If true, it
17466/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017467bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017468 EVT VT = Op.getValueType();
17469 if (VT != MVT::i16)
17470 return false;
17471
Evan Cheng4c26e932010-04-19 19:29:22 +000017472 bool Promote = false;
17473 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017474 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017475 default: break;
17476 case ISD::LOAD: {
17477 LoadSDNode *LD = cast<LoadSDNode>(Op);
17478 // If the non-extending load has a single use and it's not live out, then it
17479 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017480 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17481 Op.hasOneUse()*/) {
17482 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17483 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17484 // The only case where we'd want to promote LOAD (rather then it being
17485 // promoted as an operand is when it's only use is liveout.
17486 if (UI->getOpcode() != ISD::CopyToReg)
17487 return false;
17488 }
17489 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017490 Promote = true;
17491 break;
17492 }
17493 case ISD::SIGN_EXTEND:
17494 case ISD::ZERO_EXTEND:
17495 case ISD::ANY_EXTEND:
17496 Promote = true;
17497 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017498 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017499 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017500 SDValue N0 = Op.getOperand(0);
17501 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017502 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017503 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017504 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017505 break;
17506 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017507 case ISD::ADD:
17508 case ISD::MUL:
17509 case ISD::AND:
17510 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017511 case ISD::XOR:
17512 Commute = true;
17513 // fallthrough
17514 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017515 SDValue N0 = Op.getOperand(0);
17516 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017517 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017518 return false;
17519 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017520 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017521 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017522 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017523 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017524 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017525 }
17526 }
17527
17528 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017529 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017530}
17531
Evan Cheng60c07e12006-07-05 22:17:51 +000017532//===----------------------------------------------------------------------===//
17533// X86 Inline Assembly Support
17534//===----------------------------------------------------------------------===//
17535
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017536namespace {
17537 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017538 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017539 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017540
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017541 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017542 StringRef piece(*args[i]);
17543 if (!s.startswith(piece)) // Check if the piece matches.
17544 return false;
17545
17546 s = s.substr(piece.size());
17547 StringRef::size_type pos = s.find_first_not_of(" \t");
17548 if (pos == 0) // We matched a prefix.
17549 return false;
17550
17551 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017552 }
17553
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017554 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017555 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017556 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017557}
17558
Chris Lattnerb8105652009-07-20 17:51:36 +000017559bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17560 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017561
17562 std::string AsmStr = IA->getAsmString();
17563
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017564 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17565 if (!Ty || Ty->getBitWidth() % 16 != 0)
17566 return false;
17567
Chris Lattnerb8105652009-07-20 17:51:36 +000017568 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017569 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017570 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017571
17572 switch (AsmPieces.size()) {
17573 default: return false;
17574 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017575 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017576 // we will turn this bswap into something that will be lowered to logical
17577 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17578 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017579 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017580 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17581 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17582 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17583 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17584 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17585 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017586 // No need to check constraints, nothing other than the equivalent of
17587 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017588 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017589 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017590
Chris Lattnerb8105652009-07-20 17:51:36 +000017591 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017592 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017593 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017594 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17595 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017596 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017597 const std::string &ConstraintsStr = IA->getConstraintString();
17598 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017599 std::sort(AsmPieces.begin(), AsmPieces.end());
17600 if (AsmPieces.size() == 4 &&
17601 AsmPieces[0] == "~{cc}" &&
17602 AsmPieces[1] == "~{dirflag}" &&
17603 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017604 AsmPieces[3] == "~{fpsr}")
17605 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017606 }
17607 break;
17608 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017609 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017610 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017611 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17612 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17613 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017614 AsmPieces.clear();
17615 const std::string &ConstraintsStr = IA->getConstraintString();
17616 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17617 std::sort(AsmPieces.begin(), AsmPieces.end());
17618 if (AsmPieces.size() == 4 &&
17619 AsmPieces[0] == "~{cc}" &&
17620 AsmPieces[1] == "~{dirflag}" &&
17621 AsmPieces[2] == "~{flags}" &&
17622 AsmPieces[3] == "~{fpsr}")
17623 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017624 }
Evan Cheng55d42002011-01-08 01:24:27 +000017625
17626 if (CI->getType()->isIntegerTy(64)) {
17627 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17628 if (Constraints.size() >= 2 &&
17629 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17630 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17631 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017632 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17633 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17634 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017635 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017636 }
17637 }
17638 break;
17639 }
17640 return false;
17641}
17642
Chris Lattnerf4dff842006-07-11 02:54:03 +000017643/// getConstraintType - Given a constraint letter, return the type of
17644/// constraint it is for this target.
17645X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017646X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17647 if (Constraint.size() == 1) {
17648 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017649 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017650 case 'q':
17651 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017652 case 'f':
17653 case 't':
17654 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017655 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017656 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017657 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017658 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017659 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017660 case 'a':
17661 case 'b':
17662 case 'c':
17663 case 'd':
17664 case 'S':
17665 case 'D':
17666 case 'A':
17667 return C_Register;
17668 case 'I':
17669 case 'J':
17670 case 'K':
17671 case 'L':
17672 case 'M':
17673 case 'N':
17674 case 'G':
17675 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017676 case 'e':
17677 case 'Z':
17678 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017679 default:
17680 break;
17681 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017682 }
Chris Lattner4234f572007-03-25 02:14:49 +000017683 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017684}
17685
John Thompson44ab89e2010-10-29 17:29:13 +000017686/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017687/// This object must already have been set up with the operand type
17688/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017689TargetLowering::ConstraintWeight
17690 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017691 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017692 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017693 Value *CallOperandVal = info.CallOperandVal;
17694 // If we don't have a value, we can't do a match,
17695 // but allow it at the lowest weight.
17696 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017697 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017698 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017699 // Look at the constraint type.
17700 switch (*constraint) {
17701 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017702 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17703 case 'R':
17704 case 'q':
17705 case 'Q':
17706 case 'a':
17707 case 'b':
17708 case 'c':
17709 case 'd':
17710 case 'S':
17711 case 'D':
17712 case 'A':
17713 if (CallOperandVal->getType()->isIntegerTy())
17714 weight = CW_SpecificReg;
17715 break;
17716 case 'f':
17717 case 't':
17718 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017719 if (type->isFloatingPointTy())
17720 weight = CW_SpecificReg;
17721 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017722 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017723 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17724 weight = CW_SpecificReg;
17725 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017726 case 'x':
17727 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017728 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017729 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017730 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017731 break;
17732 case 'I':
17733 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17734 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017735 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017736 }
17737 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017738 case 'J':
17739 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17740 if (C->getZExtValue() <= 63)
17741 weight = CW_Constant;
17742 }
17743 break;
17744 case 'K':
17745 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17746 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17747 weight = CW_Constant;
17748 }
17749 break;
17750 case 'L':
17751 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17752 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17753 weight = CW_Constant;
17754 }
17755 break;
17756 case 'M':
17757 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17758 if (C->getZExtValue() <= 3)
17759 weight = CW_Constant;
17760 }
17761 break;
17762 case 'N':
17763 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17764 if (C->getZExtValue() <= 0xff)
17765 weight = CW_Constant;
17766 }
17767 break;
17768 case 'G':
17769 case 'C':
17770 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17771 weight = CW_Constant;
17772 }
17773 break;
17774 case 'e':
17775 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17776 if ((C->getSExtValue() >= -0x80000000LL) &&
17777 (C->getSExtValue() <= 0x7fffffffLL))
17778 weight = CW_Constant;
17779 }
17780 break;
17781 case 'Z':
17782 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17783 if (C->getZExtValue() <= 0xffffffff)
17784 weight = CW_Constant;
17785 }
17786 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017787 }
17788 return weight;
17789}
17790
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017791/// LowerXConstraint - try to replace an X constraint, which matches anything,
17792/// with another that has more specific requirements based on the type of the
17793/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017794const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017795LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017796 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17797 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017798 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017799 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017800 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017801 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017802 return "x";
17803 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017804
Chris Lattner5e764232008-04-26 23:02:14 +000017805 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017806}
17807
Chris Lattner48884cd2007-08-25 00:47:38 +000017808/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17809/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017810void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017811 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017812 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017813 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017814 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017815
Eric Christopher100c8332011-06-02 23:16:42 +000017816 // Only support length 1 constraints for now.
17817 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017818
Eric Christopher100c8332011-06-02 23:16:42 +000017819 char ConstraintLetter = Constraint[0];
17820 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017821 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017822 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017823 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017824 if (C->getZExtValue() <= 31) {
17825 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017826 break;
17827 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017828 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017829 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017830 case 'J':
17831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017832 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017833 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17834 break;
17835 }
17836 }
17837 return;
17838 case 'K':
17839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017840 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017841 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17842 break;
17843 }
17844 }
17845 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017846 case 'N':
17847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017848 if (C->getZExtValue() <= 255) {
17849 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017850 break;
17851 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017852 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017853 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017854 case 'e': {
17855 // 32-bit signed value
17856 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017857 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17858 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017859 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017860 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017861 break;
17862 }
17863 // FIXME gcc accepts some relocatable values here too, but only in certain
17864 // memory models; it's complicated.
17865 }
17866 return;
17867 }
17868 case 'Z': {
17869 // 32-bit unsigned value
17870 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017871 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17872 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017873 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17874 break;
17875 }
17876 }
17877 // FIXME gcc accepts some relocatable values here too, but only in certain
17878 // memory models; it's complicated.
17879 return;
17880 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017881 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017882 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017883 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017884 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017885 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017886 break;
17887 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017888
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017889 // In any sort of PIC mode addresses need to be computed at runtime by
17890 // adding in a register or some sort of table lookup. These can't
17891 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017892 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017893 return;
17894
Chris Lattnerdc43a882007-05-03 16:52:29 +000017895 // If we are in non-pic codegen mode, we allow the address of a global (with
17896 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017897 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017898 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017899
Chris Lattner49921962009-05-08 18:23:14 +000017900 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17901 while (1) {
17902 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17903 Offset += GA->getOffset();
17904 break;
17905 } else if (Op.getOpcode() == ISD::ADD) {
17906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17907 Offset += C->getZExtValue();
17908 Op = Op.getOperand(0);
17909 continue;
17910 }
17911 } else if (Op.getOpcode() == ISD::SUB) {
17912 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17913 Offset += -C->getZExtValue();
17914 Op = Op.getOperand(0);
17915 continue;
17916 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017917 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017918
Chris Lattner49921962009-05-08 18:23:14 +000017919 // Otherwise, this isn't something we can handle, reject it.
17920 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017921 }
Eric Christopherfd179292009-08-27 18:07:15 +000017922
Dan Gohman46510a72010-04-15 01:51:59 +000017923 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017924 // If we require an extra load to get this address, as in PIC mode, we
17925 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017926 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17927 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017928 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017929
Devang Patel0d881da2010-07-06 22:08:15 +000017930 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17931 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017932 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017933 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017934 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017935
Gabor Greifba36cb52008-08-28 21:40:38 +000017936 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017937 Ops.push_back(Result);
17938 return;
17939 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017940 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017941}
17942
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017943std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017944X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017945 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017946 // First, see if this is a constraint that directly corresponds to an LLVM
17947 // register class.
17948 if (Constraint.size() == 1) {
17949 // GCC Constraint Letters
17950 switch (Constraint[0]) {
17951 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017952 // TODO: Slight differences here in allocation order and leaving
17953 // RIP in the class. Do they matter any more here than they do
17954 // in the normal allocation?
17955 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17956 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017957 if (VT == MVT::i32 || VT == MVT::f32)
17958 return std::make_pair(0U, &X86::GR32RegClass);
17959 if (VT == MVT::i16)
17960 return std::make_pair(0U, &X86::GR16RegClass);
17961 if (VT == MVT::i8 || VT == MVT::i1)
17962 return std::make_pair(0U, &X86::GR8RegClass);
17963 if (VT == MVT::i64 || VT == MVT::f64)
17964 return std::make_pair(0U, &X86::GR64RegClass);
17965 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017966 }
17967 // 32-bit fallthrough
17968 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017969 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017970 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17971 if (VT == MVT::i16)
17972 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17973 if (VT == MVT::i8 || VT == MVT::i1)
17974 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17975 if (VT == MVT::i64)
17976 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017977 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017978 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017979 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017980 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017981 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017982 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017983 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017984 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017985 return std::make_pair(0U, &X86::GR32RegClass);
17986 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017987 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017988 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017989 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017990 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017991 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017992 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017993 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17994 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017995 case 'f': // FP Stack registers.
17996 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17997 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017998 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017999 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018000 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018001 return std::make_pair(0U, &X86::RFP64RegClass);
18002 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018003 case 'y': // MMX_REGS if MMX allowed.
18004 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018005 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018006 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018007 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018008 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018009 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018010 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018011
Owen Anderson825b72b2009-08-11 20:47:22 +000018012 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018013 default: break;
18014 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018015 case MVT::f32:
18016 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018017 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018018 case MVT::f64:
18019 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018020 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018021 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018022 case MVT::v16i8:
18023 case MVT::v8i16:
18024 case MVT::v4i32:
18025 case MVT::v2i64:
18026 case MVT::v4f32:
18027 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018028 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018029 // AVX types.
18030 case MVT::v32i8:
18031 case MVT::v16i16:
18032 case MVT::v8i32:
18033 case MVT::v4i64:
18034 case MVT::v8f32:
18035 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018036 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018037 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018038 break;
18039 }
18040 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018041
Chris Lattnerf76d1802006-07-31 23:26:50 +000018042 // Use the default implementation in TargetLowering to convert the register
18043 // constraint into a member of a register class.
18044 std::pair<unsigned, const TargetRegisterClass*> Res;
18045 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018046
18047 // Not found as a standard register?
18048 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018049 // Map st(0) -> st(7) -> ST0
18050 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18051 tolower(Constraint[1]) == 's' &&
18052 tolower(Constraint[2]) == 't' &&
18053 Constraint[3] == '(' &&
18054 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18055 Constraint[5] == ')' &&
18056 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018057
Chris Lattner56d77c72009-09-13 22:41:48 +000018058 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018059 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018060 return Res;
18061 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018062
Chris Lattner56d77c72009-09-13 22:41:48 +000018063 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018064 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018065 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018066 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018067 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018068 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018069
18070 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018071 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018072 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018073 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018074 return Res;
18075 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018076
Dale Johannesen330169f2008-11-13 21:52:36 +000018077 // 'A' means EAX + EDX.
18078 if (Constraint == "A") {
18079 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018080 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018081 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018082 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018083 return Res;
18084 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018085
Chris Lattnerf76d1802006-07-31 23:26:50 +000018086 // Otherwise, check to see if this is a register class of the wrong value
18087 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18088 // turn into {ax},{dx}.
18089 if (Res.second->hasType(VT))
18090 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018091
Chris Lattnerf76d1802006-07-31 23:26:50 +000018092 // All of the single-register GCC register classes map their values onto
18093 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18094 // really want an 8-bit or 32-bit register, map to the appropriate register
18095 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018096 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000018097 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018098 unsigned DestReg = 0;
18099 switch (Res.first) {
18100 default: break;
18101 case X86::AX: DestReg = X86::AL; break;
18102 case X86::DX: DestReg = X86::DL; break;
18103 case X86::CX: DestReg = X86::CL; break;
18104 case X86::BX: DestReg = X86::BL; break;
18105 }
18106 if (DestReg) {
18107 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018108 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018109 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018110 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018111 unsigned DestReg = 0;
18112 switch (Res.first) {
18113 default: break;
18114 case X86::AX: DestReg = X86::EAX; break;
18115 case X86::DX: DestReg = X86::EDX; break;
18116 case X86::CX: DestReg = X86::ECX; break;
18117 case X86::BX: DestReg = X86::EBX; break;
18118 case X86::SI: DestReg = X86::ESI; break;
18119 case X86::DI: DestReg = X86::EDI; break;
18120 case X86::BP: DestReg = X86::EBP; break;
18121 case X86::SP: DestReg = X86::ESP; break;
18122 }
18123 if (DestReg) {
18124 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018125 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018126 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018127 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018128 unsigned DestReg = 0;
18129 switch (Res.first) {
18130 default: break;
18131 case X86::AX: DestReg = X86::RAX; break;
18132 case X86::DX: DestReg = X86::RDX; break;
18133 case X86::CX: DestReg = X86::RCX; break;
18134 case X86::BX: DestReg = X86::RBX; break;
18135 case X86::SI: DestReg = X86::RSI; break;
18136 case X86::DI: DestReg = X86::RDI; break;
18137 case X86::BP: DestReg = X86::RBP; break;
18138 case X86::SP: DestReg = X86::RSP; break;
18139 }
18140 if (DestReg) {
18141 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018142 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018143 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018144 }
Craig Topperc9099502012-04-20 06:31:50 +000018145 } else if (Res.second == &X86::FR32RegClass ||
18146 Res.second == &X86::FR64RegClass ||
18147 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018148 // Handle references to XMM physical registers that got mapped into the
18149 // wrong class. This can happen with constraints like {xmm0} where the
18150 // target independent register mapper will just pick the first match it can
18151 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018152
18153 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018154 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018155 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018156 Res.second = &X86::FR64RegClass;
18157 else if (X86::VR128RegClass.hasType(VT))
18158 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018159 else if (X86::VR256RegClass.hasType(VT))
18160 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018161 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018162
Chris Lattnerf76d1802006-07-31 23:26:50 +000018163 return Res;
18164}