blob: 0952350d07e094f761bb5be41339f9ea67245953 [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 Gurd9a2cfff2013-03-04 18:13:57 +0000184 // Bypass expensive divides 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 Gurd9a2cfff2013-03-04 18:13:57 +0000187 if (Subtarget->is64Bit())
188 addBypassSlowDiv(64, 16);
189 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000190
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000191 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000192 // Setup Windows compiler runtime calls.
193 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000194 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000195 setLibcallName(RTLIB::SREM_I64, "_allrem");
196 setLibcallName(RTLIB::UREM_I64, "_aullrem");
197 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000198 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000199 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000200 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
201 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
202 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000203
204 // The _ftol2 runtime function has an unusual calling conv, which
205 // is modeled by a special pseudo-instruction.
206 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
208 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
209 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000226 addRegisterClass(MVT::i8, &X86::GR8RegClass);
227 addRegisterClass(MVT::i16, &X86::GR16RegClass);
228 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000230 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000259 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000273 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000316 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000329 if (isTargetFTOL()) {
330 // Use the _ftol2 runtime function, which has a pseudo-instruction
331 // to handle its weird calling convention.
332 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
333 }
334
Chris Lattner399610a2006-12-05 18:22:22 +0000335 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000336 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
338 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000339 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000341 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000342 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000343 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000344 }
Chris Lattner21f66852005-12-23 05:15:23 +0000345
Dan Gohmanb00ee212008-02-18 19:34:53 +0000346 // Scalar integer divide and remainder are lowered to use operations that
347 // produce two results, to match the available instructions. This exposes
348 // the two-result form to trivial CSE, which is able to combine x/y and x%y
349 // into a single instruction.
350 //
351 // Scalar integer multiply-high is also lowered to use two-result
352 // operations, to match the available instructions. However, plain multiply
353 // (low) operations are left as Legal, as there are single-result
354 // instructions for this in x86. Using the two-result multiply instructions
355 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000356 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000357 MVT VT = IntVTs[i];
358 setOperationAction(ISD::MULHS, VT, Expand);
359 setOperationAction(ISD::MULHU, VT, Expand);
360 setOperationAction(ISD::SDIV, VT, Expand);
361 setOperationAction(ISD::UDIV, VT, Expand);
362 setOperationAction(ISD::SREM, VT, Expand);
363 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000364
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000365 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000366 setOperationAction(ISD::ADDC, VT, Custom);
367 setOperationAction(ISD::ADDE, VT, Custom);
368 setOperationAction(ISD::SUBC, VT, Custom);
369 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000370 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000371
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
373 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
374 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
375 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000376 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
379 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
380 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
381 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
382 setOperationAction(ISD::FREM , MVT::f32 , Expand);
383 setOperationAction(ISD::FREM , MVT::f64 , Expand);
384 setOperationAction(ISD::FREM , MVT::f80 , Expand);
385 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000386
Chandler Carruth77821022011-12-24 12:12:34 +0000387 // Promote the i8 variants and force them on up to i32 which has a shorter
388 // encoding.
389 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
391 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
392 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000393 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
396 if (Subtarget->is64Bit())
397 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000398 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000399 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
400 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
401 if (Subtarget->is64Bit())
402 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
403 }
Craig Topper37f21672011-10-11 06:44:02 +0000404
405 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000406 // When promoting the i8 variants, force them to i32 for a shorter
407 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000408 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000409 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
411 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
414 if (Subtarget->is64Bit())
415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000416 } else {
417 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
421 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
423 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000424 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000425 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
426 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000427 }
428
Benjamin Kramer1292c222010-12-04 20:32:23 +0000429 if (Subtarget->hasPOPCNT()) {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
431 } else {
432 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
433 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
434 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
435 if (Subtarget->is64Bit())
436 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
437 }
438
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
440 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000441
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000442 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000444 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000445 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000446 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
449 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
450 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
451 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000452 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
455 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
456 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000459 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000460 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
463 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000464 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000465 // other SjLj exception interfaces are implemented and please don't build
466 // your own exception handling based on them.
467 // LLVM/Clang supports zero-cost DWARF exception handling.
468 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
469 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000470
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000471 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
473 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
474 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
475 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000476 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
478 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000479 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000480 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
482 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
483 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
484 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000485 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000486 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000487 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
489 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
490 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000491 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
493 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
494 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000495 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000496
Craig Topper1accb7e2012-01-10 06:54:16 +0000497 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000499
Eric Christopher9a9d2752010-07-22 02:48:34 +0000500 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000501 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000502
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000503 // On X86 and X86-64, atomic operations are lowered to locked instructions.
504 // Locked instructions, in turn, have implicit fence semantics (all memory
505 // operations are flushed before issuing the locked instruction, and they
506 // are not buffered), so we can fold away the common pattern of
507 // fence-atomic-fence.
508 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000509
Mon P Wang63307c32008-05-05 19:05:59 +0000510 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000511 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000512 MVT VT = IntVTs[i];
513 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000515 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000516 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000517
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000518 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000519 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
524 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000527 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
528 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
529 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
530 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000531 }
532
Eli Friedman43f51ae2011-08-26 21:21:21 +0000533 if (Subtarget->hasCmpxchg16b()) {
534 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
535 }
536
Evan Cheng3c992d22006-03-07 02:02:57 +0000537 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000538 if (!Subtarget->isTargetDarwin() &&
539 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000540 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000542 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
545 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
546 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
547 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000548 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000549 setExceptionPointerRegister(X86::RAX);
550 setExceptionSelectorRegister(X86::RDX);
551 } else {
552 setExceptionPointerRegister(X86::EAX);
553 setExceptionSelectorRegister(X86::EDX);
554 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
556 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000557
Duncan Sands4a544a72011-09-06 13:37:06 +0000558 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
559 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000560
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000562 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000563
Nate Begemanacc398c2006-01-25 18:21:52 +0000564 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VASTART , MVT::Other, Custom);
566 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Custom);
569 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::VAARG , MVT::Other, Expand);
572 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000573 }
Evan Chengae642192007-03-02 23:16:35 +0000574
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
576 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000577
578 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000581 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Custom);
584 else
585 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
586 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000587
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000588 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000591 addRegisterClass(MVT::f32, &X86::FR32RegClass);
592 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000593
Evan Cheng223547a2006-01-31 22:28:30 +0000594 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FABS , MVT::f64, Custom);
596 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000597
598 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::FNEG , MVT::f64, Custom);
600 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000601
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
604 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000605
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000606 // Lower this to FGETSIGNx86 plus an AND.
607 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
608 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
609
Evan Chengd25e9e82006-02-02 00:28:23 +0000610 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000611 setOperationAction(ISD::FSIN , MVT::f64, Expand);
612 setOperationAction(ISD::FCOS , MVT::f64, Expand);
613 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
614 setOperationAction(ISD::FSIN , MVT::f32, Expand);
615 setOperationAction(ISD::FCOS , MVT::f32, Expand);
616 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617
Chris Lattnera54aa942006-01-29 06:26:08 +0000618 // Expand FP immediates into loads from the stack, except for the special
619 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 addLegalFPImmediate(APFloat(+0.0)); // xorpd
621 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000622 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 // Use SSE for f32, x87 for f64.
624 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000625 addRegisterClass(MVT::f32, &X86::FR32RegClass);
626 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000627
628 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000633
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635
636 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
638 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639
640 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000641 setOperationAction(ISD::FSIN , MVT::f32, Expand);
642 setOperationAction(ISD::FCOS , MVT::f32, Expand);
643 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000644
Nate Begemane1795842008-02-14 08:57:00 +0000645 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000646 addLegalFPImmediate(APFloat(+0.0f)); // xorps
647 addLegalFPImmediate(APFloat(+0.0)); // FLD0
648 addLegalFPImmediate(APFloat(+1.0)); // FLD1
649 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
650 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
651
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000652 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000653 setOperationAction(ISD::FSIN , MVT::f64, Expand);
654 setOperationAction(ISD::FCOS , MVT::f64, Expand);
655 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000656 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000657 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000658 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000659 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000660 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
661 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
664 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
665 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
666 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000667
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000669 setOperationAction(ISD::FSIN , MVT::f64, Expand);
670 setOperationAction(ISD::FSIN , MVT::f32, Expand);
671 setOperationAction(ISD::FCOS , MVT::f64, Expand);
672 setOperationAction(ISD::FCOS , MVT::f32, Expand);
673 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
674 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000676 addLegalFPImmediate(APFloat(+0.0)); // FLD0
677 addLegalFPImmediate(APFloat(+1.0)); // FLD1
678 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
679 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000680 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
681 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
682 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
683 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000684 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000685
Cameron Zwarich33390842011-07-08 21:39:21 +0000686 // We don't support FMA.
687 setOperationAction(ISD::FMA, MVT::f64, Expand);
688 setOperationAction(ISD::FMA, MVT::f32, Expand);
689
Dale Johannesen59a58732007-08-05 18:49:15 +0000690 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000691 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000692 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
694 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000695 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000696 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000697 addLegalFPImmediate(TmpFlt); // FLD0
698 TmpFlt.changeSign();
699 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000700
701 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000702 APFloat TmpFlt2(+1.0);
703 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
704 &ignored);
705 addLegalFPImmediate(TmpFlt2); // FLD1
706 TmpFlt2.changeSign();
707 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
708 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000709
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000710 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000711 setOperationAction(ISD::FSIN , MVT::f80, Expand);
712 setOperationAction(ISD::FCOS , MVT::f80, Expand);
713 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000714 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000715
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000716 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
717 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
718 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
719 setOperationAction(ISD::FRINT, MVT::f80, Expand);
720 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000721 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000722 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000723
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000724 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
726 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
727 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000728
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 setOperationAction(ISD::FLOG, MVT::f80, Expand);
730 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
731 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
732 setOperationAction(ISD::FEXP, MVT::f80, Expand);
733 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000734
Mon P Wangf007a8b2008-11-06 05:31:54 +0000735 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000736 // (for widening) or expand (for scalarization). Then we will selectively
737 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000738 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
739 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000740 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000741 setOperationAction(ISD::ADD , VT, Expand);
742 setOperationAction(ISD::SUB , VT, Expand);
743 setOperationAction(ISD::FADD, VT, Expand);
744 setOperationAction(ISD::FNEG, VT, Expand);
745 setOperationAction(ISD::FSUB, VT, Expand);
746 setOperationAction(ISD::MUL , VT, Expand);
747 setOperationAction(ISD::FMUL, VT, Expand);
748 setOperationAction(ISD::SDIV, VT, Expand);
749 setOperationAction(ISD::UDIV, VT, Expand);
750 setOperationAction(ISD::FDIV, VT, Expand);
751 setOperationAction(ISD::SREM, VT, Expand);
752 setOperationAction(ISD::UREM, VT, Expand);
753 setOperationAction(ISD::LOAD, VT, Expand);
754 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
755 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
756 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
757 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
758 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
759 setOperationAction(ISD::FABS, VT, Expand);
760 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000761 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000763 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000764 setOperationAction(ISD::FREM, VT, Expand);
765 setOperationAction(ISD::FMA, VT, Expand);
766 setOperationAction(ISD::FPOWI, VT, Expand);
767 setOperationAction(ISD::FSQRT, VT, Expand);
768 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
769 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000770 setOperationAction(ISD::FCEIL, VT, Expand);
771 setOperationAction(ISD::FTRUNC, VT, Expand);
772 setOperationAction(ISD::FRINT, VT, Expand);
773 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000774 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
775 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
776 setOperationAction(ISD::SDIVREM, VT, Expand);
777 setOperationAction(ISD::UDIVREM, VT, Expand);
778 setOperationAction(ISD::FPOW, VT, Expand);
779 setOperationAction(ISD::CTPOP, VT, Expand);
780 setOperationAction(ISD::CTTZ, VT, Expand);
781 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
782 setOperationAction(ISD::CTLZ, VT, Expand);
783 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
784 setOperationAction(ISD::SHL, VT, Expand);
785 setOperationAction(ISD::SRA, VT, Expand);
786 setOperationAction(ISD::SRL, VT, Expand);
787 setOperationAction(ISD::ROTL, VT, Expand);
788 setOperationAction(ISD::ROTR, VT, Expand);
789 setOperationAction(ISD::BSWAP, VT, Expand);
790 setOperationAction(ISD::SETCC, VT, Expand);
791 setOperationAction(ISD::FLOG, VT, Expand);
792 setOperationAction(ISD::FLOG2, VT, Expand);
793 setOperationAction(ISD::FLOG10, VT, Expand);
794 setOperationAction(ISD::FEXP, VT, Expand);
795 setOperationAction(ISD::FEXP2, VT, Expand);
796 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
797 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
798 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
799 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
800 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
801 setOperationAction(ISD::TRUNCATE, VT, Expand);
802 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
803 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
804 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
805 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000806 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
807 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000808 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000809 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000810 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
811 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
812 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000813 }
814
Evan Chengc7ce29b2009-02-13 22:36:38 +0000815 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
816 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000817 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000818 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000819 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820 }
821
Dale Johannesen0488fb62010-09-30 23:57:10 +0000822 // MMX-sized vectors (other than x86mmx) are expected to be expanded
823 // into smaller operations.
824 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
825 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
826 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
827 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
828 setOperationAction(ISD::AND, MVT::v8i8, Expand);
829 setOperationAction(ISD::AND, MVT::v4i16, Expand);
830 setOperationAction(ISD::AND, MVT::v2i32, Expand);
831 setOperationAction(ISD::AND, MVT::v1i64, Expand);
832 setOperationAction(ISD::OR, MVT::v8i8, Expand);
833 setOperationAction(ISD::OR, MVT::v4i16, Expand);
834 setOperationAction(ISD::OR, MVT::v2i32, Expand);
835 setOperationAction(ISD::OR, MVT::v1i64, Expand);
836 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
837 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
838 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
839 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
840 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
841 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
845 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
846 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
847 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
848 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000849 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
850 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
851 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
852 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000853
Craig Topper1accb7e2012-01-10 06:54:16 +0000854 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000855 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000856
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
858 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
859 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
860 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
861 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
862 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000863 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
865 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
866 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
868 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000869 }
870
Craig Topper1accb7e2012-01-10 06:54:16 +0000871 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000872 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000873
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000874 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
875 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000876 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
877 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
878 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
879 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000880
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
882 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
883 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
884 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000885 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
887 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
888 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
889 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
890 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
891 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
892 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
893 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
894 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
895 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
896 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
897 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000898 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Nadav Rotem354efd82011-09-18 14:57:03 +0000900 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000901 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
902 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
903 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000904
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
906 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
907 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
908 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
909 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000910
Evan Cheng2c3ae372006-04-12 21:21:57 +0000911 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000912 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000914 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000915 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000916 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000917 // Do not attempt to custom lower non-128-bit vectors
918 if (!VT.is128BitVector())
919 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000920 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
921 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000923 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000924
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
926 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
927 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
928 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
929 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
930 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000931
Nate Begemancdd1eec2008-02-12 22:51:28 +0000932 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
934 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000935 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000936
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000937 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000938 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000939 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000940
941 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000942 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000943 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000944
Craig Topper0d1f1762012-08-12 00:34:56 +0000945 setOperationAction(ISD::AND, VT, Promote);
946 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
947 setOperationAction(ISD::OR, VT, Promote);
948 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
949 setOperationAction(ISD::XOR, VT, Promote);
950 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
951 setOperationAction(ISD::LOAD, VT, Promote);
952 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
953 setOperationAction(ISD::SELECT, VT, Promote);
954 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000955 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000956
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000958
Evan Cheng2c3ae372006-04-12 21:21:57 +0000959 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
961 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
962 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
963 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
966 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000967
Michael Liaoa7554632012-10-23 17:36:08 +0000968 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
969 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000970 // As there is no 64-bit GPR available, we need build a special custom
971 // sequence to convert from v2i32 to v2f32.
972 if (!Subtarget->is64Bit())
973 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000974
Michael Liao9d796db2012-10-10 16:32:15 +0000975 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000976 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000977
Michael Liaob8150d82012-09-10 18:33:51 +0000978 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000979 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000980
Craig Topperd0a31172012-01-10 06:37:29 +0000981 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000982 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
983 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
984 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
985 setOperationAction(ISD::FRINT, MVT::f32, Legal);
986 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
987 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
988 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
989 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
990 setOperationAction(ISD::FRINT, MVT::f64, Legal);
991 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
992
Craig Topper12fb5c62012-09-08 17:42:27 +0000993 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000994 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
995 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
996 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
997 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000998 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000999 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1000 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1001 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1002 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001003
Nate Begeman14d12ca2008-02-11 04:19:36 +00001004 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001006
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001007 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1008 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1009 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1010 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1011 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001012
Nate Begeman14d12ca2008-02-11 04:19:36 +00001013 // i8 and i16 vectors are custom , because the source register and source
1014 // source memory operand types are not the same width. f32 vectors are
1015 // custom since the immediate controlling the insert encodes additional
1016 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1018 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1019 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1020 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001021
Owen Anderson825b72b2009-08-11 20:47:22 +00001022 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1023 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1024 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1025 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001026
Pete Coopera77214a2011-11-14 19:38:42 +00001027 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001028 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001029 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001030 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1031 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001032 }
1033 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001034
Craig Topper1accb7e2012-01-10 06:54:16 +00001035 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001036 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001037 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001038
Nadav Rotem43012222011-05-11 08:12:09 +00001039 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001040 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001041
Nadav Rotem43012222011-05-11 08:12:09 +00001042 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001043 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001044
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001045 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001046 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1047 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1048
1049 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1050 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1051
1052 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1053 } else {
1054 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1055 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1056
1057 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1058 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1059
1060 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1061 }
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001062 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1063 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001064 }
1065
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001066 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001067 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1068 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1069 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1070 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1071 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1072 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001073
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1076 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001077
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1079 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1080 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1081 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1082 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001083 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001084 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1085 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1086 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1087 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001089 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001090
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1092 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1093 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1094 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1095 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001096 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001097 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1098 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1099 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1100 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001102 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001103
Michael Liaobedcbd42012-10-16 18:14:11 +00001104 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001105 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001106
1107 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1108
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001109 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1110 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001111 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001112
Michael Liaoa7554632012-10-23 17:36:08 +00001113 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1114 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1115 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1116
Michael Liaob8150d82012-09-10 18:33:51 +00001117 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1118
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001119 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1120 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1121
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001122 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1123 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1124
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001125 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001126 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001127
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001128 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1129
Duncan Sands28b77e92011-09-06 19:07:46 +00001130 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1131 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1132 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1133 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001134
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001135 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1136 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1137 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1138
Craig Topperaaa643c2011-11-09 07:28:55 +00001139 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1140 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1141 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1142 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001143
Nadav Rotem0509db22012-12-28 05:45:24 +00001144 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1145 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1146 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1147 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1148 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1149 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001150
Craig Topperbf404372012-08-31 15:40:30 +00001151 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001152 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1153 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1154 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1155 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1156 setOperationAction(ISD::FMA, MVT::f32, Legal);
1157 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001158 }
Craig Topper880ef452012-08-11 22:34:26 +00001159
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001160 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001161 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1162 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1163 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1164 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001165
Craig Topperaaa643c2011-11-09 07:28:55 +00001166 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1167 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1168 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1169 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001170
Craig Topperaaa643c2011-11-09 07:28:55 +00001171 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1172 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1173 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001174 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001175
1176 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001177
1178 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1179 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1180
1181 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1182 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1183
1184 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001185
1186 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001187 } else {
1188 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1189 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1190 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1191 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1192
1193 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1194 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1195 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1196 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1197
1198 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1199 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1200 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1201 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001202
1203 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1204 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1205
1206 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1207 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1208
1209 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001210 }
Craig Topper13894fa2011-08-24 06:14:18 +00001211
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001212 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001213 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1214 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001215 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001216
1217 // Extract subvector is special because the value type
1218 // (result) is 128-bit but the source is 256-bit wide.
1219 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001220 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001221
1222 // Do not attempt to custom lower other non-256-bit vectors
1223 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001224 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001225
Craig Topper0d1f1762012-08-12 00:34:56 +00001226 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1227 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1228 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1229 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1230 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1231 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1232 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001233 }
1234
David Greene54d8eba2011-01-27 22:38:56 +00001235 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001236 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001237 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001238
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001239 // Do not attempt to promote non-256-bit vectors
1240 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001241 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001242
Craig Topper0d1f1762012-08-12 00:34:56 +00001243 setOperationAction(ISD::AND, VT, Promote);
1244 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1245 setOperationAction(ISD::OR, VT, Promote);
1246 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1247 setOperationAction(ISD::XOR, VT, Promote);
1248 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1249 setOperationAction(ISD::LOAD, VT, Promote);
1250 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1251 setOperationAction(ISD::SELECT, VT, Promote);
1252 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001253 }
David Greene9b9838d2009-06-29 16:47:10 +00001254 }
1255
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001256 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1257 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001258 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1259 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001260 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1261 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001262 }
1263
Evan Cheng6be2c582006-04-05 23:38:46 +00001264 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001265 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001266 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001267
Eli Friedman962f5492010-06-02 19:35:46 +00001268 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1269 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001270 //
Eli Friedman962f5492010-06-02 19:35:46 +00001271 // FIXME: We really should do custom legalization for addition and
1272 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1273 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001274 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1275 // Add/Sub/Mul with overflow operations are custom lowered.
1276 MVT VT = IntVTs[i];
1277 setOperationAction(ISD::SADDO, VT, Custom);
1278 setOperationAction(ISD::UADDO, VT, Custom);
1279 setOperationAction(ISD::SSUBO, VT, Custom);
1280 setOperationAction(ISD::USUBO, VT, Custom);
1281 setOperationAction(ISD::SMULO, VT, Custom);
1282 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001283 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001284
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001285 // There are no 8-bit 3-address imul/mul instructions
1286 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1287 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001288
Evan Chengd54f2d52009-03-31 19:38:51 +00001289 if (!Subtarget->is64Bit()) {
1290 // These libcalls are not available in 32-bit.
1291 setLibcallName(RTLIB::SHL_I128, 0);
1292 setLibcallName(RTLIB::SRL_I128, 0);
1293 setLibcallName(RTLIB::SRA_I128, 0);
1294 }
1295
Evan Cheng8688a582013-01-29 02:32:37 +00001296 // Combine sin / cos into one node or libcall if possible.
1297 if (Subtarget->hasSinCos()) {
1298 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1299 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001300 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001301 // For MacOSX, we don't want to the normal expansion of a libcall to
1302 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1303 // traffic.
1304 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1305 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1306 }
1307 }
1308
Evan Cheng206ee9d2006-07-07 08:33:52 +00001309 // We have target-specific dag combine patterns for the following nodes:
1310 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001311 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001312 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001313 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001314 setTargetDAGCombine(ISD::SHL);
1315 setTargetDAGCombine(ISD::SRA);
1316 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001317 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001318 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001319 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001320 setTargetDAGCombine(ISD::FADD);
1321 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001322 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001323 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001324 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001325 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001326 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001327 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001328 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001329 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001330 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001331 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001332 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001333 if (Subtarget->is64Bit())
1334 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001335 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001336
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001337 computeRegisterProperties();
1338
Evan Cheng05219282011-01-06 06:52:41 +00001339 // On Darwin, -Os means optimize for size without hurting performance,
1340 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001341 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1342 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1343 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1344 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1345 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1346 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001347 setPrefLoopAlignment(4); // 2^4 bytes.
Jim Grosbach3450f802013-02-20 21:13:59 +00001348 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001349
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001350 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001351 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001352
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001353 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001354}
1355
Duncan Sands28b77e92011-09-06 19:07:46 +00001356EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1357 if (!VT.isVector()) return MVT::i8;
1358 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001359}
1360
Evan Cheng29286502008-01-23 23:17:41 +00001361/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1362/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001363static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001364 if (MaxAlign == 16)
1365 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001366 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001367 if (VTy->getBitWidth() == 128)
1368 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001369 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001370 unsigned EltAlign = 0;
1371 getMaxByValAlign(ATy->getElementType(), EltAlign);
1372 if (EltAlign > MaxAlign)
1373 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001374 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001375 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1376 unsigned EltAlign = 0;
1377 getMaxByValAlign(STy->getElementType(i), EltAlign);
1378 if (EltAlign > MaxAlign)
1379 MaxAlign = EltAlign;
1380 if (MaxAlign == 16)
1381 break;
1382 }
1383 }
Evan Cheng29286502008-01-23 23:17:41 +00001384}
1385
1386/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1387/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001388/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1389/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001390unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001391 if (Subtarget->is64Bit()) {
1392 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001393 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001394 if (TyAlign > 8)
1395 return TyAlign;
1396 return 8;
1397 }
1398
Evan Cheng29286502008-01-23 23:17:41 +00001399 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001400 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001401 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001402 return Align;
1403}
Chris Lattner2b02a442007-02-25 08:29:00 +00001404
Evan Chengf0df0312008-05-15 08:39:06 +00001405/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001406/// and store operations as a result of memset, memcpy, and memmove
1407/// lowering. If DstAlign is zero that means it's safe to destination
1408/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1409/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001410/// probably because the source does not need to be loaded. If 'IsMemset' is
1411/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1412/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1413/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001414/// It returns EVT::Other if the type should be determined using generic
1415/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001416EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001417X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1418 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001419 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001420 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001421 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001422 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001423 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001424 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1425 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001426 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001427 (Subtarget->isUnalignedMemAccessFast() ||
1428 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001429 (SrcAlign == 0 || SrcAlign >= 16)))) {
1430 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001431 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001432 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001433 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001434 return MVT::v8f32;
1435 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001436 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001437 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001438 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001439 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001440 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001441 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001442 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001443 // Do not use f64 to lower memcpy if source is string constant. It's
1444 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001445 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001446 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001447 }
Evan Chengf0df0312008-05-15 08:39:06 +00001448 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 return MVT::i64;
1450 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001451}
1452
Evan Cheng7d342672012-12-12 01:32:07 +00001453bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001454 if (VT == MVT::f32)
1455 return X86ScalarSSEf32;
1456 else if (VT == MVT::f64)
1457 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001458 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001459}
1460
Evan Cheng376642e2012-12-10 23:21:26 +00001461bool
1462X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1463 if (Fast)
1464 *Fast = Subtarget->isUnalignedMemAccessFast();
1465 return true;
1466}
1467
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001468/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1469/// current function. The returned value is a member of the
1470/// MachineJumpTableInfo::JTEntryKind enum.
1471unsigned X86TargetLowering::getJumpTableEncoding() const {
1472 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1473 // symbol.
1474 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1475 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001476 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001477
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001478 // Otherwise, use the normal jump table encoding heuristics.
1479 return TargetLowering::getJumpTableEncoding();
1480}
1481
Chris Lattnerc64daab2010-01-26 05:02:42 +00001482const MCExpr *
1483X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1484 const MachineBasicBlock *MBB,
1485 unsigned uid,MCContext &Ctx) const{
1486 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1487 Subtarget->isPICStyleGOT());
1488 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1489 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001490 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1491 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001492}
1493
Evan Chengcc415862007-11-09 01:32:10 +00001494/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1495/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001496SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001497 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001498 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001499 // This doesn't have DebugLoc associated with it, but is not really the
1500 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001501 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001502 return Table;
1503}
1504
Chris Lattner589c6f62010-01-26 06:28:43 +00001505/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1506/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1507/// MCExpr.
1508const MCExpr *X86TargetLowering::
1509getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1510 MCContext &Ctx) const {
1511 // X86-64 uses RIP relative addressing based on the jump table label.
1512 if (Subtarget->isPICStyleRIPRel())
1513 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1514
1515 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001516 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001517}
1518
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001519// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001520std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001521X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001522 const TargetRegisterClass *RRC = 0;
1523 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001524 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001525 default:
1526 return TargetLowering::findRepresentativeClass(VT);
1527 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001528 RRC = Subtarget->is64Bit() ?
1529 (const TargetRegisterClass*)&X86::GR64RegClass :
1530 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001531 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001532 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001533 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001534 break;
1535 case MVT::f32: case MVT::f64:
1536 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1537 case MVT::v4f32: case MVT::v2f64:
1538 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1539 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001540 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001541 break;
1542 }
1543 return std::make_pair(RRC, Cost);
1544}
1545
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001546bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1547 unsigned &Offset) const {
1548 if (!Subtarget->isTargetLinux())
1549 return false;
1550
1551 if (Subtarget->is64Bit()) {
1552 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1553 Offset = 0x28;
1554 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1555 AddressSpace = 256;
1556 else
1557 AddressSpace = 257;
1558 } else {
1559 // %gs:0x14 on i386
1560 Offset = 0x14;
1561 AddressSpace = 256;
1562 }
1563 return true;
1564}
1565
Chris Lattner2b02a442007-02-25 08:29:00 +00001566//===----------------------------------------------------------------------===//
1567// Return Value Calling Convention Implementation
1568//===----------------------------------------------------------------------===//
1569
Chris Lattner59ed56b2007-02-28 04:55:35 +00001570#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001571
Michael J. Spencerec38de22010-10-10 22:04:20 +00001572bool
Eric Christopher471e4222011-06-08 23:55:35 +00001573X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001574 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001575 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001576 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001577 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001578 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001579 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001580 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001581}
1582
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583SDValue
1584X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001585 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001587 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001588 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001589 MachineFunction &MF = DAG.getMachineFunction();
1590 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Chris Lattner9774c912007-02-27 05:28:59 +00001592 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001593 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594 RVLocs, *DAG.getContext());
1595 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Dan Gohman475871a2008-07-27 21:46:04 +00001597 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001598 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001599 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1600 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001601 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1602 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001603
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001604 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001605 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1606 CCValAssign &VA = RVLocs[i];
1607 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001608 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001609 EVT ValVT = ValToCopy.getValueType();
1610
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001611 // Promote values to the appropriate types
1612 if (VA.getLocInfo() == CCValAssign::SExt)
1613 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1614 else if (VA.getLocInfo() == CCValAssign::ZExt)
1615 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1616 else if (VA.getLocInfo() == CCValAssign::AExt)
1617 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1618 else if (VA.getLocInfo() == CCValAssign::BCvt)
1619 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1620
Dale Johannesenc4510512010-09-24 19:05:48 +00001621 // If this is x86-64, and we disabled SSE, we can't return FP values,
1622 // or SSE or MMX vectors.
1623 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1624 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001625 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001626 report_fatal_error("SSE register return with SSE disabled");
1627 }
1628 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1629 // llvm-gcc has never done it right and no one has noticed, so this
1630 // should be OK for now.
1631 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001632 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001633 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001634
Chris Lattner447ff682008-03-11 03:23:40 +00001635 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1636 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001637 if (VA.getLocReg() == X86::ST0 ||
1638 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001639 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1640 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001641 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001643 RetOps.push_back(ValToCopy);
1644 // Don't emit a copytoreg.
1645 continue;
1646 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001647
Evan Cheng242b38b2009-02-23 09:03:22 +00001648 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1649 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001650 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001651 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001652 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001653 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001654 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1655 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001656 // If we don't have SSE2 available, convert to v4f32 so the generated
1657 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001658 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001659 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001660 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001661 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001662 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001663
Dale Johannesendd64c412009-02-04 00:33:20 +00001664 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001665 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001666 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001667 }
Dan Gohman61a92132008-04-21 23:59:07 +00001668
Eli Benderskya5597f02013-01-25 22:07:43 +00001669 // The x86-64 ABIs require that for returning structs by value we copy
1670 // the sret argument into %rax/%eax (depending on ABI) for the return.
1671 // We saved the argument into a virtual register in the entry block,
1672 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001673 if (Subtarget->is64Bit() &&
1674 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1675 MachineFunction &MF = DAG.getMachineFunction();
1676 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1677 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001678 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001679 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001680 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001681
Eli Benderskya5597f02013-01-25 22:07:43 +00001682 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1683 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001684 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001685
Eli Benderskya5597f02013-01-25 22:07:43 +00001686 // RAX/EAX now acts like a return value.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001687 RetOps.push_back(DAG.getRegister(RetValReg, MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001688 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattner447ff682008-03-11 03:23:40 +00001690 RetOps[0] = Chain; // Update chain.
1691
1692 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001694 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
1696 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001698}
1699
Evan Chengbf010eb2012-04-10 01:51:00 +00001700bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001701 if (N->getNumValues() != 1)
1702 return false;
1703 if (!N->hasNUsesOfValue(1, 0))
1704 return false;
1705
Evan Chengbf010eb2012-04-10 01:51:00 +00001706 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001707 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001708 if (Copy->getOpcode() == ISD::CopyToReg) {
1709 // If the copy has a glue operand, we conservatively assume it isn't safe to
1710 // perform a tail call.
1711 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1712 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001713 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001714 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001715 return false;
1716
Evan Cheng1bf891a2010-12-01 22:59:46 +00001717 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001719 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001720 if (UI->getOpcode() != X86ISD::RET_FLAG)
1721 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001722 HasRet = true;
1723 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001724
Evan Chengbf010eb2012-04-10 01:51:00 +00001725 if (!HasRet)
1726 return false;
1727
1728 Chain = TCChain;
1729 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001730}
1731
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001732MVT
1733X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001734 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001735 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001736 // TODO: Is this also valid on 32-bit?
1737 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001738 ReturnMVT = MVT::i8;
1739 else
1740 ReturnMVT = MVT::i32;
1741
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001742 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001743 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001744}
1745
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746/// LowerCallResult - Lower the result values of a call into the
1747/// appropriate copies out of appropriate physical registers.
1748///
1749SDValue
1750X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001751 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 const SmallVectorImpl<ISD::InputArg> &Ins,
1753 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001755
Chris Lattnere32bbf62007-02-28 07:09:55 +00001756 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001757 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001758 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001759 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001760 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001762
Chris Lattner3085e152007-02-25 08:59:22 +00001763 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001764 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001765 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001766 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Torok Edwin3f142c32009-02-01 18:15:56 +00001768 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001770 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001771 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001772 }
1773
Evan Cheng79fb3b42009-02-20 20:43:02 +00001774 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001775
1776 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001777 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001778 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001779 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001780 // instead.
1781 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1782 // If we prefer to use the value in xmm registers, copy it out as f80 and
1783 // use a truncate to move it from fp stack reg to xmm reg.
1784 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001785 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001786 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1787 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001788 Val = Chain.getValue(0);
1789
1790 // Round the f80 to the right size, which also moves it to the appropriate
1791 // xmm register.
1792 if (CopyVT != VA.getValVT())
1793 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1794 // This truncation won't change the value.
1795 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001796 } else {
1797 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1798 CopyVT, InFlag).getValue(1);
1799 Val = Chain.getValue(0);
1800 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001801 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001803 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001804
Dan Gohman98ca4f22009-08-05 01:29:28 +00001805 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001806}
1807
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001808//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001809// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001810//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001811// StdCall calling convention seems to be standard for many Windows' API
1812// routines and around. It differs from C calling convention just a little:
1813// callee should clean up the stack, not caller. Symbols should be also
1814// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001815// For info on fast calling convention see Fast Calling Convention (tail call)
1816// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001817
Dan Gohman98ca4f22009-08-05 01:29:28 +00001818/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001819/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001820enum StructReturnType {
1821 NotStructReturn,
1822 RegStructReturn,
1823 StackStructReturn
1824};
1825static StructReturnType
1826callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001828 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001829
Rafael Espindola1cee7102012-07-25 13:41:10 +00001830 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1831 if (!Flags.isSRet())
1832 return NotStructReturn;
1833 if (Flags.isInReg())
1834 return RegStructReturn;
1835 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001836}
1837
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001838/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001839/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001840static StructReturnType
1841argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001843 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001844
Rafael Espindola1cee7102012-07-25 13:41:10 +00001845 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1846 if (!Flags.isSRet())
1847 return NotStructReturn;
1848 if (Flags.isInReg())
1849 return RegStructReturn;
1850 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001851}
1852
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001853/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1854/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001855/// the specific parameter attribute. The copy will be passed as a byval
1856/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001857static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001858CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001859 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1860 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001861 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001862
Dale Johannesendd64c412009-02-04 00:33:20 +00001863 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001864 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001865 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001866}
1867
Chris Lattner29689432010-03-11 00:22:57 +00001868/// IsTailCallConvention - Return true if the calling convention is one that
1869/// supports tail call optimization.
1870static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001871 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1872 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001873}
1874
Evan Cheng485fafc2011-03-21 01:19:09 +00001875bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001876 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001877 return false;
1878
1879 CallSite CS(CI);
1880 CallingConv::ID CalleeCC = CS.getCallingConv();
1881 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1882 return false;
1883
1884 return true;
1885}
1886
Evan Cheng0c439eb2010-01-27 00:07:07 +00001887/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1888/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001889static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1890 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001891 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001892}
1893
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894SDValue
1895X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001896 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001897 const SmallVectorImpl<ISD::InputArg> &Ins,
1898 DebugLoc dl, SelectionDAG &DAG,
1899 const CCValAssign &VA,
1900 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001901 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001902 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001904 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1905 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001906 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001907 EVT ValVT;
1908
1909 // If value is passed by pointer we have address passed instead of the value
1910 // itself.
1911 if (VA.getLocInfo() == CCValAssign::Indirect)
1912 ValVT = VA.getLocVT();
1913 else
1914 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001915
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001916 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001917 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001918 // In case of tail call optimization mark all arguments mutable. Since they
1919 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001920 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001921 unsigned Bytes = Flags.getByValSize();
1922 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1923 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001924 return DAG.getFrameIndex(FI, getPointerTy());
1925 } else {
1926 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001927 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001928 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1929 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001930 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001931 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001932 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001933}
1934
Dan Gohman475871a2008-07-27 21:46:04 +00001935SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001937 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 bool isVarArg,
1939 const SmallVectorImpl<ISD::InputArg> &Ins,
1940 DebugLoc dl,
1941 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001942 SmallVectorImpl<SDValue> &InVals)
1943 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001944 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Gordon Henriksen86737662008-01-05 16:56:59 +00001947 const Function* Fn = MF.getFunction();
1948 if (Fn->hasExternalLinkage() &&
1949 Subtarget->isTargetCygMing() &&
1950 Fn->getName() == "main")
1951 FuncInfo->setForceFramePointer(true);
1952
Evan Cheng1bc78042006-04-26 01:20:17 +00001953 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001955 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001956 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001957
Chris Lattner29689432010-03-11 00:22:57 +00001958 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001959 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001960
Chris Lattner638402b2007-02-28 07:00:42 +00001961 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001962 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001963 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001965
1966 // Allocate shadow area for Win64
1967 if (IsWin64) {
1968 CCInfo.AllocateStack(32, 8);
1969 }
1970
Duncan Sands45907662010-10-31 13:21:44 +00001971 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Chris Lattnerf39f7712007-02-28 05:46:49 +00001973 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001974 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001975 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1976 CCValAssign &VA = ArgLocs[i];
1977 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1978 // places.
1979 assert(VA.getValNo() != LastVal &&
1980 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001981 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001982 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001985 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001986 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001988 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001990 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001994 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001995 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001997 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001999 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002000 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002001 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002002 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003
Devang Patel68e6bee2011-02-21 23:21:26 +00002004 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002006
Chris Lattnerf39f7712007-02-28 05:46:49 +00002007 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2008 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2009 // right size.
2010 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002011 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002012 DAG.getValueType(VA.getValVT()));
2013 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002014 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002015 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002016 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002017 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002019 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002020 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002021 if (RegVT.isVector())
2022 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2023 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002024 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002025 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 } else {
2027 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002029 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002030
2031 // If value is passed via pointer - do a load.
2032 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002033 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002034 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002035
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002037 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002038
Eli Benderskya5597f02013-01-25 22:07:43 +00002039 // The x86-64 ABIs require that for returning structs by value we copy
2040 // the sret argument into %rax/%eax (depending on ABI) for the return.
2041 // Save the argument into a virtual register so that we can access it
2042 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002043 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002044 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2045 unsigned Reg = FuncInfo->getSRetReturnReg();
2046 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002047 MVT PtrTy = getPointerTy();
2048 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002049 FuncInfo->setSRetReturnReg(Reg);
2050 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002051 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002053 }
2054
Chris Lattnerf39f7712007-02-28 05:46:49 +00002055 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002056 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002057 if (FuncIsMadeTailCallSafe(CallConv,
2058 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002059 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002060
Evan Cheng1bc78042006-04-26 01:20:17 +00002061 // If the function takes variable number of arguments, make a frame index for
2062 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002063 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002064 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2065 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002066 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002067 }
2068 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002069 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2070
2071 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002072 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002073 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002075 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002076 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2077 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002078 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2080 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2081 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002082 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002083 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002084
2085 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002086 // The XMM registers which might contain var arg parameters are shadowed
2087 // in their paired GPR. So we only need to save the GPR to their home
2088 // slots.
2089 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002090 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002091 } else {
2092 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2093 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002094
Chad Rosier30450e82011-12-22 22:35:21 +00002095 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2096 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002097 }
2098 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2099 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002100
Bill Wendling831737d2012-12-30 10:32:01 +00002101 bool NoImplicitFloatOps = Fn->getAttributes().
2102 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002103 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002104 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002105 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2106 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002107 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002108 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002109 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002110 // Kernel mode asks for SSE to be disabled, so don't push them
2111 // on the stack.
2112 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002113
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002114 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002115 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002116 // Get to the caller-allocated home save location. Add 8 to account
2117 // for the return address.
2118 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002119 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002120 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002121 // Fixup to set vararg frame on shadow area (4 x i64).
2122 if (NumIntRegs < 4)
2123 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002124 } else {
2125 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002126 // registers, then we must store them to their spots on the stack so
2127 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002128 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2129 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2130 FuncInfo->setRegSaveFrameIndex(
2131 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002132 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002133 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002134
Gordon Henriksen86737662008-01-05 16:56:59 +00002135 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002136 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002137 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2138 getPointerTy());
2139 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002140 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002141 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2142 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002143 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002144 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002147 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002148 MachinePointerInfo::getFixedStack(
2149 FuncInfo->getRegSaveFrameIndex(), Offset),
2150 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002151 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002152 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002154
Dan Gohmanface41a2009-08-16 21:24:25 +00002155 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2156 // Now store the XMM (fp + vector) parameter registers.
2157 SmallVector<SDValue, 11> SaveXMMOps;
2158 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002159
Craig Topperc9099502012-04-20 06:31:50 +00002160 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002161 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2162 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002163
Dan Gohman1e93df62010-04-17 14:41:14 +00002164 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2165 FuncInfo->getRegSaveFrameIndex()));
2166 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2167 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002168
Dan Gohmanface41a2009-08-16 21:24:25 +00002169 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002170 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002171 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002172 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2173 SaveXMMOps.push_back(Val);
2174 }
2175 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2176 MVT::Other,
2177 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002179
2180 if (!MemOps.empty())
2181 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2182 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002185
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002187 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2188 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002189 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002190 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002191 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002192 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002193 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002194 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002195 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002196 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002197
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002199 // RegSaveFrameIndex is X86-64 only.
2200 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002201 if (CallConv == CallingConv::X86_FastCall ||
2202 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002203 // fastcc functions can't have varargs.
2204 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002205 }
Evan Cheng25caf632006-05-23 21:06:34 +00002206
Rafael Espindola76927d752011-08-30 19:39:58 +00002207 FuncInfo->setArgumentStackSize(StackSize);
2208
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002210}
2211
Dan Gohman475871a2008-07-27 21:46:04 +00002212SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2214 SDValue StackPtr, SDValue Arg,
2215 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002216 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002217 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002218 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002219 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002220 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002221 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002222 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002223
2224 return DAG.getStore(Chain, dl, Arg, PtrOff,
2225 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002226 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002227}
2228
Bill Wendling64e87322009-01-16 19:25:27 +00002229/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002230/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002231SDValue
2232X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002233 SDValue &OutRetAddr, SDValue Chain,
2234 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002235 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002236 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002237 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002239
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002241 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002242 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002243 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002244}
2245
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002246/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002247/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002248static SDValue
2249EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002250 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2251 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002252 // Store the return address to the appropriate stack slot.
2253 if (!FPDiff) return Chain;
2254 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002255 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002256 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002257 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002259 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002260 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002261 return Chain;
2262}
2263
Dan Gohman98ca4f22009-08-05 01:29:28 +00002264SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002265X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002266 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002267 SelectionDAG &DAG = CLI.DAG;
2268 DebugLoc &dl = CLI.DL;
2269 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2270 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2271 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2272 SDValue Chain = CLI.Chain;
2273 SDValue Callee = CLI.Callee;
2274 CallingConv::ID CallConv = CLI.CallConv;
2275 bool &isTailCall = CLI.IsTailCall;
2276 bool isVarArg = CLI.IsVarArg;
2277
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 MachineFunction &MF = DAG.getMachineFunction();
2279 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002280 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002281 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002282 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002283 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002284
Nick Lewycky22de16d2012-01-19 00:34:10 +00002285 if (MF.getTarget().Options.DisableTailCalls)
2286 isTailCall = false;
2287
Evan Cheng5f941932010-02-05 02:21:12 +00002288 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002289 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002290 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002291 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002292 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002293 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002294
2295 // Sibcalls are automatically detected tailcalls which do not require
2296 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002297 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002298 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002299
2300 if (isTailCall)
2301 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002302 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002303
Chris Lattner29689432010-03-11 00:22:57 +00002304 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002305 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002306
Chris Lattner638402b2007-02-28 07:00:42 +00002307 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002308 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002309 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002310 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002311
2312 // Allocate shadow area for Win64
2313 if (IsWin64) {
2314 CCInfo.AllocateStack(32, 8);
2315 }
2316
Duncan Sands45907662010-10-31 13:21:44 +00002317 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002318
Chris Lattner423c5f42007-02-28 05:31:48 +00002319 // Get a count of how many bytes are to be pushed on the stack.
2320 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002321 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002322 // This is a sibcall. The memory operands are available in caller's
2323 // own caller's stack.
2324 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002325 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2326 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002327 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002328
Gordon Henriksen86737662008-01-05 16:56:59 +00002329 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002330 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002331 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002332 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2333 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2334
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 FPDiff = NumBytesCallerPushed - NumBytes;
2336
2337 // Set the delta of movement of the returnaddr stackslot.
2338 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002339 if (FPDiff < X86Info->getTCReturnAddrDelta())
2340 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002341 }
2342
Evan Chengf22f9b32010-02-06 03:28:46 +00002343 if (!IsSibcall)
2344 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002345
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002347 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002348 if (isTailCall && FPDiff)
2349 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2350 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002351
Dan Gohman475871a2008-07-27 21:46:04 +00002352 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2353 SmallVector<SDValue, 8> MemOpChains;
2354 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002355
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002356 // Walk the register/memloc assignments, inserting copies/loads. In the case
2357 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002358 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2359 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002360 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002361 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002362 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002363 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002364
Chris Lattner423c5f42007-02-28 05:31:48 +00002365 // Promote the value if needed.
2366 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002367 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002368 case CCValAssign::Full: break;
2369 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002370 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002371 break;
2372 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002373 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002374 break;
2375 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002376 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002377 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002378 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002379 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2380 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002381 } else
2382 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2383 break;
2384 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002385 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002386 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002387 case CCValAssign::Indirect: {
2388 // Store the argument.
2389 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002390 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002391 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002392 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002393 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002394 Arg = SpillSlot;
2395 break;
2396 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002398
Chris Lattner423c5f42007-02-28 05:31:48 +00002399 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002400 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2401 if (isVarArg && IsWin64) {
2402 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2403 // shadow reg if callee is a varargs function.
2404 unsigned ShadowReg = 0;
2405 switch (VA.getLocReg()) {
2406 case X86::XMM0: ShadowReg = X86::RCX; break;
2407 case X86::XMM1: ShadowReg = X86::RDX; break;
2408 case X86::XMM2: ShadowReg = X86::R8; break;
2409 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002410 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002411 if (ShadowReg)
2412 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002413 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002414 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002415 assert(VA.isMemLoc());
2416 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002417 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2418 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002419 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2420 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002421 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002423
Evan Cheng32fe1032006-05-25 00:59:30 +00002424 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002426 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002427
Chris Lattner88e1fd52009-07-09 04:24:46 +00002428 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002429 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2430 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002432 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2433 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002434 } else {
2435 // If we are tail calling and generating PIC/GOT style code load the
2436 // address of the callee into ECX. The value in ecx is used as target of
2437 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2438 // for tail calls on PIC/GOT architectures. Normally we would just put the
2439 // address of GOT into ebx and then call target@PLT. But for tail calls
2440 // ebx would be restored (since ebx is callee saved) before jumping to the
2441 // target@PLT.
2442
2443 // Note: The actual moving to ECX is done further down.
2444 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2445 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2446 !G->getGlobal()->hasProtectedVisibility())
2447 Callee = LowerGlobalAddress(Callee, DAG);
2448 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002449 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002450 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002451 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002452
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002453 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002454 // From AMD64 ABI document:
2455 // For calls that may call functions that use varargs or stdargs
2456 // (prototype-less calls or calls to functions containing ellipsis (...) in
2457 // the declaration) %al is used as hidden argument to specify the number
2458 // of SSE registers used. The contents of %al do not need to match exactly
2459 // the number of registers, but must be an ubound on the number of SSE
2460 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002461
Gordon Henriksen86737662008-01-05 16:56:59 +00002462 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002463 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2465 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2466 };
2467 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002468 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002469 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002470
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002471 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2472 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002473 }
2474
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002475 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002476 if (isTailCall) {
2477 // Force all the incoming stack arguments to be loaded from the stack
2478 // before any new outgoing arguments are stored to the stack, because the
2479 // outgoing stack slots may alias the incoming argument stack slots, and
2480 // the alias isn't otherwise explicit. This is slightly more conservative
2481 // than necessary, because it means that each store effectively depends
2482 // on every argument instead of just those arguments it would clobber.
2483 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2484
Dan Gohman475871a2008-07-27 21:46:04 +00002485 SmallVector<SDValue, 8> MemOpChains2;
2486 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002487 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002488 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002489 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2490 CCValAssign &VA = ArgLocs[i];
2491 if (VA.isRegLoc())
2492 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002493 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002494 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002495 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002496 // Create frame index.
2497 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002499 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002500 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002501
Duncan Sands276dcbd2008-03-21 09:14:45 +00002502 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002503 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002505 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002506 StackPtr = DAG.getCopyFromReg(Chain, dl,
2507 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002508 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002509 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002510
Dan Gohman98ca4f22009-08-05 01:29:28 +00002511 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2512 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002513 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002514 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002515 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002516 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002518 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002519 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002520 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002521 }
2522 }
2523
2524 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002526 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002527
2528 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002529 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2530 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002531 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002532 }
2533
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002534 // Build a sequence of copy-to-reg nodes chained together with token chain
2535 // and flag operands which copy the outgoing args into registers.
2536 SDValue InFlag;
2537 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2538 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2539 RegsToPass[i].second, InFlag);
2540 InFlag = Chain.getValue(1);
2541 }
2542
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002543 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2544 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2545 // In the 64-bit large code model, we have to make all calls
2546 // through a register, since the call instruction's 32-bit
2547 // pc-relative offset may not be large enough to hold the whole
2548 // address.
2549 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002550 // If the callee is a GlobalAddress node (quite common, every direct call
2551 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2552 // it.
2553
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002554 // We should use extra load for direct calls to dllimported functions in
2555 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002556 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002557 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002558 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002559 bool ExtraLoad = false;
2560 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002561
Chris Lattner48a7d022009-07-09 05:02:21 +00002562 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2563 // external symbols most go through the PLT in PIC mode. If the symbol
2564 // has hidden or protected visibility, or if it is static or local, then
2565 // we don't need to use the PLT - we can directly call it.
2566 if (Subtarget->isTargetELF() &&
2567 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002568 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002569 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002570 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002571 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002572 (!Subtarget->getTargetTriple().isMacOSX() ||
2573 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002574 // PC-relative references to external symbols should go through $stub,
2575 // unless we're building with the leopard linker or later, which
2576 // automatically synthesizes these stubs.
2577 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002578 } else if (Subtarget->isPICStyleRIPRel() &&
2579 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002580 cast<Function>(GV)->getAttributes().
2581 hasAttribute(AttributeSet::FunctionIndex,
2582 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002583 // If the function is marked as non-lazy, generate an indirect call
2584 // which loads from the GOT directly. This avoids runtime overhead
2585 // at the cost of eager binding (and one extra byte of encoding).
2586 OpFlags = X86II::MO_GOTPCREL;
2587 WrapperKind = X86ISD::WrapperRIP;
2588 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002589 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002590
Devang Patel0d881da2010-07-06 22:08:15 +00002591 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002592 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002593
2594 // Add a wrapper if needed.
2595 if (WrapperKind != ISD::DELETED_NODE)
2596 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2597 // Add extra indirection if needed.
2598 if (ExtraLoad)
2599 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2600 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002601 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002602 }
Bill Wendling056292f2008-09-16 21:48:12 +00002603 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002604 unsigned char OpFlags = 0;
2605
Evan Cheng1bf891a2010-12-01 22:59:46 +00002606 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2607 // external symbols should go through the PLT.
2608 if (Subtarget->isTargetELF() &&
2609 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2610 OpFlags = X86II::MO_PLT;
2611 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002612 (!Subtarget->getTargetTriple().isMacOSX() ||
2613 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002614 // PC-relative references to external symbols should go through $stub,
2615 // unless we're building with the leopard linker or later, which
2616 // automatically synthesizes these stubs.
2617 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002618 }
Eric Christopherfd179292009-08-27 18:07:15 +00002619
Chris Lattner48a7d022009-07-09 05:02:21 +00002620 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2621 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002622 }
2623
Chris Lattnerd96d0722007-02-25 06:40:16 +00002624 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002625 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002626 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002627
Evan Chengf22f9b32010-02-06 03:28:46 +00002628 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002629 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2630 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002631 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002632 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002633
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002634 Ops.push_back(Chain);
2635 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002636
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002639
Gordon Henriksen86737662008-01-05 16:56:59 +00002640 // Add argument registers to the end of the list so that they are known live
2641 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002642 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2643 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2644 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002645
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002646 // Add a register mask operand representing the call-preserved registers.
2647 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2648 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2649 assert(Mask && "Missing call preserved mask for calling convention");
2650 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002651
Gabor Greifba36cb52008-08-28 21:40:38 +00002652 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002653 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002654
Dan Gohman98ca4f22009-08-05 01:29:28 +00002655 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002656 // We used to do:
2657 //// If this is the first return lowered for this function, add the regs
2658 //// to the liveout set for the function.
2659 // This isn't right, although it's probably harmless on x86; liveouts
2660 // should be computed from returns not tail calls. Consider a void
2661 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002662 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002663 }
2664
Dale Johannesenace16102009-02-03 19:33:06 +00002665 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002666 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002667
Chris Lattner2d297092006-05-23 18:50:38 +00002668 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002669 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002670 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2671 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002672 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002673 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002674 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002675 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002676 // pops the hidden struct pointer, so we have to push it back.
2677 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002678 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002679 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002680 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002681 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002682
Gordon Henriksenae636f82008-01-03 16:47:34 +00002683 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002684 if (!IsSibcall) {
2685 Chain = DAG.getCALLSEQ_END(Chain,
2686 DAG.getIntPtrConstant(NumBytes, true),
2687 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2688 true),
2689 InFlag);
2690 InFlag = Chain.getValue(1);
2691 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002692
Chris Lattner3085e152007-02-25 08:59:22 +00002693 // Handle result values, copying them out of physregs into vregs that we
2694 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002695 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2696 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002697}
2698
Evan Cheng25ab6902006-09-08 06:48:29 +00002699//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002700// Fast Calling Convention (tail call) implementation
2701//===----------------------------------------------------------------------===//
2702
2703// Like std call, callee cleans arguments, convention except that ECX is
2704// reserved for storing the tail called function address. Only 2 registers are
2705// free for argument passing (inreg). Tail call optimization is performed
2706// provided:
2707// * tailcallopt is enabled
2708// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002709// On X86_64 architecture with GOT-style position independent code only local
2710// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002711// To keep the stack aligned according to platform abi the function
2712// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2713// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002714// If a tail called function callee has more arguments than the caller the
2715// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002716// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002717// original REtADDR, but before the saved framepointer or the spilled registers
2718// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2719// stack layout:
2720// arg1
2721// arg2
2722// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002723// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002724// move area ]
2725// (possible EBP)
2726// ESI
2727// EDI
2728// local1 ..
2729
2730/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2731/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002732unsigned
2733X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2734 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002735 MachineFunction &MF = DAG.getMachineFunction();
2736 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002737 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002738 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002739 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002740 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002741 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002742 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2743 // Number smaller than 12 so just add the difference.
2744 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2745 } else {
2746 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002747 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002748 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002749 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002750 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002751}
2752
Evan Cheng5f941932010-02-05 02:21:12 +00002753/// MatchingStackOffset - Return true if the given stack call argument is
2754/// already available in the same position (relatively) of the caller's
2755/// incoming argument stack.
2756static
2757bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2758 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2759 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002760 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2761 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002762 if (Arg.getOpcode() == ISD::CopyFromReg) {
2763 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002764 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002765 return false;
2766 MachineInstr *Def = MRI->getVRegDef(VR);
2767 if (!Def)
2768 return false;
2769 if (!Flags.isByVal()) {
2770 if (!TII->isLoadFromStackSlot(Def, FI))
2771 return false;
2772 } else {
2773 unsigned Opcode = Def->getOpcode();
2774 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2775 Def->getOperand(1).isFI()) {
2776 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002777 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002778 } else
2779 return false;
2780 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002781 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2782 if (Flags.isByVal())
2783 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002784 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002785 // define @foo(%struct.X* %A) {
2786 // tail call @bar(%struct.X* byval %A)
2787 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002788 return false;
2789 SDValue Ptr = Ld->getBasePtr();
2790 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2791 if (!FINode)
2792 return false;
2793 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002794 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002795 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002796 FI = FINode->getIndex();
2797 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002798 } else
2799 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002800
Evan Cheng4cae1332010-03-05 08:38:04 +00002801 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002802 if (!MFI->isFixedObjectIndex(FI))
2803 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002804 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002805}
2806
Dan Gohman98ca4f22009-08-05 01:29:28 +00002807/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2808/// for tail call optimization. Targets which want to do tail call
2809/// optimization should implement this function.
2810bool
2811X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002812 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002813 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002814 bool isCalleeStructRet,
2815 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002816 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002817 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002818 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002819 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002820 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002821 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002822 CalleeCC != CallingConv::C)
2823 return false;
2824
Evan Cheng7096ae42010-01-29 06:45:59 +00002825 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002826 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002827 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002828
2829 // If the function return type is x86_fp80 and the callee return type is not,
2830 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2831 // perform a tailcall optimization here.
2832 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2833 return false;
2834
Evan Cheng13617962010-04-30 01:12:32 +00002835 CallingConv::ID CallerCC = CallerF->getCallingConv();
2836 bool CCMatch = CallerCC == CalleeCC;
2837
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002838 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002839 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002840 return true;
2841 return false;
2842 }
2843
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002844 // Look for obvious safe cases to perform tail call optimization that do not
2845 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002846
Evan Cheng2c12cb42010-03-26 16:26:03 +00002847 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2848 // emit a special epilogue.
2849 if (RegInfo->needsStackRealignment(MF))
2850 return false;
2851
Evan Chenga375d472010-03-15 18:54:48 +00002852 // Also avoid sibcall optimization if either caller or callee uses struct
2853 // return semantics.
2854 if (isCalleeStructRet || isCallerStructRet)
2855 return false;
2856
Chad Rosier2416da32011-06-24 21:15:36 +00002857 // An stdcall caller is expected to clean up its arguments; the callee
2858 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002859 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002860 return false;
2861
Chad Rosier871f6642011-05-18 19:59:50 +00002862 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002863 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002864 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002865
2866 // Optimizing for varargs on Win64 is unlikely to be safe without
2867 // additional testing.
2868 if (Subtarget->isTargetWin64())
2869 return false;
2870
Chad Rosier871f6642011-05-18 19:59:50 +00002871 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002872 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002873 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002874
Chad Rosier871f6642011-05-18 19:59:50 +00002875 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2876 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2877 if (!ArgLocs[i].isRegLoc())
2878 return false;
2879 }
2880
Chad Rosier30450e82011-12-22 22:35:21 +00002881 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2882 // stack. Therefore, if it's not used by the call it is not safe to optimize
2883 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002884 bool Unused = false;
2885 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2886 if (!Ins[i].Used) {
2887 Unused = true;
2888 break;
2889 }
2890 }
2891 if (Unused) {
2892 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002893 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002894 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002895 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002896 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002897 CCValAssign &VA = RVLocs[i];
2898 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2899 return false;
2900 }
2901 }
2902
Evan Cheng13617962010-04-30 01:12:32 +00002903 // If the calling conventions do not match, then we'd better make sure the
2904 // results are returned in the same way as what the caller expects.
2905 if (!CCMatch) {
2906 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002907 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002908 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002909 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2910
2911 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002912 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002913 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002914 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2915
2916 if (RVLocs1.size() != RVLocs2.size())
2917 return false;
2918 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2919 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2920 return false;
2921 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2922 return false;
2923 if (RVLocs1[i].isRegLoc()) {
2924 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2925 return false;
2926 } else {
2927 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2928 return false;
2929 }
2930 }
2931 }
2932
Evan Chenga6bff982010-01-30 01:22:00 +00002933 // If the callee takes no arguments then go on to check the results of the
2934 // call.
2935 if (!Outs.empty()) {
2936 // Check if stack adjustment is needed. For now, do not do this if any
2937 // argument is passed on the stack.
2938 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002939 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002940 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002941
2942 // Allocate shadow area for Win64
2943 if (Subtarget->isTargetWin64()) {
2944 CCInfo.AllocateStack(32, 8);
2945 }
2946
Duncan Sands45907662010-10-31 13:21:44 +00002947 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002948 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002949 MachineFunction &MF = DAG.getMachineFunction();
2950 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2951 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002952
2953 // Check if the arguments are already laid out in the right way as
2954 // the caller's fixed stack objects.
2955 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002956 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2957 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002958 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002959 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2960 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002961 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002962 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002963 if (VA.getLocInfo() == CCValAssign::Indirect)
2964 return false;
2965 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002966 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2967 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002968 return false;
2969 }
2970 }
2971 }
Evan Cheng9c044672010-05-29 01:35:22 +00002972
2973 // If the tailcall address may be in a register, then make sure it's
2974 // possible to register allocate for it. In 32-bit, the call address can
2975 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002976 // callee-saved registers are restored. These happen to be the same
2977 // registers used to pass 'inreg' arguments so watch out for those.
2978 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002979 ((!isa<GlobalAddressSDNode>(Callee) &&
2980 !isa<ExternalSymbolSDNode>(Callee)) ||
2981 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002982 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002983 // In PIC we need an extra register to formulate the address computation
2984 // for the callee.
2985 unsigned MaxInRegs =
2986 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
2987
Evan Cheng9c044672010-05-29 01:35:22 +00002988 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2989 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002990 if (!VA.isRegLoc())
2991 continue;
2992 unsigned Reg = VA.getLocReg();
2993 switch (Reg) {
2994 default: break;
2995 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002996 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00002997 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002998 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002999 }
3000 }
3001 }
Evan Chenga6bff982010-01-30 01:22:00 +00003002 }
Evan Chengb1712452010-01-27 06:25:16 +00003003
Evan Cheng86809cc2010-02-03 03:28:02 +00003004 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003005}
3006
Dan Gohman3df24e62008-09-03 23:12:08 +00003007FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003008X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3009 const TargetLibraryInfo *libInfo) const {
3010 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003011}
3012
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003013//===----------------------------------------------------------------------===//
3014// Other Lowering Hooks
3015//===----------------------------------------------------------------------===//
3016
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003017static bool MayFoldLoad(SDValue Op) {
3018 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3019}
3020
3021static bool MayFoldIntoStore(SDValue Op) {
3022 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3023}
3024
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003025static bool isTargetShuffle(unsigned Opcode) {
3026 switch(Opcode) {
3027 default: return false;
3028 case X86ISD::PSHUFD:
3029 case X86ISD::PSHUFHW:
3030 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003031 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003032 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003033 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003034 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003035 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003036 case X86ISD::MOVLPS:
3037 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003038 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003039 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003040 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003041 case X86ISD::MOVSS:
3042 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003043 case X86ISD::UNPCKL:
3044 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003045 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003046 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003047 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003048 return true;
3049 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003050}
3051
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003052static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003053 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003054 switch(Opc) {
3055 default: llvm_unreachable("Unknown x86 shuffle node");
3056 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003057 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003058 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003059 return DAG.getNode(Opc, dl, VT, V1);
3060 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003061}
3062
3063static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003064 SDValue V1, unsigned TargetMask,
3065 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003066 switch(Opc) {
3067 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003068 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003069 case X86ISD::PSHUFHW:
3070 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003071 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003072 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003073 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3074 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003075}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003076
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003077static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003078 SDValue V1, SDValue V2, unsigned TargetMask,
3079 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003080 switch(Opc) {
3081 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003082 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003083 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003084 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003085 return DAG.getNode(Opc, dl, VT, V1, V2,
3086 DAG.getConstant(TargetMask, MVT::i8));
3087 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003088}
3089
3090static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3091 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3092 switch(Opc) {
3093 default: llvm_unreachable("Unknown x86 shuffle node");
3094 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003095 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003096 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003097 case X86ISD::MOVLPS:
3098 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003099 case X86ISD::MOVSS:
3100 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003101 case X86ISD::UNPCKL:
3102 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003103 return DAG.getNode(Opc, dl, VT, V1, V2);
3104 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003105}
3106
Dan Gohmand858e902010-04-17 15:26:15 +00003107SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003108 MachineFunction &MF = DAG.getMachineFunction();
3109 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3110 int ReturnAddrIndex = FuncInfo->getRAIndex();
3111
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003112 if (ReturnAddrIndex == 0) {
3113 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003114 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003115 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003116 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003117 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003118 }
3119
Evan Cheng25ab6902006-09-08 06:48:29 +00003120 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003121}
3122
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003123bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3124 bool hasSymbolicDisplacement) {
3125 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003126 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003127 return false;
3128
3129 // If we don't have a symbolic displacement - we don't have any extra
3130 // restrictions.
3131 if (!hasSymbolicDisplacement)
3132 return true;
3133
3134 // FIXME: Some tweaks might be needed for medium code model.
3135 if (M != CodeModel::Small && M != CodeModel::Kernel)
3136 return false;
3137
3138 // For small code model we assume that latest object is 16MB before end of 31
3139 // bits boundary. We may also accept pretty large negative constants knowing
3140 // that all objects are in the positive half of address space.
3141 if (M == CodeModel::Small && Offset < 16*1024*1024)
3142 return true;
3143
3144 // For kernel code model we know that all object resist in the negative half
3145 // of 32bits address space. We may not accept negative offsets, since they may
3146 // be just off and we may accept pretty large positive ones.
3147 if (M == CodeModel::Kernel && Offset > 0)
3148 return true;
3149
3150 return false;
3151}
3152
Evan Chengef41ff62011-06-23 17:54:54 +00003153/// isCalleePop - Determines whether the callee is required to pop its
3154/// own arguments. Callee pop is necessary to support tail calls.
3155bool X86::isCalleePop(CallingConv::ID CallingConv,
3156 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3157 if (IsVarArg)
3158 return false;
3159
3160 switch (CallingConv) {
3161 default:
3162 return false;
3163 case CallingConv::X86_StdCall:
3164 return !is64Bit;
3165 case CallingConv::X86_FastCall:
3166 return !is64Bit;
3167 case CallingConv::X86_ThisCall:
3168 return !is64Bit;
3169 case CallingConv::Fast:
3170 return TailCallOpt;
3171 case CallingConv::GHC:
3172 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003173 case CallingConv::HiPE:
3174 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003175 }
3176}
3177
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003178/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3179/// specific condition code, returning the condition code and the LHS/RHS of the
3180/// comparison to make.
3181static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3182 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003183 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003184 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3185 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3186 // X > -1 -> X == 0, jump !sign.
3187 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003188 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003189 }
3190 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003191 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003192 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003193 }
3194 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003195 // X < 1 -> X <= 0
3196 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003197 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003198 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003199 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003200
Evan Chengd9558e02006-01-06 00:43:03 +00003201 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003202 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003203 case ISD::SETEQ: return X86::COND_E;
3204 case ISD::SETGT: return X86::COND_G;
3205 case ISD::SETGE: return X86::COND_GE;
3206 case ISD::SETLT: return X86::COND_L;
3207 case ISD::SETLE: return X86::COND_LE;
3208 case ISD::SETNE: return X86::COND_NE;
3209 case ISD::SETULT: return X86::COND_B;
3210 case ISD::SETUGT: return X86::COND_A;
3211 case ISD::SETULE: return X86::COND_BE;
3212 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003213 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003214 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003215
Chris Lattner4c78e022008-12-23 23:42:27 +00003216 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003217
Chris Lattner4c78e022008-12-23 23:42:27 +00003218 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003219 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3220 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003221 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3222 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003223 }
3224
Chris Lattner4c78e022008-12-23 23:42:27 +00003225 switch (SetCCOpcode) {
3226 default: break;
3227 case ISD::SETOLT:
3228 case ISD::SETOLE:
3229 case ISD::SETUGT:
3230 case ISD::SETUGE:
3231 std::swap(LHS, RHS);
3232 break;
3233 }
3234
3235 // On a floating point condition, the flags are set as follows:
3236 // ZF PF CF op
3237 // 0 | 0 | 0 | X > Y
3238 // 0 | 0 | 1 | X < Y
3239 // 1 | 0 | 0 | X == Y
3240 // 1 | 1 | 1 | unordered
3241 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003242 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003243 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003244 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003245 case ISD::SETOLT: // flipped
3246 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003247 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003248 case ISD::SETOLE: // flipped
3249 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003250 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003251 case ISD::SETUGT: // flipped
3252 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003253 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003254 case ISD::SETUGE: // flipped
3255 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003256 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003257 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003258 case ISD::SETNE: return X86::COND_NE;
3259 case ISD::SETUO: return X86::COND_P;
3260 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003261 case ISD::SETOEQ:
3262 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003263 }
Evan Chengd9558e02006-01-06 00:43:03 +00003264}
3265
Evan Cheng4a460802006-01-11 00:33:36 +00003266/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3267/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003268/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003269static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003270 switch (X86CC) {
3271 default:
3272 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003273 case X86::COND_B:
3274 case X86::COND_BE:
3275 case X86::COND_E:
3276 case X86::COND_P:
3277 case X86::COND_A:
3278 case X86::COND_AE:
3279 case X86::COND_NE:
3280 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003281 return true;
3282 }
3283}
3284
Evan Chengeb2f9692009-10-27 19:56:55 +00003285/// isFPImmLegal - Returns true if the target can instruction select the
3286/// specified FP immediate natively. If false, the legalizer will
3287/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003288bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003289 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3290 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3291 return true;
3292 }
3293 return false;
3294}
3295
Nate Begeman9008ca62009-04-27 18:41:29 +00003296/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3297/// the specified range (L, H].
3298static bool isUndefOrInRange(int Val, int Low, int Hi) {
3299 return (Val < 0) || (Val >= Low && Val < Hi);
3300}
3301
3302/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3303/// specified value.
3304static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003305 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003306}
3307
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003308/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003309/// from position Pos and ending in Pos+Size, falls within the specified
3310/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003311static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003312 unsigned Pos, unsigned Size, int Low) {
3313 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003314 if (!isUndefOrEqual(Mask[i], Low))
3315 return false;
3316 return true;
3317}
3318
Nate Begeman9008ca62009-04-27 18:41:29 +00003319/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3320/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3321/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003322static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003323 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 return (Mask[0] < 2 && Mask[1] < 2);
3327 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003328}
3329
Nate Begeman9008ca62009-04-27 18:41:29 +00003330/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3331/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003332static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3333 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003334 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003335
Nate Begeman9008ca62009-04-27 18:41:29 +00003336 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003337 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3338 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Evan Cheng506d3df2006-03-29 23:07:14 +00003340 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003341 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003342 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003343 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003344
Craig Toppera9a568a2012-05-02 08:03:44 +00003345 if (VT == MVT::v16i16) {
3346 // Lower quadword copied in order or undef.
3347 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3348 return false;
3349
3350 // Upper quadword shuffled.
3351 for (unsigned i = 12; i != 16; ++i)
3352 if (!isUndefOrInRange(Mask[i], 12, 16))
3353 return false;
3354 }
3355
Evan Cheng506d3df2006-03-29 23:07:14 +00003356 return true;
3357}
3358
Nate Begeman9008ca62009-04-27 18:41:29 +00003359/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3360/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003361static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3362 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003363 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Rafael Espindola15684b22009-04-24 12:40:33 +00003365 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003366 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3367 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003368
Rafael Espindola15684b22009-04-24 12:40:33 +00003369 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003370 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003371 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Craig Toppera9a568a2012-05-02 08:03:44 +00003374 if (VT == MVT::v16i16) {
3375 // Upper quadword copied in order.
3376 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3377 return false;
3378
3379 // Lower quadword shuffled.
3380 for (unsigned i = 8; i != 12; ++i)
3381 if (!isUndefOrInRange(Mask[i], 8, 12))
3382 return false;
3383 }
3384
Rafael Espindola15684b22009-04-24 12:40:33 +00003385 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003386}
3387
Nate Begemana09008b2009-10-19 02:17:23 +00003388/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3389/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003390static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3391 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003392 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3393 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003394 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003395
Craig Topper0e2037b2012-01-20 05:53:00 +00003396 unsigned NumElts = VT.getVectorNumElements();
3397 unsigned NumLanes = VT.getSizeInBits()/128;
3398 unsigned NumLaneElts = NumElts/NumLanes;
3399
3400 // Do not handle 64-bit element shuffles with palignr.
3401 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003402 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003403
Craig Topper0e2037b2012-01-20 05:53:00 +00003404 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3405 unsigned i;
3406 for (i = 0; i != NumLaneElts; ++i) {
3407 if (Mask[i+l] >= 0)
3408 break;
3409 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003410
Craig Topper0e2037b2012-01-20 05:53:00 +00003411 // Lane is all undef, go to next lane
3412 if (i == NumLaneElts)
3413 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003414
Craig Topper0e2037b2012-01-20 05:53:00 +00003415 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003416
Craig Topper0e2037b2012-01-20 05:53:00 +00003417 // Make sure its in this lane in one of the sources
3418 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3419 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003420 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003421
3422 // If not lane 0, then we must match lane 0
3423 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3424 return false;
3425
3426 // Correct second source to be contiguous with first source
3427 if (Start >= (int)NumElts)
3428 Start -= NumElts - NumLaneElts;
3429
3430 // Make sure we're shifting in the right direction.
3431 if (Start <= (int)(i+l))
3432 return false;
3433
3434 Start -= i;
3435
3436 // Check the rest of the elements to see if they are consecutive.
3437 for (++i; i != NumLaneElts; ++i) {
3438 int Idx = Mask[i+l];
3439
3440 // Make sure its in this lane
3441 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3442 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3443 return false;
3444
3445 // If not lane 0, then we must match lane 0
3446 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3447 return false;
3448
3449 if (Idx >= (int)NumElts)
3450 Idx -= NumElts - NumLaneElts;
3451
3452 if (!isUndefOrEqual(Idx, Start+i))
3453 return false;
3454
3455 }
Nate Begemana09008b2009-10-19 02:17:23 +00003456 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003457
Nate Begemana09008b2009-10-19 02:17:23 +00003458 return true;
3459}
3460
Craig Topper1a7700a2012-01-19 08:19:12 +00003461/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3462/// the two vector operands have swapped position.
3463static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3464 unsigned NumElems) {
3465 for (unsigned i = 0; i != NumElems; ++i) {
3466 int idx = Mask[i];
3467 if (idx < 0)
3468 continue;
3469 else if (idx < (int)NumElems)
3470 Mask[i] = idx + NumElems;
3471 else
3472 Mask[i] = idx - NumElems;
3473 }
3474}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003475
Craig Topper1a7700a2012-01-19 08:19:12 +00003476/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3477/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3478/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3479/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003480static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003481 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003482 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003483 return false;
3484
Craig Topper1a7700a2012-01-19 08:19:12 +00003485 unsigned NumElems = VT.getVectorNumElements();
3486 unsigned NumLanes = VT.getSizeInBits()/128;
3487 unsigned NumLaneElems = NumElems/NumLanes;
3488
3489 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003490 return false;
3491
3492 // VSHUFPSY divides the resulting vector into 4 chunks.
3493 // The sources are also splitted into 4 chunks, and each destination
3494 // chunk must come from a different source chunk.
3495 //
3496 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3497 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3498 //
3499 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3500 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3501 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003502 // VSHUFPDY divides the resulting vector into 4 chunks.
3503 // The sources are also splitted into 4 chunks, and each destination
3504 // chunk must come from a different source chunk.
3505 //
3506 // SRC1 => X3 X2 X1 X0
3507 // SRC2 => Y3 Y2 Y1 Y0
3508 //
3509 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3510 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003511 unsigned HalfLaneElems = NumLaneElems/2;
3512 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3513 for (unsigned i = 0; i != NumLaneElems; ++i) {
3514 int Idx = Mask[i+l];
3515 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3516 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3517 return false;
3518 // For VSHUFPSY, the mask of the second half must be the same as the
3519 // first but with the appropriate offsets. This works in the same way as
3520 // VPERMILPS works with masks.
3521 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3522 continue;
3523 if (!isUndefOrEqual(Idx, Mask[i]+l))
3524 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003525 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003526 }
3527
3528 return true;
3529}
3530
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003531/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3532/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003533static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003534 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003535 return false;
3536
Craig Topper7a9a28b2012-08-12 02:23:29 +00003537 unsigned NumElems = VT.getVectorNumElements();
3538
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003539 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003540 return false;
3541
Evan Cheng2064a2b2006-03-28 06:50:32 +00003542 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003543 return isUndefOrEqual(Mask[0], 6) &&
3544 isUndefOrEqual(Mask[1], 7) &&
3545 isUndefOrEqual(Mask[2], 2) &&
3546 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003547}
3548
Nate Begeman0b10b912009-11-07 23:17:15 +00003549/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3550/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3551/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003552static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003553 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003554 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003555
Craig Topper7a9a28b2012-08-12 02:23:29 +00003556 unsigned NumElems = VT.getVectorNumElements();
3557
Nate Begeman0b10b912009-11-07 23:17:15 +00003558 if (NumElems != 4)
3559 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003560
Craig Topperdd637ae2012-02-19 05:41:45 +00003561 return isUndefOrEqual(Mask[0], 2) &&
3562 isUndefOrEqual(Mask[1], 3) &&
3563 isUndefOrEqual(Mask[2], 2) &&
3564 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003565}
3566
Evan Cheng5ced1d82006-04-06 23:23:56 +00003567/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3568/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003569static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003570 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003571 return false;
3572
Craig Topperdd637ae2012-02-19 05:41:45 +00003573 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003574
Evan Cheng5ced1d82006-04-06 23:23:56 +00003575 if (NumElems != 2 && NumElems != 4)
3576 return false;
3577
Chad Rosier238ae312012-04-30 17:47:15 +00003578 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003579 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003580 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003581
Chad Rosier238ae312012-04-30 17:47:15 +00003582 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003583 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003584 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003585
3586 return true;
3587}
3588
Nate Begeman0b10b912009-11-07 23:17:15 +00003589/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3590/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003591static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003592 if (!VT.is128BitVector())
3593 return false;
3594
Craig Topperdd637ae2012-02-19 05:41:45 +00003595 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003596
Craig Topper7a9a28b2012-08-12 02:23:29 +00003597 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003598 return false;
3599
Chad Rosier238ae312012-04-30 17:47:15 +00003600 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003601 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003602 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003603
Chad Rosier238ae312012-04-30 17:47:15 +00003604 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3605 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003606 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003607
3608 return true;
3609}
3610
Elena Demikhovsky15963732012-06-26 08:04:10 +00003611//
3612// Some special combinations that can be optimized.
3613//
3614static
3615SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3616 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003617 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003618 DebugLoc dl = SVOp->getDebugLoc();
3619
3620 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3621 return SDValue();
3622
3623 ArrayRef<int> Mask = SVOp->getMask();
3624
3625 // These are the special masks that may be optimized.
3626 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3627 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3628 bool MatchEvenMask = true;
3629 bool MatchOddMask = true;
3630 for (int i=0; i<8; ++i) {
3631 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3632 MatchEvenMask = false;
3633 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3634 MatchOddMask = false;
3635 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003636
Elena Demikhovsky32510202012-09-04 12:49:02 +00003637 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003638 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003639
Elena Demikhovsky15963732012-06-26 08:04:10 +00003640 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3641
Elena Demikhovsky32510202012-09-04 12:49:02 +00003642 SDValue Op0 = SVOp->getOperand(0);
3643 SDValue Op1 = SVOp->getOperand(1);
3644
3645 if (MatchEvenMask) {
3646 // Shift the second operand right to 32 bits.
3647 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3648 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3649 } else {
3650 // Shift the first operand left to 32 bits.
3651 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3652 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3653 }
3654 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3655 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003656}
3657
Evan Cheng0038e592006-03-28 00:39:58 +00003658/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3659/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003660static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003661 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003662 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003663
3664 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3665 "Unsupported vector type for unpckh");
3666
Craig Topper5a529e42013-01-18 06:44:29 +00003667 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003668 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003669 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003671 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3672 // independently on 128-bit lanes.
3673 unsigned NumLanes = VT.getSizeInBits()/128;
3674 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003675
Craig Topper94438ba2011-12-16 08:06:31 +00003676 for (unsigned l = 0; l != NumLanes; ++l) {
3677 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3678 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003679 i += 2, ++j) {
3680 int BitI = Mask[i];
3681 int BitI1 = Mask[i+1];
3682 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003683 return false;
David Greenea20244d2011-03-02 17:23:43 +00003684 if (V2IsSplat) {
3685 if (!isUndefOrEqual(BitI1, NumElts))
3686 return false;
3687 } else {
3688 if (!isUndefOrEqual(BitI1, j + NumElts))
3689 return false;
3690 }
Evan Cheng39623da2006-04-20 08:58:49 +00003691 }
Evan Cheng0038e592006-03-28 00:39:58 +00003692 }
David Greenea20244d2011-03-02 17:23:43 +00003693
Evan Cheng0038e592006-03-28 00:39:58 +00003694 return true;
3695}
3696
Evan Cheng4fcb9222006-03-28 02:43:26 +00003697/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3698/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003699static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003700 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003701 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003702
3703 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3704 "Unsupported vector type for unpckh");
3705
Craig Topper5a529e42013-01-18 06:44:29 +00003706 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003707 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003708 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003709
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003710 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3711 // independently on 128-bit lanes.
3712 unsigned NumLanes = VT.getSizeInBits()/128;
3713 unsigned NumLaneElts = NumElts/NumLanes;
3714
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003715 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003716 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3717 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003718 int BitI = Mask[i];
3719 int BitI1 = Mask[i+1];
3720 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003721 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003722 if (V2IsSplat) {
3723 if (isUndefOrEqual(BitI1, NumElts))
3724 return false;
3725 } else {
3726 if (!isUndefOrEqual(BitI1, j+NumElts))
3727 return false;
3728 }
Evan Cheng39623da2006-04-20 08:58:49 +00003729 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003730 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003731 return true;
3732}
3733
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003734/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3735/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3736/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003737static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003738 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003739 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003740
3741 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3742 "Unsupported vector type for unpckh");
3743
Craig Topper5a529e42013-01-18 06:44:29 +00003744 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003745 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003747
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003748 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3749 // FIXME: Need a better way to get rid of this, there's no latency difference
3750 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3751 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003752 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003753 return false;
3754
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003755 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3756 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003757 unsigned NumLanes = VT.getSizeInBits()/128;
3758 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003759
Craig Topper94438ba2011-12-16 08:06:31 +00003760 for (unsigned l = 0; l != NumLanes; ++l) {
3761 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3762 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003763 i += 2, ++j) {
3764 int BitI = Mask[i];
3765 int BitI1 = Mask[i+1];
3766
3767 if (!isUndefOrEqual(BitI, j))
3768 return false;
3769 if (!isUndefOrEqual(BitI1, j))
3770 return false;
3771 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003772 }
David Greenea20244d2011-03-02 17:23:43 +00003773
Rafael Espindola15684b22009-04-24 12:40:33 +00003774 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003775}
3776
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003777/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3778/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3779/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003780static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003781 unsigned NumElts = VT.getVectorNumElements();
3782
3783 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3784 "Unsupported vector type for unpckh");
3785
Craig Topper5a529e42013-01-18 06:44:29 +00003786 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003787 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003788 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003789
Craig Topper94438ba2011-12-16 08:06:31 +00003790 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3791 // independently on 128-bit lanes.
3792 unsigned NumLanes = VT.getSizeInBits()/128;
3793 unsigned NumLaneElts = NumElts/NumLanes;
3794
3795 for (unsigned l = 0; l != NumLanes; ++l) {
3796 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3797 i != (l+1)*NumLaneElts; i += 2, ++j) {
3798 int BitI = Mask[i];
3799 int BitI1 = Mask[i+1];
3800 if (!isUndefOrEqual(BitI, j))
3801 return false;
3802 if (!isUndefOrEqual(BitI1, j))
3803 return false;
3804 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003805 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003806 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003807}
3808
Evan Cheng017dcc62006-04-21 01:05:10 +00003809/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3810/// specifies a shuffle of elements that is suitable for input to MOVSS,
3811/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003812static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003813 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003814 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003815 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003816 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003817
Craig Topperc612d792012-01-02 09:17:37 +00003818 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003819
Nate Begeman9008ca62009-04-27 18:41:29 +00003820 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003821 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003822
Craig Topperc612d792012-01-02 09:17:37 +00003823 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003824 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003825 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003826
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003827 return true;
3828}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003829
Craig Topper70b883b2011-11-28 10:14:51 +00003830/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003831/// as permutations between 128-bit chunks or halves. As an example: this
3832/// shuffle bellow:
3833/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3834/// The first half comes from the second half of V1 and the second half from the
3835/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003836static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3837 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003838 return false;
3839
3840 // The shuffle result is divided into half A and half B. In total the two
3841 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3842 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003843 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003844 bool MatchA = false, MatchB = false;
3845
3846 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003847 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003848 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3849 MatchA = true;
3850 break;
3851 }
3852 }
3853
3854 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003855 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003856 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3857 MatchB = true;
3858 break;
3859 }
3860 }
3861
3862 return MatchA && MatchB;
3863}
3864
Craig Topper70b883b2011-11-28 10:14:51 +00003865/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3866/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003867static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003868 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003869
Craig Topperc612d792012-01-02 09:17:37 +00003870 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003871
Craig Topperc612d792012-01-02 09:17:37 +00003872 unsigned FstHalf = 0, SndHalf = 0;
3873 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003874 if (SVOp->getMaskElt(i) > 0) {
3875 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3876 break;
3877 }
3878 }
Craig Topperc612d792012-01-02 09:17:37 +00003879 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003880 if (SVOp->getMaskElt(i) > 0) {
3881 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3882 break;
3883 }
3884 }
3885
3886 return (FstHalf | (SndHalf << 4));
3887}
3888
Craig Topper70b883b2011-11-28 10:14:51 +00003889/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003890/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3891/// Note that VPERMIL mask matching is different depending whether theunderlying
3892/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3893/// to the same elements of the low, but to the higher half of the source.
3894/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003895/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003896static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3897 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003898 return false;
3899
Craig Topperc612d792012-01-02 09:17:37 +00003900 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003901 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003902 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003903 return false;
3904
Craig Topperc612d792012-01-02 09:17:37 +00003905 unsigned NumLanes = VT.getSizeInBits()/128;
3906 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003907 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003908 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003909 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003910 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003911 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003912 continue;
3913 // VPERMILPS handling
3914 if (Mask[i] < 0)
3915 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003916 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003917 return false;
3918 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003919 }
3920
3921 return true;
3922}
3923
Craig Topper5aaffa82012-02-19 02:53:47 +00003924/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003925/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003926/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003927static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003929 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003930 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003931
3932 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003933 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003934 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003935
Nate Begeman9008ca62009-04-27 18:41:29 +00003936 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003937 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003938
Craig Topperc612d792012-01-02 09:17:37 +00003939 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3941 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3942 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003943 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003944
Evan Cheng39623da2006-04-20 08:58:49 +00003945 return true;
3946}
3947
Evan Chengd9539472006-04-14 21:59:03 +00003948/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3949/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003950/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003951static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003952 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003953 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003954 return false;
3955
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003956 unsigned NumElems = VT.getVectorNumElements();
3957
Craig Topper5a529e42013-01-18 06:44:29 +00003958 if ((VT.is128BitVector() && NumElems != 4) ||
3959 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003960 return false;
3961
3962 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003963 for (unsigned i = 0; i != NumElems; i += 2)
3964 if (!isUndefOrEqual(Mask[i], i+1) ||
3965 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003967
3968 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003969}
3970
3971/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3972/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003973/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003974static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003975 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003976 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003977 return false;
3978
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003979 unsigned NumElems = VT.getVectorNumElements();
3980
Craig Topper5a529e42013-01-18 06:44:29 +00003981 if ((VT.is128BitVector() && NumElems != 4) ||
3982 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003983 return false;
3984
3985 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003986 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003987 if (!isUndefOrEqual(Mask[i], i) ||
3988 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003989 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003990
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003991 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003992}
3993
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003994/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3995/// specifies a shuffle of elements that is suitable for input to 256-bit
3996/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003997static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3998 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003999 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004000
Craig Topper7a9a28b2012-08-12 02:23:29 +00004001 unsigned NumElts = VT.getVectorNumElements();
4002 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004003 return false;
4004
Craig Topperc612d792012-01-02 09:17:37 +00004005 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004006 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004007 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004008 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004009 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004010 return false;
4011 return true;
4012}
4013
Evan Cheng0b457f02008-09-25 20:50:48 +00004014/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004015/// specifies a shuffle of elements that is suitable for input to 128-bit
4016/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004017static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004018 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004019 return false;
4020
Craig Topperc612d792012-01-02 09:17:37 +00004021 unsigned e = VT.getVectorNumElements() / 2;
4022 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004023 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004024 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004025 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004026 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004027 return false;
4028 return true;
4029}
4030
David Greenec38a03e2011-02-03 15:50:00 +00004031/// isVEXTRACTF128Index - Return true if the specified
4032/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4033/// suitable for input to VEXTRACTF128.
4034bool X86::isVEXTRACTF128Index(SDNode *N) {
4035 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4036 return false;
4037
4038 // The index should be aligned on a 128-bit boundary.
4039 uint64_t Index =
4040 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4041
Craig Topper5141d972013-01-18 08:41:28 +00004042 MVT VT = N->getValueType(0).getSimpleVT();
4043 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004044 bool Result = (Index * ElSize) % 128 == 0;
4045
4046 return Result;
4047}
4048
David Greeneccacdc12011-02-04 16:08:29 +00004049/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4050/// operand specifies a subvector insert that is suitable for input to
4051/// VINSERTF128.
4052bool X86::isVINSERTF128Index(SDNode *N) {
4053 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4054 return false;
4055
4056 // The index should be aligned on a 128-bit boundary.
4057 uint64_t Index =
4058 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4059
Craig Topper5141d972013-01-18 08:41:28 +00004060 MVT VT = N->getValueType(0).getSimpleVT();
4061 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004062 bool Result = (Index * ElSize) % 128 == 0;
4063
4064 return Result;
4065}
4066
Evan Cheng63d33002006-03-22 08:01:21 +00004067/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004068/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004069/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004070static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004071 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004072
Craig Topper1a7700a2012-01-19 08:19:12 +00004073 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4074 "Unsupported vector type for PSHUF/SHUFP");
4075
4076 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4077 // independently on 128-bit lanes.
4078 unsigned NumElts = VT.getVectorNumElements();
4079 unsigned NumLanes = VT.getSizeInBits()/128;
4080 unsigned NumLaneElts = NumElts/NumLanes;
4081
4082 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4083 "Only supports 2 or 4 elements per lane");
4084
4085 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004086 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004087 for (unsigned i = 0; i != NumElts; ++i) {
4088 int Elt = N->getMaskElt(i);
4089 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004090 Elt &= NumLaneElts - 1;
4091 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004092 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004093 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004094
Evan Cheng63d33002006-03-22 08:01:21 +00004095 return Mask;
4096}
4097
Evan Cheng506d3df2006-03-29 23:07:14 +00004098/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004099/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004100static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004101 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004102
4103 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4104 "Unsupported vector type for PSHUFHW");
4105
4106 unsigned NumElts = VT.getVectorNumElements();
4107
Evan Cheng506d3df2006-03-29 23:07:14 +00004108 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004109 for (unsigned l = 0; l != NumElts; l += 8) {
4110 // 8 nodes per lane, but we only care about the last 4.
4111 for (unsigned i = 0; i < 4; ++i) {
4112 int Elt = N->getMaskElt(l+i+4);
4113 if (Elt < 0) continue;
4114 Elt &= 0x3; // only 2-bits.
4115 Mask |= Elt << (i * 2);
4116 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004117 }
Craig Topper6b28d352012-05-03 07:12:59 +00004118
Evan Cheng506d3df2006-03-29 23:07:14 +00004119 return Mask;
4120}
4121
4122/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004123/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004124static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004125 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004126
4127 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4128 "Unsupported vector type for PSHUFHW");
4129
4130 unsigned NumElts = VT.getVectorNumElements();
4131
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004133 for (unsigned l = 0; l != NumElts; l += 8) {
4134 // 8 nodes per lane, but we only care about the first 4.
4135 for (unsigned i = 0; i < 4; ++i) {
4136 int Elt = N->getMaskElt(l+i);
4137 if (Elt < 0) continue;
4138 Elt &= 0x3; // only 2-bits
4139 Mask |= Elt << (i * 2);
4140 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004141 }
Craig Topper6b28d352012-05-03 07:12:59 +00004142
Evan Cheng506d3df2006-03-29 23:07:14 +00004143 return Mask;
4144}
4145
Nate Begemana09008b2009-10-19 02:17:23 +00004146/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4147/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004148static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004149 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004150 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004151
Craig Topper0e2037b2012-01-20 05:53:00 +00004152 unsigned NumElts = VT.getVectorNumElements();
4153 unsigned NumLanes = VT.getSizeInBits()/128;
4154 unsigned NumLaneElts = NumElts/NumLanes;
4155
4156 int Val = 0;
4157 unsigned i;
4158 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004159 Val = SVOp->getMaskElt(i);
4160 if (Val >= 0)
4161 break;
4162 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004163 if (Val >= (int)NumElts)
4164 Val -= NumElts - NumLaneElts;
4165
Eli Friedman63f8dde2011-07-25 21:36:45 +00004166 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004167 return (Val - i) * EltSize;
4168}
4169
David Greenec38a03e2011-02-03 15:50:00 +00004170/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4171/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4172/// instructions.
4173unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4174 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4175 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4176
4177 uint64_t Index =
4178 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4179
Craig Toppercfcab212013-01-19 08:27:45 +00004180 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4181 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004182
4183 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004184 return Index / NumElemsPerChunk;
4185}
4186
David Greeneccacdc12011-02-04 16:08:29 +00004187/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4188/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4189/// instructions.
4190unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4191 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4192 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4193
4194 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004195 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004196
Craig Toppercfcab212013-01-19 08:27:45 +00004197 MVT VecVT = N->getValueType(0).getSimpleVT();
4198 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004199
4200 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004201 return Index / NumElemsPerChunk;
4202}
4203
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004204/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4205/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4206/// Handles 256-bit.
4207static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004208 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004209
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004210 unsigned NumElts = VT.getVectorNumElements();
4211
Craig Topper095c5282012-04-15 23:48:57 +00004212 assert((VT.is256BitVector() && NumElts == 4) &&
4213 "Unsupported vector type for VPERMQ/VPERMPD");
4214
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004215 unsigned Mask = 0;
4216 for (unsigned i = 0; i != NumElts; ++i) {
4217 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004218 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004219 continue;
4220 Mask |= Elt << (i*2);
4221 }
4222
4223 return Mask;
4224}
Evan Cheng37b73872009-07-30 08:33:02 +00004225/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4226/// constant +0.0.
4227bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004228 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4229 return CN->isNullValue();
4230 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4231 return CFP->getValueAPF().isPosZero();
4232 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004233}
4234
Nate Begeman9008ca62009-04-27 18:41:29 +00004235/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4236/// their permute mask.
4237static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4238 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004239 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004240 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004242
Nate Begeman5a5ca152009-04-29 05:20:52 +00004243 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004244 int Idx = SVOp->getMaskElt(i);
4245 if (Idx >= 0) {
4246 if (Idx < (int)NumElems)
4247 Idx += NumElems;
4248 else
4249 Idx -= NumElems;
4250 }
4251 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004252 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4254 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004255}
4256
Evan Cheng533a0aa2006-04-19 20:35:22 +00004257/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4258/// match movhlps. The lower half elements should come from upper half of
4259/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004260/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004261static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004262 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004263 return false;
4264 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004265 return false;
4266 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004267 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004268 return false;
4269 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004270 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004271 return false;
4272 return true;
4273}
4274
Evan Cheng5ced1d82006-04-06 23:23:56 +00004275/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004276/// is promoted to a vector. It also returns the LoadSDNode by reference if
4277/// required.
4278static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004279 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4280 return false;
4281 N = N->getOperand(0).getNode();
4282 if (!ISD::isNON_EXTLoad(N))
4283 return false;
4284 if (LD)
4285 *LD = cast<LoadSDNode>(N);
4286 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004287}
4288
Dan Gohman65fd6562011-11-03 21:49:52 +00004289// Test whether the given value is a vector value which will be legalized
4290// into a load.
4291static bool WillBeConstantPoolLoad(SDNode *N) {
4292 if (N->getOpcode() != ISD::BUILD_VECTOR)
4293 return false;
4294
4295 // Check for any non-constant elements.
4296 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4297 switch (N->getOperand(i).getNode()->getOpcode()) {
4298 case ISD::UNDEF:
4299 case ISD::ConstantFP:
4300 case ISD::Constant:
4301 break;
4302 default:
4303 return false;
4304 }
4305
4306 // Vectors of all-zeros and all-ones are materialized with special
4307 // instructions rather than being loaded.
4308 return !ISD::isBuildVectorAllZeros(N) &&
4309 !ISD::isBuildVectorAllOnes(N);
4310}
4311
Evan Cheng533a0aa2006-04-19 20:35:22 +00004312/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4313/// match movlp{s|d}. The lower half elements should come from lower half of
4314/// V1 (and in order), and the upper half elements should come from the upper
4315/// half of V2 (and in order). And since V1 will become the source of the
4316/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004317static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004318 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004319 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004320 return false;
4321
Evan Cheng466685d2006-10-09 20:57:25 +00004322 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004323 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004324 // Is V2 is a vector load, don't do this transformation. We will try to use
4325 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004326 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004327 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004328
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004329 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004330
Evan Cheng533a0aa2006-04-19 20:35:22 +00004331 if (NumElems != 2 && NumElems != 4)
4332 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004333 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004334 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004335 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004336 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004337 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004338 return false;
4339 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004340}
4341
Evan Cheng39623da2006-04-20 08:58:49 +00004342/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4343/// all the same.
4344static bool isSplatVector(SDNode *N) {
4345 if (N->getOpcode() != ISD::BUILD_VECTOR)
4346 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004347
Dan Gohman475871a2008-07-27 21:46:04 +00004348 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004349 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4350 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004351 return false;
4352 return true;
4353}
4354
Evan Cheng213d2cf2007-05-17 18:45:50 +00004355/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004356/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004357/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004358static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004359 SDValue V1 = N->getOperand(0);
4360 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004361 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4362 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004364 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004366 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4367 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004368 if (Opc != ISD::BUILD_VECTOR ||
4369 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 return false;
4371 } else if (Idx >= 0) {
4372 unsigned Opc = V1.getOpcode();
4373 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4374 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004375 if (Opc != ISD::BUILD_VECTOR ||
4376 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004377 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004378 }
4379 }
4380 return true;
4381}
4382
4383/// getZeroVector - Returns a vector of specified type with all zero elements.
4384///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004385static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004386 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004387 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004388
Dale Johannesen0488fb62010-09-30 23:57:10 +00004389 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004390 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004391 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004392 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004393 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004394 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4395 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4396 } else { // SSE1
4397 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4398 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4399 }
Craig Topper5a529e42013-01-18 06:44:29 +00004400 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004401 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004402 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4403 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4404 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4405 } else {
4406 // 256-bit logic and arithmetic instructions in AVX are all
4407 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4408 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4409 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4410 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4411 }
Craig Topper9d352402012-04-23 07:24:41 +00004412 } else
4413 llvm_unreachable("Unexpected vector type");
4414
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004415 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004416}
4417
Chris Lattner8a594482007-11-25 00:24:49 +00004418/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004419/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4420/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4421/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004422static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004423 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004424 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004425
Owen Anderson825b72b2009-08-11 20:47:22 +00004426 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004427 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004428 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004429 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004430 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4431 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4432 } else { // AVX
4433 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004434 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004435 }
Craig Topper5a529e42013-01-18 06:44:29 +00004436 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004437 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004438 } else
4439 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004440
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004441 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004442}
4443
Evan Cheng39623da2006-04-20 08:58:49 +00004444/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4445/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004446static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004447 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004448 if (Mask[i] > (int)NumElems) {
4449 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004450 }
Evan Cheng39623da2006-04-20 08:58:49 +00004451 }
Evan Cheng39623da2006-04-20 08:58:49 +00004452}
4453
Evan Cheng017dcc62006-04-21 01:05:10 +00004454/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4455/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004456static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 SDValue V2) {
4458 unsigned NumElems = VT.getVectorNumElements();
4459 SmallVector<int, 8> Mask;
4460 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004461 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 Mask.push_back(i);
4463 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004464}
4465
Nate Begeman9008ca62009-04-27 18:41:29 +00004466/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004467static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 SDValue V2) {
4469 unsigned NumElems = VT.getVectorNumElements();
4470 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004471 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 Mask.push_back(i);
4473 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004474 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004476}
4477
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004479static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 SDValue V2) {
4481 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004483 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 Mask.push_back(i + Half);
4485 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004486 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004488}
4489
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004490// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491// a generic shuffle instruction because the target has no such instructions.
4492// Generate shuffles which repeat i16 and i8 several times until they can be
4493// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004494static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004498
Nate Begeman9008ca62009-04-27 18:41:29 +00004499 while (NumElems > 4) {
4500 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 EltNo -= NumElems/2;
4505 }
4506 NumElems >>= 1;
4507 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004508 return V;
4509}
Eric Christopherfd179292009-08-27 18:07:15 +00004510
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4512static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4513 EVT VT = V.getValueType();
4514 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004515
Craig Topper5a529e42013-01-18 06:44:29 +00004516 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004517 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004518 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004519 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4520 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004521 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004522 // To use VPERMILPS to splat scalars, the second half of indicies must
4523 // refer to the higher part, which is a duplication of the lower one,
4524 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4526 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004527
4528 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4529 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4530 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004531 } else
4532 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533
4534 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4535}
4536
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004537/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004538static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4539 EVT SrcVT = SV->getValueType(0);
4540 SDValue V1 = SV->getOperand(0);
4541 DebugLoc dl = SV->getDebugLoc();
4542
4543 int EltNo = SV->getSplatIndex();
4544 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004545 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004546
Craig Topper5a529e42013-01-18 06:44:29 +00004547 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4548 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004549
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004550 // Extract the 128-bit part containing the splat element and update
4551 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004552 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004553 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4554 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004555 EltNo -= NumElems/2;
4556 }
4557
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004558 // All i16 and i8 vector types can't be used directly by a generic shuffle
4559 // instruction because the target has no such instruction. Generate shuffles
4560 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004561 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004562 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004563 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004564 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004565
4566 // Recreate the 256-bit vector and place the same 128-bit vector
4567 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004568 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004569 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004570 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004571 }
4572
4573 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004574}
4575
Evan Chengba05f722006-04-21 23:03:30 +00004576/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004577/// vector of zero or undef vector. This produces a shuffle where the low
4578/// element of V2 is swizzled into the zero/undef vector, landing at element
4579/// 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 +00004580static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004581 bool IsZero,
4582 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004583 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004584 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004585 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004586 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004587 unsigned NumElems = VT.getVectorNumElements();
4588 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004589 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004590 // If this is the insertion idx, put the low elt of V2 here.
4591 MaskVec.push_back(i == Idx ? NumElems : i);
4592 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004593}
4594
Craig Toppera1ffc682012-03-20 06:42:26 +00004595/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4596/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004597/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004598static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004599 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004600 unsigned NumElems = VT.getVectorNumElements();
4601 SDValue ImmN;
4602
Craig Topper89f4e662012-03-20 07:17:59 +00004603 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004604 switch(N->getOpcode()) {
4605 case X86ISD::SHUFP:
4606 ImmN = N->getOperand(N->getNumOperands()-1);
4607 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4608 break;
4609 case X86ISD::UNPCKH:
4610 DecodeUNPCKHMask(VT, Mask);
4611 break;
4612 case X86ISD::UNPCKL:
4613 DecodeUNPCKLMask(VT, Mask);
4614 break;
4615 case X86ISD::MOVHLPS:
4616 DecodeMOVHLPSMask(NumElems, Mask);
4617 break;
4618 case X86ISD::MOVLHPS:
4619 DecodeMOVLHPSMask(NumElems, Mask);
4620 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004621 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004622 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004623 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004624 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004625 case X86ISD::PSHUFD:
4626 case X86ISD::VPERMILP:
4627 ImmN = N->getOperand(N->getNumOperands()-1);
4628 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004629 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004630 break;
4631 case X86ISD::PSHUFHW:
4632 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004633 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004634 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004635 break;
4636 case X86ISD::PSHUFLW:
4637 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004638 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004639 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004640 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004641 case X86ISD::VPERMI:
4642 ImmN = N->getOperand(N->getNumOperands()-1);
4643 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4644 IsUnary = true;
4645 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004646 case X86ISD::MOVSS:
4647 case X86ISD::MOVSD: {
4648 // The index 0 always comes from the first element of the second source,
4649 // this is why MOVSS and MOVSD are used in the first place. The other
4650 // elements come from the other positions of the first source vector
4651 Mask.push_back(NumElems);
4652 for (unsigned i = 1; i != NumElems; ++i) {
4653 Mask.push_back(i);
4654 }
4655 break;
4656 }
4657 case X86ISD::VPERM2X128:
4658 ImmN = N->getOperand(N->getNumOperands()-1);
4659 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004660 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004661 break;
4662 case X86ISD::MOVDDUP:
4663 case X86ISD::MOVLHPD:
4664 case X86ISD::MOVLPD:
4665 case X86ISD::MOVLPS:
4666 case X86ISD::MOVSHDUP:
4667 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004668 // Not yet implemented
4669 return false;
4670 default: llvm_unreachable("unknown target shuffle node");
4671 }
4672
4673 return true;
4674}
4675
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4677/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004678static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004679 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004680 if (Depth == 6)
4681 return SDValue(); // Limit search depth.
4682
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004683 SDValue V = SDValue(N, 0);
4684 EVT VT = V.getValueType();
4685 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686
4687 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4688 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004689 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690
Craig Topper3d092db2012-03-21 02:14:01 +00004691 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692 return DAG.getUNDEF(VT.getVectorElementType());
4693
Craig Topperd156dc12012-02-06 07:17:51 +00004694 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004695 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4696 : SV->getOperand(1);
4697 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004698 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699
4700 // Recurse into target specific vector shuffles to find scalars.
4701 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004702 MVT ShufVT = V.getValueType().getSimpleVT();
4703 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004704 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004705 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004706
Craig Topperd978c542012-05-06 19:46:21 +00004707 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004708 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004709
Craig Topper3d092db2012-03-21 02:14:01 +00004710 int Elt = ShuffleMask[Index];
4711 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004712 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004713
Craig Topper3d092db2012-03-21 02:14:01 +00004714 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004715 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004716 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004717 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004718 }
4719
4720 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004721 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004722 V = V.getOperand(0);
4723 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004724 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004726 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727 return SDValue();
4728 }
4729
4730 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4731 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004732 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733
4734 if (V.getOpcode() == ISD::BUILD_VECTOR)
4735 return V.getOperand(Index);
4736
4737 return SDValue();
4738}
4739
4740/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4741/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004742/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743static
Craig Topper3d092db2012-03-21 02:14:01 +00004744unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004745 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004746 unsigned i;
4747 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004748 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004749 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750 if (!(Elt.getNode() &&
4751 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4752 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004753 }
4754
4755 return i;
4756}
4757
Craig Topper3d092db2012-03-21 02:14:01 +00004758/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4759/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004760/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4761static
Craig Topper3d092db2012-03-21 02:14:01 +00004762bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4763 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4764 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004765 bool SeenV1 = false;
4766 bool SeenV2 = false;
4767
Craig Topper3d092db2012-03-21 02:14:01 +00004768 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004769 int Idx = SVOp->getMaskElt(i);
4770 // Ignore undef indicies
4771 if (Idx < 0)
4772 continue;
4773
Craig Topper3d092db2012-03-21 02:14:01 +00004774 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004775 SeenV1 = true;
4776 else
4777 SeenV2 = true;
4778
4779 // Only accept consecutive elements from the same vector
4780 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4781 return false;
4782 }
4783
4784 OpNum = SeenV1 ? 0 : 1;
4785 return true;
4786}
4787
4788/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4789/// logical left shift of a vector.
4790static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4791 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4792 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4793 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4794 false /* check zeros from right */, DAG);
4795 unsigned OpSrc;
4796
4797 if (!NumZeros)
4798 return false;
4799
4800 // Considering the elements in the mask that are not consecutive zeros,
4801 // check if they consecutively come from only one of the source vectors.
4802 //
4803 // V1 = {X, A, B, C} 0
4804 // \ \ \ /
4805 // vector_shuffle V1, V2 <1, 2, 3, X>
4806 //
4807 if (!isShuffleMaskConsecutive(SVOp,
4808 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004809 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004810 NumZeros, // Where to start looking in the src vector
4811 NumElems, // Number of elements in vector
4812 OpSrc)) // Which source operand ?
4813 return false;
4814
4815 isLeft = false;
4816 ShAmt = NumZeros;
4817 ShVal = SVOp->getOperand(OpSrc);
4818 return true;
4819}
4820
4821/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4822/// logical left shift of a vector.
4823static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4824 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4825 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4826 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4827 true /* check zeros from left */, DAG);
4828 unsigned OpSrc;
4829
4830 if (!NumZeros)
4831 return false;
4832
4833 // Considering the elements in the mask that are not consecutive zeros,
4834 // check if they consecutively come from only one of the source vectors.
4835 //
4836 // 0 { A, B, X, X } = V2
4837 // / \ / /
4838 // vector_shuffle V1, V2 <X, X, 4, 5>
4839 //
4840 if (!isShuffleMaskConsecutive(SVOp,
4841 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004842 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004843 0, // Where to start looking in the src vector
4844 NumElems, // Number of elements in vector
4845 OpSrc)) // Which source operand ?
4846 return false;
4847
4848 isLeft = true;
4849 ShAmt = NumZeros;
4850 ShVal = SVOp->getOperand(OpSrc);
4851 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004852}
4853
4854/// isVectorShift - Returns true if the shuffle can be implemented as a
4855/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004856static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004857 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004858 // Although the logic below support any bitwidth size, there are no
4859 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004860 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004861 return false;
4862
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004863 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4864 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4865 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004866
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004867 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004868}
4869
Evan Chengc78d3b42006-04-24 18:01:45 +00004870/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4871///
Dan Gohman475871a2008-07-27 21:46:04 +00004872static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004873 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004874 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004875 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004876 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004877 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004878 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004879
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004880 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004881 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004882 bool First = true;
4883 for (unsigned i = 0; i < 16; ++i) {
4884 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4885 if (ThisIsNonZero && First) {
4886 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004887 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004890 First = false;
4891 }
4892
4893 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004894 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004895 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4896 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004897 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004899 }
4900 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4902 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4903 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004904 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004905 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 } else
4907 ThisElt = LastElt;
4908
Gabor Greifba36cb52008-08-28 21:40:38 +00004909 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004911 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004912 }
4913 }
4914
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004915 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004916}
4917
Bill Wendlinga348c562007-03-22 18:42:45 +00004918/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004919///
Dan Gohman475871a2008-07-27 21:46:04 +00004920static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004921 unsigned NumNonZero, unsigned NumZero,
4922 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004923 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004924 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004925 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004926 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004927
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004928 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004929 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004930 bool First = true;
4931 for (unsigned i = 0; i < 8; ++i) {
4932 bool isNonZero = (NonZeros & (1 << i)) != 0;
4933 if (isNonZero) {
4934 if (First) {
4935 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004936 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004937 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004939 First = false;
4940 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004941 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004943 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004944 }
4945 }
4946
4947 return V;
4948}
4949
Evan Chengf26ffe92008-05-29 08:22:04 +00004950/// getVShift - Return a vector logical shift node.
4951///
Owen Andersone50ed302009-08-10 22:56:29 +00004952static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 unsigned NumBits, SelectionDAG &DAG,
4954 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004955 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004956 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004957 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004958 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4959 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004960 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004961 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004962 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004963}
4964
Dan Gohman475871a2008-07-27 21:46:04 +00004965SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004966X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004967 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004968
Evan Chengc3630942009-12-09 21:00:30 +00004969 // Check if the scalar load can be widened into a vector load. And if
4970 // the address is "base + cst" see if the cst can be "absorbed" into
4971 // the shuffle mask.
4972 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4973 SDValue Ptr = LD->getBasePtr();
4974 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4975 return SDValue();
4976 EVT PVT = LD->getValueType(0);
4977 if (PVT != MVT::i32 && PVT != MVT::f32)
4978 return SDValue();
4979
4980 int FI = -1;
4981 int64_t Offset = 0;
4982 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4983 FI = FINode->getIndex();
4984 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004985 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004986 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4987 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4988 Offset = Ptr.getConstantOperandVal(1);
4989 Ptr = Ptr.getOperand(0);
4990 } else {
4991 return SDValue();
4992 }
4993
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004994 // FIXME: 256-bit vector instructions don't require a strict alignment,
4995 // improve this code to support it better.
4996 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004997 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004998 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004999 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005000 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005001 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005002 // Can't change the alignment. FIXME: It's possible to compute
5003 // the exact stack offset and reference FI + adjust offset instead.
5004 // If someone *really* cares about this. That's the way to implement it.
5005 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005006 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005007 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005008 }
5009 }
5010
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005011 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005012 // Ptr + (Offset & ~15).
5013 if (Offset < 0)
5014 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005015 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005016 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005017 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005018 if (StartOffset)
5019 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5020 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5021
5022 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005023 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005024
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005025 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5026 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005027 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005028 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005029
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005030 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005031 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005032 Mask.push_back(EltNo);
5033
Craig Toppercc3000632012-01-30 07:50:31 +00005034 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005035 }
5036
5037 return SDValue();
5038}
5039
Michael J. Spencerec38de22010-10-10 22:04:20 +00005040/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5041/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005042/// load which has the same value as a build_vector whose operands are 'elts'.
5043///
5044/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005045///
Nate Begeman1449f292010-03-24 22:19:06 +00005046/// FIXME: we'd also like to handle the case where the last elements are zero
5047/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5048/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005049static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005050 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005051 EVT EltVT = VT.getVectorElementType();
5052 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005053
Nate Begemanfdea31a2010-03-24 20:49:50 +00005054 LoadSDNode *LDBase = NULL;
5055 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005056
Nate Begeman1449f292010-03-24 22:19:06 +00005057 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005058 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005059 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005060 for (unsigned i = 0; i < NumElems; ++i) {
5061 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005062
Nate Begemanfdea31a2010-03-24 20:49:50 +00005063 if (!Elt.getNode() ||
5064 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5065 return SDValue();
5066 if (!LDBase) {
5067 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5068 return SDValue();
5069 LDBase = cast<LoadSDNode>(Elt.getNode());
5070 LastLoadedElt = i;
5071 continue;
5072 }
5073 if (Elt.getOpcode() == ISD::UNDEF)
5074 continue;
5075
5076 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5077 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5078 return SDValue();
5079 LastLoadedElt = i;
5080 }
Nate Begeman1449f292010-03-24 22:19:06 +00005081
5082 // If we have found an entire vector of loads and undefs, then return a large
5083 // load of the entire vector width starting at the base pointer. If we found
5084 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005085 if (LastLoadedElt == NumElems - 1) {
5086 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005087 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005088 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005089 LDBase->isVolatile(), LDBase->isNonTemporal(),
5090 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005091 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005092 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005093 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005094 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005095 }
5096 if (NumElems == 4 && LastLoadedElt == 1 &&
5097 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005098 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5099 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005100 SDValue ResNode =
5101 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5102 LDBase->getPointerInfo(),
5103 LDBase->getAlignment(),
5104 false/*isVolatile*/, true/*ReadMem*/,
5105 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005106
5107 // Make sure the newly-created LOAD is in the same position as LDBase in
5108 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5109 // update uses of LDBase's output chain to use the TokenFactor.
5110 if (LDBase->hasAnyUseOfValue(1)) {
5111 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5112 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5113 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5114 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5115 SDValue(ResNode.getNode(), 1));
5116 }
5117
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005118 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005119 }
5120 return SDValue();
5121}
5122
Nadav Rotem9d68b062012-04-08 12:54:54 +00005123/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5124/// to generate a splat value for the following cases:
5125/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005126/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127/// a scalar load, or a constant.
5128/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005129/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005130SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005131X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005132 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005133 return SDValue();
5134
Craig Topper45e1c752013-01-20 00:38:18 +00005135 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005136 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005137
Craig Topper5da8a802012-05-04 05:49:51 +00005138 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5139 "Unsupported vector type for broadcast.");
5140
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005141 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005142 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005143
Nadav Rotem9d68b062012-04-08 12:54:54 +00005144 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005145 default:
5146 // Unknown pattern found.
5147 return SDValue();
5148
5149 case ISD::BUILD_VECTOR: {
5150 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005151 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005152 return SDValue();
5153
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154 Ld = Op.getOperand(0);
5155 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5156 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005157
5158 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005159 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005160 // Constants may have multiple users.
5161 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005162 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005163 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005164 }
5165
5166 case ISD::VECTOR_SHUFFLE: {
5167 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5168
5169 // Shuffles must have a splat mask where the first element is
5170 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005171 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005172 return SDValue();
5173
5174 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005175 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005176 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5177
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005178 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005179 return SDValue();
5180
5181 // Use the register form of the broadcast instruction available on AVX2.
5182 if (VT.is256BitVector())
5183 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5184 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5185 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186
5187 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005188 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005189 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005190
5191 // The scalar_to_vector node and the suspected
5192 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005193 // Constants may have multiple users.
5194 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005195 return SDValue();
5196 break;
5197 }
5198 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005199
Craig Topper7a9a28b2012-08-12 02:23:29 +00005200 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005201
5202 // Handle the broadcasting a single constant scalar from the constant pool
5203 // into a vector. On Sandybridge it is still better to load a constant vector
5204 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005205 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005206 EVT CVT = Ld.getValueType();
5207 assert(!CVT.isVector() && "Must not broadcast a vector type");
5208 unsigned ScalarSize = CVT.getSizeInBits();
5209
Craig Topper5da8a802012-05-04 05:49:51 +00005210 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005211 const Constant *C = 0;
5212 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5213 C = CI->getConstantIntValue();
5214 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5215 C = CF->getConstantFPValue();
5216
5217 assert(C && "Invalid constant type");
5218
Nadav Rotem154819d2012-04-09 07:45:58 +00005219 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005220 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005221 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005222 MachinePointerInfo::getConstantPool(),
5223 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005224
Nadav Rotem9d68b062012-04-08 12:54:54 +00005225 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5226 }
5227 }
5228
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005229 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005230 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5231
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005232 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005233 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005234 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5235 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5236
5237 // The scalar source must be a normal load.
5238 if (!IsLoad)
5239 return SDValue();
5240
Craig Topper5da8a802012-05-04 05:49:51 +00005241 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005242 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005243
Craig Toppera9376332012-01-10 08:23:59 +00005244 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005245 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005246 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005247 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005248 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005249 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005250
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005251 // Unsupported broadcast.
5252 return SDValue();
5253}
5254
Evan Chengc3630942009-12-09 21:00:30 +00005255SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005256X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5257 EVT VT = Op.getValueType();
5258
5259 // Skip if insert_vec_elt is not supported.
5260 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5261 return SDValue();
5262
5263 DebugLoc DL = Op.getDebugLoc();
5264 unsigned NumElems = Op.getNumOperands();
5265
5266 SDValue VecIn1;
5267 SDValue VecIn2;
5268 SmallVector<unsigned, 4> InsertIndices;
5269 SmallVector<int, 8> Mask(NumElems, -1);
5270
5271 for (unsigned i = 0; i != NumElems; ++i) {
5272 unsigned Opc = Op.getOperand(i).getOpcode();
5273
5274 if (Opc == ISD::UNDEF)
5275 continue;
5276
5277 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5278 // Quit if more than 1 elements need inserting.
5279 if (InsertIndices.size() > 1)
5280 return SDValue();
5281
5282 InsertIndices.push_back(i);
5283 continue;
5284 }
5285
5286 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5287 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5288
5289 // Quit if extracted from vector of different type.
5290 if (ExtractedFromVec.getValueType() != VT)
5291 return SDValue();
5292
5293 // Quit if non-constant index.
5294 if (!isa<ConstantSDNode>(ExtIdx))
5295 return SDValue();
5296
5297 if (VecIn1.getNode() == 0)
5298 VecIn1 = ExtractedFromVec;
5299 else if (VecIn1 != ExtractedFromVec) {
5300 if (VecIn2.getNode() == 0)
5301 VecIn2 = ExtractedFromVec;
5302 else if (VecIn2 != ExtractedFromVec)
5303 // Quit if more than 2 vectors to shuffle
5304 return SDValue();
5305 }
5306
5307 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5308
5309 if (ExtractedFromVec == VecIn1)
5310 Mask[i] = Idx;
5311 else if (ExtractedFromVec == VecIn2)
5312 Mask[i] = Idx + NumElems;
5313 }
5314
5315 if (VecIn1.getNode() == 0)
5316 return SDValue();
5317
5318 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5319 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5320 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5321 unsigned Idx = InsertIndices[i];
5322 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5323 DAG.getIntPtrConstant(Idx));
5324 }
5325
5326 return NV;
5327}
5328
5329SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005330X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005331 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005332
Craig Topper45e1c752013-01-20 00:38:18 +00005333 MVT VT = Op.getValueType().getSimpleVT();
5334 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005335 unsigned NumElems = Op.getNumOperands();
5336
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005337 // Vectors containing all zeros can be matched by pxor and xorps later
5338 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5339 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5340 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005341 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005342 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005343
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005344 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005345 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005347 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005348 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5349 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005350 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005351 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005352 return Op;
5353
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005354 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005355 }
5356
Nadav Rotem154819d2012-04-09 07:45:58 +00005357 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005358 if (Broadcast.getNode())
5359 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005360
Owen Andersone50ed302009-08-10 22:56:29 +00005361 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005362
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363 unsigned NumZero = 0;
5364 unsigned NumNonZero = 0;
5365 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005366 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005367 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005368 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005369 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005370 if (Elt.getOpcode() == ISD::UNDEF)
5371 continue;
5372 Values.insert(Elt);
5373 if (Elt.getOpcode() != ISD::Constant &&
5374 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005375 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005376 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005377 NumZero++;
5378 else {
5379 NonZeros |= (1 << i);
5380 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 }
5382 }
5383
Chris Lattner97a2a562010-08-26 05:24:29 +00005384 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5385 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005386 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387
Chris Lattner67f453a2008-03-09 05:42:06 +00005388 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005389 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005391 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005392
Chris Lattner62098042008-03-09 01:05:04 +00005393 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5394 // the value are obviously zero, truncate the value to i32 and do the
5395 // insertion that way. Only do this if the value is non-constant or if the
5396 // value is a constant being inserted into element 0. It is cheaper to do
5397 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005398 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005399 (!IsAllConstants || Idx == 0)) {
5400 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005401 // Handle SSE only.
5402 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5403 EVT VecVT = MVT::v4i32;
5404 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005405
Chris Lattner62098042008-03-09 01:05:04 +00005406 // Truncate the value (which may itself be a constant) to i32, and
5407 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005408 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005409 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005410 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005411
Chris Lattner62098042008-03-09 01:05:04 +00005412 // Now we have our 32-bit value zero extended in the low element of
5413 // a vector. If Idx != 0, swizzle it into place.
5414 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 SmallVector<int, 4> Mask;
5416 Mask.push_back(Idx);
5417 for (unsigned i = 1; i != VecElts; ++i)
5418 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005419 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005420 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005421 }
Craig Topper07a27622012-01-22 03:07:48 +00005422 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005423 }
5424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005425
Chris Lattner19f79692008-03-08 22:59:52 +00005426 // If we have a constant or non-constant insertion into the low element of
5427 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5428 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005429 // depending on what the source datatype is.
5430 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005431 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005432 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005433
5434 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005435 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005436 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005437 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005438 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5439 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005440 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005441 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005442 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5443 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005444 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005445 }
5446
5447 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005448 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005449 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005450 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005451 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005452 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005453 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005454 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005455 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005456 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005457 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005458 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005459 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005460
5461 // Is it a vector logical left shift?
5462 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005463 X86::isZeroNode(Op.getOperand(0)) &&
5464 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005465 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005466 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005467 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005468 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005469 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005470 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005471
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005472 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005473 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005474
Chris Lattner19f79692008-03-08 22:59:52 +00005475 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5476 // is a non-constant being inserted into an element other than the low one,
5477 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5478 // movd/movss) to move this into the low element, then shuffle it into
5479 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005480 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005481 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005482
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005484 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005485 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005486 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 MaskVec.push_back(i == Idx ? 0 : 1);
5488 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005489 }
5490 }
5491
Chris Lattner67f453a2008-03-09 05:42:06 +00005492 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005493 if (Values.size() == 1) {
5494 if (EVTBits == 32) {
5495 // Instead of a shuffle like this:
5496 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5497 // Check if it's possible to issue this instead.
5498 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5499 unsigned Idx = CountTrailingZeros_32(NonZeros);
5500 SDValue Item = Op.getOperand(Idx);
5501 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5502 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5503 }
Dan Gohman475871a2008-07-27 21:46:04 +00005504 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005505 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005506
Dan Gohmana3941172007-07-24 22:55:08 +00005507 // A vector full of immediates; various special cases are already
5508 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005509 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005510 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005511
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005512 // For AVX-length vectors, build the individual 128-bit pieces and use
5513 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005514 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005515 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005516 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005517 V.push_back(Op.getOperand(i));
5518
5519 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5520
5521 // Build both the lower and upper subvector.
5522 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5523 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5524 NumElems/2);
5525
5526 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005527 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005528 }
5529
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005530 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005531 if (EVTBits == 64) {
5532 if (NumNonZero == 1) {
5533 // One half is zero or undef.
5534 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005535 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005536 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005537 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005538 }
Dan Gohman475871a2008-07-27 21:46:04 +00005539 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005540 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005541
5542 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005543 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005544 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005545 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005546 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005547 }
5548
Bill Wendling826f36f2007-03-28 00:57:11 +00005549 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005550 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005551 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005552 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005553 }
5554
5555 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005556 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005557 if (NumElems == 4 && NumZero > 0) {
5558 for (unsigned i = 0; i < 4; ++i) {
5559 bool isZero = !(NonZeros & (1 << i));
5560 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005561 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005562 else
Dale Johannesenace16102009-02-03 19:33:06 +00005563 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 }
5565
5566 for (unsigned i = 0; i < 2; ++i) {
5567 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5568 default: break;
5569 case 0:
5570 V[i] = V[i*2]; // Must be a zero vector.
5571 break;
5572 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005573 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005574 break;
5575 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005576 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005577 break;
5578 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005580 break;
5581 }
5582 }
5583
Benjamin Kramer9c683542012-01-30 15:16:21 +00005584 bool Reverse1 = (NonZeros & 0x3) == 2;
5585 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5586 int MaskVec[] = {
5587 Reverse1 ? 1 : 0,
5588 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005589 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5590 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005591 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005593 }
5594
Craig Topper7a9a28b2012-08-12 02:23:29 +00005595 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005596 // Check for a build vector of consecutive loads.
5597 for (unsigned i = 0; i < NumElems; ++i)
5598 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005599
Nate Begemanfdea31a2010-03-24 20:49:50 +00005600 // Check for elements which are consecutive loads.
5601 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5602 if (LD.getNode())
5603 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005604
Michael Liaofacace82012-10-19 17:15:18 +00005605 // Check for a build vector from mostly shuffle plus few inserting.
5606 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5607 if (Sh.getNode())
5608 return Sh;
5609
Michael J. Spencerec38de22010-10-10 22:04:20 +00005610 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005611 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005612 SDValue Result;
5613 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5614 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5615 else
5616 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005617
Chris Lattner24faf612010-08-28 17:59:08 +00005618 for (unsigned i = 1; i < NumElems; ++i) {
5619 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5620 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005622 }
5623 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005625
Chris Lattner6e80e442010-08-28 17:15:43 +00005626 // Otherwise, expand into a number of unpckl*, start by extending each of
5627 // our (non-undef) elements to the full vector width with the element in the
5628 // bottom slot of the vector (which generates no code for SSE).
5629 for (unsigned i = 0; i < NumElems; ++i) {
5630 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5631 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5632 else
5633 V[i] = DAG.getUNDEF(VT);
5634 }
5635
5636 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005637 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5638 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5639 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005640 unsigned EltStride = NumElems >> 1;
5641 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005642 for (unsigned i = 0; i < EltStride; ++i) {
5643 // If V[i+EltStride] is undef and this is the first round of mixing,
5644 // then it is safe to just drop this shuffle: V[i] is already in the
5645 // right place, the one element (since it's the first round) being
5646 // inserted as undef can be dropped. This isn't safe for successive
5647 // rounds because they will permute elements within both vectors.
5648 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5649 EltStride == NumElems/2)
5650 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005651
Chris Lattner6e80e442010-08-28 17:15:43 +00005652 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005653 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005654 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005655 }
5656 return V[0];
5657 }
Dan Gohman475871a2008-07-27 21:46:04 +00005658 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005659}
5660
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005661// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5662// to create 256-bit vectors from two other 128-bit ones.
5663static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5664 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005665 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005666
Craig Topper7a9a28b2012-08-12 02:23:29 +00005667 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005668
5669 SDValue V1 = Op.getOperand(0);
5670 SDValue V2 = Op.getOperand(1);
5671 unsigned NumElems = ResVT.getVectorNumElements();
5672
Craig Topper4c7972d2012-04-22 18:15:59 +00005673 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005674}
5675
Craig Topper55b24052012-09-11 06:15:32 +00005676static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005677 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005678
5679 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5680 // from two other 128-bit ones.
5681 return LowerAVXCONCAT_VECTORS(Op, DAG);
5682}
5683
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005684// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005685static SDValue
5686LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5687 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005688 SDValue V1 = SVOp->getOperand(0);
5689 SDValue V2 = SVOp->getOperand(1);
5690 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005691 MVT VT = SVOp->getValueType(0).getSimpleVT();
5692 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005693 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005694
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005695 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5696 return SDValue();
5697 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005698 return SDValue();
5699
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005700 // Check the mask for BLEND and build the value.
5701 unsigned MaskValue = 0;
5702 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005703 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005704 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005705
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005706 // Blend for v16i16 should be symetric for the both lanes.
5707 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005708
Craig Topper9b33ef72013-01-21 06:57:59 +00005709 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005710 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005711 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005712
Craig Topper04f74a12013-01-21 07:25:16 +00005713 if ((EltIdx < 0 || EltIdx == (int)i) &&
5714 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005715 continue;
5716
Craig Topper9b33ef72013-01-21 06:57:59 +00005717 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005718 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005719 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5720 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005721 else
Craig Topper1842ba02012-04-23 06:38:28 +00005722 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005723 }
5724
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005725 // Convert i32 vectors to floating point if it is not AVX2.
5726 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005727 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005728 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005729 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5730 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005731 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5732 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5733 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005734
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005735 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5736 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005737 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005738}
5739
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740// v8i16 shuffles - Prefer shuffles in the following order:
5741// 1. [all] pshuflw, pshufhw, optional move
5742// 2. [ssse3] 1 x pshufb
5743// 3. [ssse3] 2 x pshufb + 1 x por
5744// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005745static SDValue
5746LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5747 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005748 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005749 SDValue V1 = SVOp->getOperand(0);
5750 SDValue V2 = SVOp->getOperand(1);
5751 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005753
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 // Determine if more than 1 of the words in each of the low and high quadwords
5755 // of the result come from the same quadword of one of the two inputs. Undef
5756 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005757 unsigned LoQuad[] = { 0, 0, 0, 0 };
5758 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005759 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005761 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005762 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 MaskVals.push_back(EltIdx);
5764 if (EltIdx < 0) {
5765 ++Quad[0];
5766 ++Quad[1];
5767 ++Quad[2];
5768 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005769 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 }
5771 ++Quad[EltIdx / 4];
5772 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005773 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005776 unsigned MaxQuad = 1;
5777 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 if (LoQuad[i] > MaxQuad) {
5779 BestLoQuad = i;
5780 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005781 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005782 }
5783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005785 MaxQuad = 1;
5786 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 if (HiQuad[i] > MaxQuad) {
5788 BestHiQuad = i;
5789 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005790 }
5791 }
5792
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005794 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // single pshufb instruction is necessary. If There are more than 2 input
5796 // quads, disable the next transformation since it does not help SSSE3.
5797 bool V1Used = InputQuads[0] || InputQuads[1];
5798 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005799 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005801 BestLoQuad = InputQuads[0] ? 0 : 1;
5802 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 }
5804 if (InputQuads.count() > 2) {
5805 BestLoQuad = -1;
5806 BestHiQuad = -1;
5807 }
5808 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005809
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5811 // the shuffle mask. If a quad is scored as -1, that means that it contains
5812 // words from all 4 input quadwords.
5813 SDValue NewV;
5814 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005815 int MaskV[] = {
5816 BestLoQuad < 0 ? 0 : BestLoQuad,
5817 BestHiQuad < 0 ? 1 : BestHiQuad
5818 };
Eric Christopherfd179292009-08-27 18:07:15 +00005819 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005820 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5821 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5822 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005823
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5825 // source words for the shuffle, to aid later transformations.
5826 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005827 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005828 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005830 if (idx != (int)i)
5831 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005833 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 AllWordsInNewV = false;
5835 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005836 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005837
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5839 if (AllWordsInNewV) {
5840 for (int i = 0; i != 8; ++i) {
5841 int idx = MaskVals[i];
5842 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005843 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005844 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 if ((idx != i) && idx < 4)
5846 pshufhw = false;
5847 if ((idx != i) && idx > 3)
5848 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005849 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 V1 = NewV;
5851 V2Used = false;
5852 BestLoQuad = 0;
5853 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005854 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005855
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5857 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005858 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005859 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5860 unsigned TargetMask = 0;
5861 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005862 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005863 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5864 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5865 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005866 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005867 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005868 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005869 }
Eric Christopherfd179292009-08-27 18:07:15 +00005870
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005871 // Promote splats to a larger type which usually leads to more efficient code.
5872 // FIXME: Is this true if pshufb is available?
5873 if (SVOp->isSplat())
5874 return PromoteSplat(SVOp, DAG);
5875
Nate Begemanb9a47b82009-02-23 08:49:38 +00005876 // If we have SSSE3, and all words of the result are from 1 input vector,
5877 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5878 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005879 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005881
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005883 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 // mask, and elements that come from V1 in the V2 mask, so that the two
5885 // results can be OR'd together.
5886 bool TwoInputs = V1Used && V2Used;
5887 for (unsigned i = 0; i != 8; ++i) {
5888 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005889 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5890 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005891 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005892 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005894 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005895 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005896 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005899 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // Calculate the shuffle mask for the second input, shuffle it, and
5902 // OR it with the first shuffled input.
5903 pshufbMask.clear();
5904 for (unsigned i = 0; i != 8; ++i) {
5905 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005906 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5907 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5908 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5909 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005911 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005912 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005913 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 MVT::v16i8, &pshufbMask[0], 16));
5915 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 }
5918
5919 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5920 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005921 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005923 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 for (int i = 0; i != 4; ++i) {
5925 int idx = MaskVals[i];
5926 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005927 InOrder.set(i);
5928 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005929 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 }
5932 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005934 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005935
Craig Topperdd637ae2012-02-19 05:41:45 +00005936 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5937 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005938 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005939 NewV.getOperand(0),
5940 getShufflePSHUFLWImmediate(SVOp), DAG);
5941 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 }
Eric Christopherfd179292009-08-27 18:07:15 +00005943
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5945 // and update MaskVals with the new element order.
5946 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005947 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 for (unsigned i = 4; i != 8; ++i) {
5949 int idx = MaskVals[i];
5950 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 InOrder.set(i);
5952 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005953 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 }
5956 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005957 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005958 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005959
Craig Topperdd637ae2012-02-19 05:41:45 +00005960 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5961 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005962 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005963 NewV.getOperand(0),
5964 getShufflePSHUFHWImmediate(SVOp), DAG);
5965 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 }
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // In case BestHi & BestLo were both -1, which means each quadword has a word
5969 // from each of the four input quadwords, calculate the InOrder bitvector now
5970 // before falling through to the insert/extract cleanup.
5971 if (BestLoQuad == -1 && BestHiQuad == -1) {
5972 NewV = V1;
5973 for (int i = 0; i != 8; ++i)
5974 if (MaskVals[i] < 0 || MaskVals[i] == i)
5975 InOrder.set(i);
5976 }
Eric Christopherfd179292009-08-27 18:07:15 +00005977
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 // The other elements are put in the right place using pextrw and pinsrw.
5979 for (unsigned i = 0; i != 8; ++i) {
5980 if (InOrder[i])
5981 continue;
5982 int EltIdx = MaskVals[i];
5983 if (EltIdx < 0)
5984 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005985 SDValue ExtOp = (EltIdx < 8) ?
5986 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5987 DAG.getIntPtrConstant(EltIdx)) :
5988 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005990 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 DAG.getIntPtrConstant(i));
5992 }
5993 return NewV;
5994}
5995
5996// v16i8 shuffles - Prefer shuffles in the following order:
5997// 1. [ssse3] 1 x pshufb
5998// 2. [ssse3] 2 x pshufb + 1 x por
5999// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6000static
Nate Begeman9008ca62009-04-27 18:41:29 +00006001SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006002 SelectionDAG &DAG,
6003 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006004 SDValue V1 = SVOp->getOperand(0);
6005 SDValue V2 = SVOp->getOperand(1);
6006 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006007 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006008
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006009 // Promote splats to a larger type which usually leads to more efficient code.
6010 // FIXME: Is this true if pshufb is available?
6011 if (SVOp->isSplat())
6012 return PromoteSplat(SVOp, DAG);
6013
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006015 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006017
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006019 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006021
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006023 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 //
6025 // Otherwise, we have elements from both input vectors, and must zero out
6026 // elements that come from V2 in the first mask, and V1 in the second mask
6027 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006028 for (unsigned i = 0; i != 16; ++i) {
6029 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006030 if (EltIdx < 0 || EltIdx >= 16)
6031 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006033 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006035 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006037
6038 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6039 // the 2nd operand if it's undefined or zero.
6040 if (V2.getOpcode() == ISD::UNDEF ||
6041 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006042 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006043
Nate Begemanb9a47b82009-02-23 08:49:38 +00006044 // Calculate the shuffle mask for the second input, shuffle it, and
6045 // OR it with the first shuffled input.
6046 pshufbMask.clear();
6047 for (unsigned i = 0; i != 16; ++i) {
6048 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006049 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006050 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006051 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006053 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 MVT::v16i8, &pshufbMask[0], 16));
6055 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006056 }
Eric Christopherfd179292009-08-27 18:07:15 +00006057
Nate Begemanb9a47b82009-02-23 08:49:38 +00006058 // No SSSE3 - Calculate in place words and then fix all out of place words
6059 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6060 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006061 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6062 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006063 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006064 for (int i = 0; i != 8; ++i) {
6065 int Elt0 = MaskVals[i*2];
6066 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006067
Nate Begemanb9a47b82009-02-23 08:49:38 +00006068 // This word of the result is all undef, skip it.
6069 if (Elt0 < 0 && Elt1 < 0)
6070 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006071
Nate Begemanb9a47b82009-02-23 08:49:38 +00006072 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006073 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006075
Nate Begemanb9a47b82009-02-23 08:49:38 +00006076 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6077 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6078 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006079
6080 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6081 // using a single extract together, load it and store it.
6082 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006083 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006084 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006085 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006086 DAG.getIntPtrConstant(i));
6087 continue;
6088 }
6089
Nate Begemanb9a47b82009-02-23 08:49:38 +00006090 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006091 // source byte is not also odd, shift the extracted word left 8 bits
6092 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006095 DAG.getIntPtrConstant(Elt1 / 2));
6096 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006098 DAG.getConstant(8,
6099 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006100 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006101 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6102 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006103 }
6104 // If Elt0 is defined, extract it from the appropriate source. If the
6105 // source byte is not also even, shift the extracted word right 8 bits. If
6106 // Elt1 was also defined, OR the extracted values together before
6107 // inserting them in the result.
6108 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006109 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006110 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6111 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006112 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006113 DAG.getConstant(8,
6114 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006115 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006116 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6117 DAG.getConstant(0x00FF, MVT::i16));
6118 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006119 : InsElt0;
6120 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006121 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006122 DAG.getIntPtrConstant(i));
6123 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006124 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006125}
6126
Elena Demikhovsky41789462012-09-06 12:42:01 +00006127// v32i8 shuffles - Translate to VPSHUFB if possible.
6128static
6129SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006130 const X86Subtarget *Subtarget,
6131 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006132 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006133 SDValue V1 = SVOp->getOperand(0);
6134 SDValue V2 = SVOp->getOperand(1);
6135 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006136 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006137
6138 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006139 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6140 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006141
Michael Liao471b9172012-10-03 23:43:52 +00006142 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006143 // (1) one of input vector is undefined or zeroinitializer.
6144 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6145 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006146 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006147 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006148 return SDValue();
6149
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006150 if (V1IsAllZero && !V2IsAllZero) {
6151 CommuteVectorShuffleMask(MaskVals, 32);
6152 V1 = V2;
6153 }
6154 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006155 for (unsigned i = 0; i != 32; i++) {
6156 int EltIdx = MaskVals[i];
6157 if (EltIdx < 0 || EltIdx >= 32)
6158 EltIdx = 0x80;
6159 else {
6160 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6161 // Cross lane is not allowed.
6162 return SDValue();
6163 EltIdx &= 0xf;
6164 }
6165 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6166 }
6167 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6168 DAG.getNode(ISD::BUILD_VECTOR, dl,
6169 MVT::v32i8, &pshufbMask[0], 32));
6170}
6171
Evan Cheng7a831ce2007-12-15 03:00:47 +00006172/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006173/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006174/// done when every pair / quad of shuffle mask elements point to elements in
6175/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006176/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006177static
Nate Begeman9008ca62009-04-27 18:41:29 +00006178SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006179 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006180 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006181 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006182 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006183 MVT NewVT;
6184 unsigned Scale;
6185 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006186 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006187 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6188 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6189 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6190 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6191 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6192 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006193 }
6194
Nate Begeman9008ca62009-04-27 18:41:29 +00006195 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006196 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006198 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006199 int EltIdx = SVOp->getMaskElt(i+j);
6200 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006201 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006202 if (StartIdx < 0)
6203 StartIdx = (EltIdx / Scale);
6204 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006205 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006206 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006207 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006208 }
6209
Craig Topper11ac1f82012-05-04 04:08:44 +00006210 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6211 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006212 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006213}
6214
Evan Chengd880b972008-05-09 21:53:03 +00006215/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006216///
Craig Topperf84b7502013-01-20 00:50:58 +00006217static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006218 SDValue SrcOp, SelectionDAG &DAG,
6219 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006220 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006221 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006222 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006223 LD = dyn_cast<LoadSDNode>(SrcOp);
6224 if (!LD) {
6225 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6226 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006227 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006228 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006229 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006230 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006231 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006232 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006233 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006234 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006235 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6236 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6237 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006238 SrcOp.getOperand(0)
6239 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006240 }
6241 }
6242 }
6243
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006244 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006245 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006246 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006247 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006248}
6249
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006250/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6251/// which could not be matched by any known target speficic shuffle
6252static SDValue
6253LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006254
6255 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6256 if (NewOp.getNode())
6257 return NewOp;
6258
Craig Topper657a99c2013-01-19 23:36:09 +00006259 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006260
Craig Topper8f35c132012-01-20 09:29:03 +00006261 unsigned NumElems = VT.getVectorNumElements();
6262 unsigned NumLaneElems = NumElems / 2;
6263
Craig Topper8f35c132012-01-20 09:29:03 +00006264 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006265 MVT EltVT = VT.getVectorElementType();
6266 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006267 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006268
Craig Topper9a2b6e12012-04-06 07:45:23 +00006269 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006270 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006271 // Build a shuffle mask for the output, discovering on the fly which
6272 // input vectors to use as shuffle operands (recorded in InputUsed).
6273 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006274 // out with UseBuildVector set.
6275 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006276 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006277 unsigned LaneStart = l * NumLaneElems;
6278 for (unsigned i = 0; i != NumLaneElems; ++i) {
6279 // The mask element. This indexes into the input.
6280 int Idx = SVOp->getMaskElt(i+LaneStart);
6281 if (Idx < 0) {
6282 // the mask element does not index into any input vector.
6283 Mask.push_back(-1);
6284 continue;
6285 }
Craig Topper8f35c132012-01-20 09:29:03 +00006286
Craig Topper9a2b6e12012-04-06 07:45:23 +00006287 // The input vector this mask element indexes into.
6288 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006289
Craig Topper9a2b6e12012-04-06 07:45:23 +00006290 // Turn the index into an offset from the start of the input vector.
6291 Idx -= Input * NumLaneElems;
6292
6293 // Find or create a shuffle vector operand to hold this input.
6294 unsigned OpNo;
6295 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6296 if (InputUsed[OpNo] == Input)
6297 // This input vector is already an operand.
6298 break;
6299 if (InputUsed[OpNo] < 0) {
6300 // Create a new operand for this input vector.
6301 InputUsed[OpNo] = Input;
6302 break;
6303 }
6304 }
6305
6306 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006307 // More than two input vectors used! Give up on trying to create a
6308 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6309 UseBuildVector = true;
6310 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006311 }
6312
6313 // Add the mask index for the new shuffle vector.
6314 Mask.push_back(Idx + OpNo * NumLaneElems);
6315 }
6316
Craig Topper8ae97ba2012-05-21 06:40:16 +00006317 if (UseBuildVector) {
6318 SmallVector<SDValue, 16> SVOps;
6319 for (unsigned i = 0; i != NumLaneElems; ++i) {
6320 // The mask element. This indexes into the input.
6321 int Idx = SVOp->getMaskElt(i+LaneStart);
6322 if (Idx < 0) {
6323 SVOps.push_back(DAG.getUNDEF(EltVT));
6324 continue;
6325 }
6326
6327 // The input vector this mask element indexes into.
6328 int Input = Idx / NumElems;
6329
6330 // Turn the index into an offset from the start of the input vector.
6331 Idx -= Input * NumElems;
6332
6333 // Extract the vector element by hand.
6334 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6335 SVOp->getOperand(Input),
6336 DAG.getIntPtrConstant(Idx)));
6337 }
6338
6339 // Construct the output using a BUILD_VECTOR.
6340 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6341 SVOps.size());
6342 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006343 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006344 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006345 } else {
6346 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006347 (InputUsed[0] % 2) * NumLaneElems,
6348 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006349 // If only one input was used, use an undefined vector for the other.
6350 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6351 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006352 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006353 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006354 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006355 }
6356
6357 Mask.clear();
6358 }
Craig Topper8f35c132012-01-20 09:29:03 +00006359
6360 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006361 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006362}
6363
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006364/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6365/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006366static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006367LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006368 SDValue V1 = SVOp->getOperand(0);
6369 SDValue V2 = SVOp->getOperand(1);
6370 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006371 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006372
Craig Topper7a9a28b2012-08-12 02:23:29 +00006373 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006374
Benjamin Kramer9c683542012-01-30 15:16:21 +00006375 std::pair<int, int> Locs[4];
6376 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006377 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006378
Evan Chengace3c172008-07-22 21:13:36 +00006379 unsigned NumHi = 0;
6380 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006381 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006382 int Idx = PermMask[i];
6383 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006384 Locs[i] = std::make_pair(-1, -1);
6385 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006386 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6387 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006388 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006389 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006390 NumLo++;
6391 } else {
6392 Locs[i] = std::make_pair(1, NumHi);
6393 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006394 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006395 NumHi++;
6396 }
6397 }
6398 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006399
Evan Chengace3c172008-07-22 21:13:36 +00006400 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006401 // If no more than two elements come from either vector. This can be
6402 // implemented with two shuffles. First shuffle gather the elements.
6403 // The second shuffle, which takes the first shuffle as both of its
6404 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006405 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006406
Benjamin Kramer9c683542012-01-30 15:16:21 +00006407 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006408
Benjamin Kramer9c683542012-01-30 15:16:21 +00006409 for (unsigned i = 0; i != 4; ++i)
6410 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006411 unsigned Idx = (i < 2) ? 0 : 4;
6412 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006413 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006414 }
Evan Chengace3c172008-07-22 21:13:36 +00006415
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006417 }
6418
6419 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006420 // Otherwise, we must have three elements from one vector, call it X, and
6421 // one element from the other, call it Y. First, use a shufps to build an
6422 // intermediate vector with the one element from Y and the element from X
6423 // that will be in the same half in the final destination (the indexes don't
6424 // matter). Then, use a shufps to build the final vector, taking the half
6425 // containing the element from Y from the intermediate, and the other half
6426 // from X.
6427 if (NumHi == 3) {
6428 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006429 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006430 std::swap(V1, V2);
6431 }
6432
6433 // Find the element from V2.
6434 unsigned HiIndex;
6435 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006436 int Val = PermMask[HiIndex];
6437 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006438 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006439 if (Val >= 4)
6440 break;
6441 }
6442
Nate Begeman9008ca62009-04-27 18:41:29 +00006443 Mask1[0] = PermMask[HiIndex];
6444 Mask1[1] = -1;
6445 Mask1[2] = PermMask[HiIndex^1];
6446 Mask1[3] = -1;
6447 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006448
6449 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006450 Mask1[0] = PermMask[0];
6451 Mask1[1] = PermMask[1];
6452 Mask1[2] = HiIndex & 1 ? 6 : 4;
6453 Mask1[3] = HiIndex & 1 ? 4 : 6;
6454 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006455 }
Craig Topper69947b92012-04-23 06:57:04 +00006456
6457 Mask1[0] = HiIndex & 1 ? 2 : 0;
6458 Mask1[1] = HiIndex & 1 ? 0 : 2;
6459 Mask1[2] = PermMask[2];
6460 Mask1[3] = PermMask[3];
6461 if (Mask1[2] >= 0)
6462 Mask1[2] += 4;
6463 if (Mask1[3] >= 0)
6464 Mask1[3] += 4;
6465 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006466 }
6467
6468 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006469 int LoMask[] = { -1, -1, -1, -1 };
6470 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006471
Benjamin Kramer9c683542012-01-30 15:16:21 +00006472 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006473 unsigned MaskIdx = 0;
6474 unsigned LoIdx = 0;
6475 unsigned HiIdx = 2;
6476 for (unsigned i = 0; i != 4; ++i) {
6477 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006478 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006479 MaskIdx = 1;
6480 LoIdx = 0;
6481 HiIdx = 2;
6482 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006483 int Idx = PermMask[i];
6484 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006485 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006486 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006487 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006488 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006489 LoIdx++;
6490 } else {
6491 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006492 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006493 HiIdx++;
6494 }
6495 }
6496
Nate Begeman9008ca62009-04-27 18:41:29 +00006497 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6498 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006499 int MaskOps[] = { -1, -1, -1, -1 };
6500 for (unsigned i = 0; i != 4; ++i)
6501 if (Locs[i].first != -1)
6502 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006503 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006504}
6505
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006506static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006507 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006508 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006509
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006510 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6511 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006512 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6513 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6514 // BUILD_VECTOR (load), undef
6515 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006516
6517 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006518}
6519
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006520static
Evan Cheng835580f2010-10-07 20:50:20 +00006521SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6522 EVT VT = Op.getValueType();
6523
6524 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006525 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6526 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006527 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6528 V1, DAG));
6529}
6530
6531static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006532SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006533 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006534 SDValue V1 = Op.getOperand(0);
6535 SDValue V2 = Op.getOperand(1);
6536 EVT VT = Op.getValueType();
6537
6538 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6539
Craig Topper1accb7e2012-01-10 06:54:16 +00006540 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006541 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6542
Evan Cheng0899f5c2011-08-31 02:05:24 +00006543 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6544 return DAG.getNode(ISD::BITCAST, dl, VT,
6545 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6546 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6547 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006548}
6549
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006550static
6551SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6552 SDValue V1 = Op.getOperand(0);
6553 SDValue V2 = Op.getOperand(1);
6554 EVT VT = Op.getValueType();
6555
6556 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6557 "unsupported shuffle type");
6558
6559 if (V2.getOpcode() == ISD::UNDEF)
6560 V2 = V1;
6561
6562 // v4i32 or v4f32
6563 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6564}
6565
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006566static
Craig Topper1accb7e2012-01-10 06:54:16 +00006567SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568 SDValue V1 = Op.getOperand(0);
6569 SDValue V2 = Op.getOperand(1);
6570 EVT VT = Op.getValueType();
6571 unsigned NumElems = VT.getVectorNumElements();
6572
6573 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6574 // operand of these instructions is only memory, so check if there's a
6575 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6576 // same masks.
6577 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006578
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006579 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006580 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006581 CanFoldLoad = true;
6582
6583 // When V1 is a load, it can be folded later into a store in isel, example:
6584 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6585 // turns into:
6586 // (MOVLPSmr addr:$src1, VR128:$src2)
6587 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006588 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006589 CanFoldLoad = true;
6590
Dan Gohman65fd6562011-11-03 21:49:52 +00006591 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006593 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006594 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6595
6596 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006597 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006598 if (SVOp->getMaskElt(1) != -1)
6599 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006600 }
6601
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006602 // movl and movlp will both match v2i64, but v2i64 is never matched by
6603 // movl earlier because we make it strict to avoid messing with the movlp load
6604 // folding logic (see the code above getMOVLP call). Match it here then,
6605 // this is horrible, but will stay like this until we move all shuffle
6606 // matching to x86 specific nodes. Note that for the 1st condition all
6607 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006608 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006609 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6610 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006611 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006612 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006613 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006614 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006615
6616 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6617
6618 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006619 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006620 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006621}
6622
Michael Liaod9d09602012-10-23 17:34:00 +00006623// Reduce a vector shuffle to zext.
6624SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006625X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006626 // PMOVZX is only available from SSE41.
6627 if (!Subtarget->hasSSE41())
6628 return SDValue();
6629
6630 EVT VT = Op.getValueType();
6631
6632 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006633 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006634 return SDValue();
6635
6636 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6637 DebugLoc DL = Op.getDebugLoc();
6638 SDValue V1 = Op.getOperand(0);
6639 SDValue V2 = Op.getOperand(1);
6640 unsigned NumElems = VT.getVectorNumElements();
6641
6642 // Extending is an unary operation and the element type of the source vector
6643 // won't be equal to or larger than i64.
6644 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6645 VT.getVectorElementType() == MVT::i64)
6646 return SDValue();
6647
6648 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6649 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006650 while ((1U << Shift) < NumElems) {
6651 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006652 break;
6653 Shift += 1;
6654 // The maximal ratio is 8, i.e. from i8 to i64.
6655 if (Shift > 3)
6656 return SDValue();
6657 }
6658
6659 // Check the shuffle mask.
6660 unsigned Mask = (1U << Shift) - 1;
6661 for (unsigned i = 0; i != NumElems; ++i) {
6662 int EltIdx = SVOp->getMaskElt(i);
6663 if ((i & Mask) != 0 && EltIdx != -1)
6664 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006665 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006666 return SDValue();
6667 }
6668
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006669 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006670 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006671 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6672 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006673
6674 if (!isTypeLegal(NVT))
6675 return SDValue();
6676
6677 // Simplify the operand as it's prepared to be fed into shuffle.
6678 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6679 if (V1.getOpcode() == ISD::BITCAST &&
6680 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6681 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6682 V1.getOperand(0)
6683 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6684 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6685 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006686 ConstantSDNode *CIdx =
6687 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006688 // If it's foldable, i.e. normal load with single use, we will let code
6689 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006690 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006691 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6692 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6693 // The "ext_vec_elt" node is wider than the result node.
6694 // In this case we should extract subvector from V.
6695 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6696 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6697 EVT FullVT = V.getValueType();
6698 EVT SubVecVT = EVT::getVectorVT(*Context,
6699 FullVT.getVectorElementType(),
6700 FullVT.getVectorNumElements()/Ratio);
6701 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6702 DAG.getIntPtrConstant(0));
6703 }
Michael Liaod9d09602012-10-23 17:34:00 +00006704 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006705 }
Michael Liaod9d09602012-10-23 17:34:00 +00006706 }
6707
6708 return DAG.getNode(ISD::BITCAST, DL, VT,
6709 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6710}
6711
Nadav Rotem154819d2012-04-09 07:45:58 +00006712SDValue
6713X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006714 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006715 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006716 DebugLoc dl = Op.getDebugLoc();
6717 SDValue V1 = Op.getOperand(0);
6718 SDValue V2 = Op.getOperand(1);
6719
6720 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006721 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006722
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006723 // Handle splat operations
6724 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006725 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006726 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006727 if (Broadcast.getNode())
6728 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006729 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006730
Michael Liaod9d09602012-10-23 17:34:00 +00006731 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006732 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006733 if (NewOp.getNode())
6734 return NewOp;
6735
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006736 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6737 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006738 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6739 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006740 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006741 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006742 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006743 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006744 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006745 // FIXME: Figure out a cleaner way to do this.
6746 // Try to make use of movq to zero out the top part.
6747 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006748 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006749 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006750 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006751 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6752 NewVT, true, false))
6753 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006754 DAG, Subtarget, dl);
6755 }
6756 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006757 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006758 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006759 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006760 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6761 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6762 DAG, Subtarget, dl);
6763 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006764 }
6765 }
6766 return SDValue();
6767}
6768
Dan Gohman475871a2008-07-27 21:46:04 +00006769SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006770X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006771 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006772 SDValue V1 = Op.getOperand(0);
6773 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006774 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006775 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006776 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006777 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006778 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006779 bool V1IsSplat = false;
6780 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006781 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006782 bool HasFp256 = Subtarget->hasFp256();
6783 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006784 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006785 bool OptForSize = MF.getFunction()->getAttributes().
6786 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787
Craig Topper3426a3e2011-11-14 06:46:21 +00006788 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006789
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006790 if (V1IsUndef && V2IsUndef)
6791 return DAG.getUNDEF(VT);
6792
6793 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006794
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006795 // Vector shuffle lowering takes 3 steps:
6796 //
6797 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6798 // narrowing and commutation of operands should be handled.
6799 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6800 // shuffle nodes.
6801 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6802 // so the shuffle can be broken into other shuffles and the legalizer can
6803 // try the lowering again.
6804 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006805 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006806 // be matched during isel, all of them must be converted to a target specific
6807 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006808
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006809 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6810 // narrowing and commutation of operands should be handled. The actual code
6811 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006812 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006813 if (NewOp.getNode())
6814 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006815
Craig Topper5aaffa82012-02-19 02:53:47 +00006816 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6817
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006818 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6819 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006820 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006821 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006822 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006823 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006824
Craig Topperdd637ae2012-02-19 05:41:45 +00006825 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006826 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006827 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006828
Craig Topperdd637ae2012-02-19 05:41:45 +00006829 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006830 return getMOVHighToLow(Op, dl, DAG);
6831
6832 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006833 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006834 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006835 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006836
Craig Topper5aaffa82012-02-19 02:53:47 +00006837 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006838 // The actual implementation will match the mask in the if above and then
6839 // during isel it can match several different instructions, not only pshufd
6840 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006841 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6842 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006843
Craig Topper5aaffa82012-02-19 02:53:47 +00006844 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006845
Craig Topper1accb7e2012-01-10 06:54:16 +00006846 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006847 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6848
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006849 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6850 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6851 DAG);
6852
Craig Topperb3982da2011-12-31 23:50:21 +00006853 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006854 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006855 }
Eric Christopherfd179292009-08-27 18:07:15 +00006856
Evan Chengf26ffe92008-05-29 08:22:04 +00006857 // Check if this can be converted into a logical shift.
6858 bool isLeft = false;
6859 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006861 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006862 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006863 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006864 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006865 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006866 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006867 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006868 }
Eric Christopherfd179292009-08-27 18:07:15 +00006869
Craig Topper5aaffa82012-02-19 02:53:47 +00006870 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006871 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006872 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006873 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006874 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006875 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6876
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006877 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006878 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6879 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006880 }
Eric Christopherfd179292009-08-27 18:07:15 +00006881
Nate Begeman9008ca62009-04-27 18:41:29 +00006882 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006883 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006884 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006885
Craig Topperdd637ae2012-02-19 05:41:45 +00006886 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006887 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006888
Craig Topperdd637ae2012-02-19 05:41:45 +00006889 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006890 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006891
Craig Topperdd637ae2012-02-19 05:41:45 +00006892 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006893 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006894
Craig Topperdd637ae2012-02-19 05:41:45 +00006895 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006896 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006897
Craig Topperdd637ae2012-02-19 05:41:45 +00006898 if (ShouldXformToMOVHLPS(M, VT) ||
6899 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006900 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901
Evan Chengf26ffe92008-05-29 08:22:04 +00006902 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006903 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006904 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006905 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006906 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006907 }
Eric Christopherfd179292009-08-27 18:07:15 +00006908
Evan Cheng9eca5e82006-10-25 21:49:50 +00006909 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006910 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6911 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006912 V1IsSplat = isSplatVector(V1.getNode());
6913 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006914
Chris Lattner8a594482007-11-25 00:24:49 +00006915 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006916 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6917 CommuteVectorShuffleMask(M, NumElems);
6918 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006919 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006920 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006921 }
6922
Craig Topperbeabc6c2011-12-05 06:56:46 +00006923 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006924 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006925 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006926 return V1;
6927 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6928 // the instruction selector will not match, so get a canonical MOVL with
6929 // swapped operands to undo the commute.
6930 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006932
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006933 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006934 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006935
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006936 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006937 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006938
Evan Cheng9bbbb982006-10-25 20:48:19 +00006939 if (V2IsSplat) {
6940 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006941 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006942 // new vector_shuffle with the corrected mask.p
6943 SmallVector<int, 8> NewMask(M.begin(), M.end());
6944 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006945 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006946 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006948 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 }
6950
Evan Cheng9eca5e82006-10-25 21:49:50 +00006951 if (Commuted) {
6952 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006953 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006954 CommuteVectorShuffleMask(M, NumElems);
6955 std::swap(V1, V2);
6956 std::swap(V1IsSplat, V2IsSplat);
6957 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006958
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006959 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006960 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006961
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006962 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006963 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006964 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006965
Nate Begeman9008ca62009-04-27 18:41:29 +00006966 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006967 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006968 return CommuteVectorShuffle(SVOp, DAG);
6969
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006970 // The checks below are all present in isShuffleMaskLegal, but they are
6971 // inlined here right now to enable us to directly emit target specific
6972 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006973
Craig Topper0e2037b2012-01-20 05:53:00 +00006974 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006975 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006976 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006977 DAG);
6978
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006979 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6980 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006981 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006982 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006983 }
6984
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006985 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006986 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006987 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006988 DAG);
6989
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006990 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006991 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006992 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006993 DAG);
6994
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006995 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006996 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006997 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006998
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006999 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007000 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007001 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007002 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007003
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007004 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007005 // Generate target specific nodes for 128 or 256-bit shuffles only
7006 // supported in the AVX instruction set.
7007 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007008
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007009 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007010 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007011 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7012
Craig Topper70b883b2011-11-28 10:14:51 +00007013 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007014 if (isVPERMILPMask(M, VT, HasFp256)) {
7015 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007016 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007017 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007018 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007019 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007020 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007021
Craig Topper70b883b2011-11-28 10:14:51 +00007022 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007023 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007024 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007025 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007026
Craig Topper1842ba02012-04-23 06:38:28 +00007027 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007028 if (BlendOp.getNode())
7029 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007030
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007031 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007032 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007033 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007034 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007035 }
Craig Topper92040742012-04-16 06:43:40 +00007036 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7037 &permclMask[0], 8);
7038 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007039 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007040 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007041 }
Craig Topper095c5282012-04-15 23:48:57 +00007042
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007043 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007044 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007045 getShuffleCLImmediate(SVOp), DAG);
7046
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007047 //===--------------------------------------------------------------------===//
7048 // Since no target specific shuffle was selected for this generic one,
7049 // lower it into other known shuffles. FIXME: this isn't true yet, but
7050 // this is the plan.
7051 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007052
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007053 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7054 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007055 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007056 if (NewOp.getNode())
7057 return NewOp;
7058 }
7059
7060 if (VT == MVT::v16i8) {
7061 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7062 if (NewOp.getNode())
7063 return NewOp;
7064 }
7065
Elena Demikhovsky41789462012-09-06 12:42:01 +00007066 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007067 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007068 if (NewOp.getNode())
7069 return NewOp;
7070 }
7071
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007072 // Handle all 128-bit wide vectors with 4 elements, and match them with
7073 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007074 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007075 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7076
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007077 // Handle general 256-bit shuffles
7078 if (VT.is256BitVector())
7079 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7080
Dan Gohman475871a2008-07-27 21:46:04 +00007081 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007082}
7083
Craig Topperf84b7502013-01-20 00:50:58 +00007084static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007085 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007086 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007087
Craig Topper45e1c752013-01-20 00:38:18 +00007088 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007089 return SDValue();
7090
Duncan Sands83ec4b62008-06-06 12:08:01 +00007091 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007093 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007094 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007095 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007096 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007097 }
7098
7099 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007100 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7101 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7102 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007103 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7104 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007105 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007107 Op.getOperand(0)),
7108 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007110 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007112 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007113 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007114 }
7115
7116 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007117 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7118 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007119 // result has a single use which is a store or a bitcast to i32. And in
7120 // the case of a store, it's not worth it if the index is a constant 0,
7121 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007122 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007123 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007124 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007125 if ((User->getOpcode() != ISD::STORE ||
7126 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7127 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007128 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007129 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007130 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007132 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007133 Op.getOperand(0)),
7134 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007135 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007136 }
7137
7138 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007139 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007140 if (isa<ConstantSDNode>(Op.getOperand(1)))
7141 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007142 }
Dan Gohman475871a2008-07-27 21:46:04 +00007143 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007144}
7145
Dan Gohman475871a2008-07-27 21:46:04 +00007146SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007147X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7148 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007150 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007151
David Greene74a579d2011-02-10 16:57:36 +00007152 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007153 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007154
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007155 // If this is a 256-bit vector result, first extract the 128-bit vector and
7156 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007157 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007158 DebugLoc dl = Op.getNode()->getDebugLoc();
7159 unsigned NumElems = VecVT.getVectorNumElements();
7160 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007161 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7162
7163 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007164 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007165
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007166 if (IdxVal >= NumElems/2)
7167 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007168 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007169 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007170 }
7171
Craig Topper7a9a28b2012-08-12 02:23:29 +00007172 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007173
Craig Topperd0a31172012-01-10 06:37:29 +00007174 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007175 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007176 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007177 return Res;
7178 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007179
Craig Topper45e1c752013-01-20 00:38:18 +00007180 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007181 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007183 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007184 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007185 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007186 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7188 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007189 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007191 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007192 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007193 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007194 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007195 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007196 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007197 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007198 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007199 }
7200
7201 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007202 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007203 if (Idx == 0)
7204 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007205
Evan Cheng0db9fe62006-04-25 20:13:52 +00007206 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007207 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007208 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007209 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007210 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007211 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007212 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007213 }
7214
7215 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007216 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7217 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7218 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007219 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007220 if (Idx == 0)
7221 return Op;
7222
7223 // UNPCKHPD the element to the lowest double word, then movsd.
7224 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7225 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007226 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007227 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007228 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007229 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007230 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007231 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007232 }
7233
Dan Gohman475871a2008-07-27 21:46:04 +00007234 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007235}
7236
Craig Topperf84b7502013-01-20 00:50:58 +00007237static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007238 MVT VT = Op.getValueType().getSimpleVT();
7239 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007240 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007241
Dan Gohman475871a2008-07-27 21:46:04 +00007242 SDValue N0 = Op.getOperand(0);
7243 SDValue N1 = Op.getOperand(1);
7244 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007245
Craig Topper7a9a28b2012-08-12 02:23:29 +00007246 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007247 return SDValue();
7248
Dan Gohman8a55ce42009-09-23 21:02:20 +00007249 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007250 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007251 unsigned Opc;
7252 if (VT == MVT::v8i16)
7253 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007254 else if (VT == MVT::v16i8)
7255 Opc = X86ISD::PINSRB;
7256 else
7257 Opc = X86ISD::PINSRB;
7258
Nate Begeman14d12ca2008-02-11 04:19:36 +00007259 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7260 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 if (N1.getValueType() != MVT::i32)
7262 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7263 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007264 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007265 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007266 }
7267
7268 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007269 // Bits [7:6] of the constant are the source select. This will always be
7270 // zero here. The DAG Combiner may combine an extract_elt index into these
7271 // bits. For example (insert (extract, 3), 2) could be matched by putting
7272 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007273 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007274 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007275 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007276 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007277 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007278 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007279 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007280 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007281 }
7282
7283 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007284 // PINSR* works with constant index.
7285 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007286 }
Dan Gohman475871a2008-07-27 21:46:04 +00007287 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007288}
7289
Dan Gohman475871a2008-07-27 21:46:04 +00007290SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007291X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007292 MVT VT = Op.getValueType().getSimpleVT();
7293 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007294
David Greene6b381262011-02-09 15:32:06 +00007295 DebugLoc dl = Op.getDebugLoc();
7296 SDValue N0 = Op.getOperand(0);
7297 SDValue N1 = Op.getOperand(1);
7298 SDValue N2 = Op.getOperand(2);
7299
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007300 // If this is a 256-bit vector result, first extract the 128-bit vector,
7301 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007302 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007303 if (!isa<ConstantSDNode>(N2))
7304 return SDValue();
7305
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007306 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007307 unsigned NumElems = VT.getVectorNumElements();
7308 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007309 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007310
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007311 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007312 bool Upper = IdxVal >= NumElems/2;
7313 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7314 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007315
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007316 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007317 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007318 }
7319
Craig Topperd0a31172012-01-10 06:37:29 +00007320 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007321 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7322
Dan Gohman8a55ce42009-09-23 21:02:20 +00007323 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007324 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007325
Dan Gohman8a55ce42009-09-23 21:02:20 +00007326 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007327 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7328 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 if (N1.getValueType() != MVT::i32)
7330 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7331 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007332 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007333 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007334 }
Dan Gohman475871a2008-07-27 21:46:04 +00007335 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007336}
7337
Craig Topper55b24052012-09-11 06:15:32 +00007338static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007339 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007340 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007341 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007342
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007343 // If this is a 256-bit vector result, first insert into a 128-bit
7344 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007345 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007346 // Insert into a 128-bit vector.
7347 EVT VT128 = EVT::getVectorVT(*Context,
7348 OpVT.getVectorElementType(),
7349 OpVT.getVectorNumElements() / 2);
7350
7351 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7352
7353 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007354 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007355 }
7356
Craig Topperd77d2fe2012-04-29 20:22:05 +00007357 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007358 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007360
Owen Anderson825b72b2009-08-11 20:47:22 +00007361 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007362 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007363 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007364 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007365}
7366
David Greene91585092011-01-26 15:38:49 +00007367// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7368// a simple subregister reference or explicit instructions to grab
7369// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007370static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7371 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007372 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007373 DebugLoc dl = Op.getNode()->getDebugLoc();
7374 SDValue Vec = Op.getNode()->getOperand(0);
7375 SDValue Idx = Op.getNode()->getOperand(1);
7376
Craig Topper7a9a28b2012-08-12 02:23:29 +00007377 if (Op.getNode()->getValueType(0).is128BitVector() &&
7378 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007379 isa<ConstantSDNode>(Idx)) {
7380 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7381 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007382 }
David Greene91585092011-01-26 15:38:49 +00007383 }
7384 return SDValue();
7385}
7386
David Greenecfe33c42011-01-26 19:13:22 +00007387// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7388// simple superregister reference or explicit instructions to insert
7389// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007390static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7391 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007392 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007393 DebugLoc dl = Op.getNode()->getDebugLoc();
7394 SDValue Vec = Op.getNode()->getOperand(0);
7395 SDValue SubVec = Op.getNode()->getOperand(1);
7396 SDValue Idx = Op.getNode()->getOperand(2);
7397
Craig Topper7a9a28b2012-08-12 02:23:29 +00007398 if (Op.getNode()->getValueType(0).is256BitVector() &&
7399 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007400 isa<ConstantSDNode>(Idx)) {
7401 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7402 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007403 }
7404 }
7405 return SDValue();
7406}
7407
Bill Wendling056292f2008-09-16 21:48:12 +00007408// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7409// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7410// one of the above mentioned nodes. It has to be wrapped because otherwise
7411// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7412// be used to form addressing mode. These wrapped nodes will be selected
7413// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007414SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007415X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007416 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007417
Chris Lattner41621a22009-06-26 19:22:52 +00007418 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7419 // global base reg.
7420 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007421 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007422 CodeModel::Model M = getTargetMachine().getCodeModel();
7423
Chris Lattner4f066492009-07-11 20:29:19 +00007424 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007425 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007426 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007427 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007428 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007429 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007430 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007431
Evan Cheng1606e8e2009-03-13 07:51:59 +00007432 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007433 CP->getAlignment(),
7434 CP->getOffset(), OpFlag);
7435 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007436 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007437 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007438 if (OpFlag) {
7439 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007440 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007441 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007442 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007443 }
7444
7445 return Result;
7446}
7447
Dan Gohmand858e902010-04-17 15:26:15 +00007448SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007449 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007450
Chris Lattner18c59872009-06-27 04:16:01 +00007451 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7452 // global base reg.
7453 unsigned char OpFlag = 0;
7454 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007455 CodeModel::Model M = getTargetMachine().getCodeModel();
7456
Chris Lattner4f066492009-07-11 20:29:19 +00007457 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007458 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007459 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007460 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007461 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007462 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007463 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007464
Chris Lattner18c59872009-06-27 04:16:01 +00007465 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7466 OpFlag);
7467 DebugLoc DL = JT->getDebugLoc();
7468 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007469
Chris Lattner18c59872009-06-27 04:16:01 +00007470 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007471 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007472 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7473 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007474 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007475 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007476
Chris Lattner18c59872009-06-27 04:16:01 +00007477 return Result;
7478}
7479
7480SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007481X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007482 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007483
Chris Lattner18c59872009-06-27 04:16:01 +00007484 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7485 // global base reg.
7486 unsigned char OpFlag = 0;
7487 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007488 CodeModel::Model M = getTargetMachine().getCodeModel();
7489
Chris Lattner4f066492009-07-11 20:29:19 +00007490 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007491 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7492 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7493 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007494 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007495 } else if (Subtarget->isPICStyleGOT()) {
7496 OpFlag = X86II::MO_GOT;
7497 } else if (Subtarget->isPICStyleStubPIC()) {
7498 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7499 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7500 OpFlag = X86II::MO_DARWIN_NONLAZY;
7501 }
Eric Christopherfd179292009-08-27 18:07:15 +00007502
Chris Lattner18c59872009-06-27 04:16:01 +00007503 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007504
Chris Lattner18c59872009-06-27 04:16:01 +00007505 DebugLoc DL = Op.getDebugLoc();
7506 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007507
Chris Lattner18c59872009-06-27 04:16:01 +00007508 // With PIC, the address is actually $g + Offset.
7509 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007510 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007511 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7512 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007513 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007514 Result);
7515 }
Eric Christopherfd179292009-08-27 18:07:15 +00007516
Eli Friedman586272d2011-08-11 01:48:05 +00007517 // For symbols that require a load from a stub to get the address, emit the
7518 // load.
7519 if (isGlobalStubReference(OpFlag))
7520 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007521 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007522
Chris Lattner18c59872009-06-27 04:16:01 +00007523 return Result;
7524}
7525
Dan Gohman475871a2008-07-27 21:46:04 +00007526SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007527X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007528 // Create the TargetBlockAddressAddress node.
7529 unsigned char OpFlags =
7530 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007531 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007532 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007533 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007534 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007535 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7536 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007537
Dan Gohmanf705adb2009-10-30 01:28:02 +00007538 if (Subtarget->isPICStyleRIPRel() &&
7539 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007540 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7541 else
7542 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007543
Dan Gohman29cbade2009-11-20 23:18:13 +00007544 // With PIC, the address is actually $g + Offset.
7545 if (isGlobalRelativeToPICBase(OpFlags)) {
7546 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7547 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7548 Result);
7549 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007550
7551 return Result;
7552}
7553
7554SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007555X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007556 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007557 // Create the TargetGlobalAddress node, folding in the constant
7558 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007559 unsigned char OpFlags =
7560 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007561 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007562 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007563 if (OpFlags == X86II::MO_NO_FLAG &&
7564 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007565 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007566 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007567 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007568 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007569 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007570 }
Eric Christopherfd179292009-08-27 18:07:15 +00007571
Chris Lattner4f066492009-07-11 20:29:19 +00007572 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007573 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007574 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7575 else
7576 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007577
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007578 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007579 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007580 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7581 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007582 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007584
Chris Lattner36c25012009-07-10 07:34:39 +00007585 // For globals that require a load from a stub to get the address, emit the
7586 // load.
7587 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007588 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007589 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007590
Dan Gohman6520e202008-10-18 02:06:02 +00007591 // If there was a non-zero offset that we didn't fold, create an explicit
7592 // addition for it.
7593 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007594 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007595 DAG.getConstant(Offset, getPointerTy()));
7596
Evan Cheng0db9fe62006-04-25 20:13:52 +00007597 return Result;
7598}
7599
Evan Chengda43bcf2008-09-24 00:05:32 +00007600SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007601X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007602 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007603 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007604 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007605}
7606
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007607static SDValue
7608GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007609 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007610 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007611 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007612 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007613 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007614 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007615 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007616 GA->getOffset(),
7617 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007618
7619 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7620 : X86ISD::TLSADDR;
7621
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007622 if (InFlag) {
7623 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007624 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007625 } else {
7626 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007627 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007628 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007629
7630 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007631 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007632
Rafael Espindola15f1b662009-04-24 12:59:40 +00007633 SDValue Flag = Chain.getValue(1);
7634 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007635}
7636
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007637// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007638static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007639LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007640 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007641 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007642 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7643 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007644 DAG.getNode(X86ISD::GlobalBaseReg,
7645 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007646 InFlag = Chain.getValue(1);
7647
Chris Lattnerb903bed2009-06-26 21:20:29 +00007648 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007649}
7650
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007651// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007652static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007653LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007654 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007655 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7656 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007657}
7658
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007659static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7660 SelectionDAG &DAG,
7661 const EVT PtrVT,
7662 bool is64Bit) {
7663 DebugLoc dl = GA->getDebugLoc();
7664
7665 // Get the start address of the TLS block for this module.
7666 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7667 .getInfo<X86MachineFunctionInfo>();
7668 MFI->incNumLocalDynamicTLSAccesses();
7669
7670 SDValue Base;
7671 if (is64Bit) {
7672 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7673 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7674 } else {
7675 SDValue InFlag;
7676 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7677 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7678 InFlag = Chain.getValue(1);
7679 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7680 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7681 }
7682
7683 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7684 // of Base.
7685
7686 // Build x@dtpoff.
7687 unsigned char OperandFlags = X86II::MO_DTPOFF;
7688 unsigned WrapperKind = X86ISD::Wrapper;
7689 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7690 GA->getValueType(0),
7691 GA->getOffset(), OperandFlags);
7692 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7693
7694 // Add x@dtpoff with the base.
7695 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7696}
7697
Hans Wennborg228756c2012-05-11 10:11:01 +00007698// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007699static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007700 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007701 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007702 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007703
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007704 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7705 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7706 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007707
Michael J. Spencerec38de22010-10-10 22:04:20 +00007708 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007709 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007710 MachinePointerInfo(Ptr),
7711 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007712
Chris Lattnerb903bed2009-06-26 21:20:29 +00007713 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007714 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7715 // initialexec.
7716 unsigned WrapperKind = X86ISD::Wrapper;
7717 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007718 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007719 } else if (model == TLSModel::InitialExec) {
7720 if (is64Bit) {
7721 OperandFlags = X86II::MO_GOTTPOFF;
7722 WrapperKind = X86ISD::WrapperRIP;
7723 } else {
7724 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7725 }
Chris Lattner18c59872009-06-27 04:16:01 +00007726 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007727 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007728 }
Eric Christopherfd179292009-08-27 18:07:15 +00007729
Hans Wennborg228756c2012-05-11 10:11:01 +00007730 // emit "addl x@ntpoff,%eax" (local exec)
7731 // or "addl x@indntpoff,%eax" (initial exec)
7732 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007733 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007734 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007735 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007736 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007737
Hans Wennborg228756c2012-05-11 10:11:01 +00007738 if (model == TLSModel::InitialExec) {
7739 if (isPIC && !is64Bit) {
7740 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7741 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7742 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007743 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007744
7745 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7746 MachinePointerInfo::getGOT(), false, false, false,
7747 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007748 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007749
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007750 // The address of the thread local variable is the add of the thread
7751 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007752 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007753}
7754
Dan Gohman475871a2008-07-27 21:46:04 +00007755SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007756X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007758 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007759 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007760
Eric Christopher30ef0e52010-06-03 04:07:48 +00007761 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007762 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007763
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 switch (model) {
7765 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007766 if (Subtarget->is64Bit())
7767 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7768 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007769 case TLSModel::LocalDynamic:
7770 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7771 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007772 case TLSModel::InitialExec:
7773 case TLSModel::LocalExec:
7774 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007775 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007776 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007777 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007778 llvm_unreachable("Unknown TLS model.");
7779 }
7780
7781 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007782 // Darwin only has one model of TLS. Lower to that.
7783 unsigned char OpFlag = 0;
7784 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7785 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007786
Eric Christopher30ef0e52010-06-03 04:07:48 +00007787 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7788 // global base reg.
7789 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7790 !Subtarget->is64Bit();
7791 if (PIC32)
7792 OpFlag = X86II::MO_TLVP_PIC_BASE;
7793 else
7794 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007795 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007796 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007797 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007798 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007799 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007800
Eric Christopher30ef0e52010-06-03 04:07:48 +00007801 // With PIC32, the address is actually $g + Offset.
7802 if (PIC32)
7803 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7804 DAG.getNode(X86ISD::GlobalBaseReg,
7805 DebugLoc(), getPointerTy()),
7806 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007807
Eric Christopher30ef0e52010-06-03 04:07:48 +00007808 // Lowering the machine isd will make sure everything is in the right
7809 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007810 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007811 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007812 SDValue Args[] = { Chain, Offset };
7813 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007814
Eric Christopher30ef0e52010-06-03 04:07:48 +00007815 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7816 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7817 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007818
Eric Christopher30ef0e52010-06-03 04:07:48 +00007819 // And our return value (tls address) is in the standard call return value
7820 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007821 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007822 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7823 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007824 }
7825
7826 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007827 // Just use the implicit TLS architecture
7828 // Need to generate someting similar to:
7829 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7830 // ; from TEB
7831 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7832 // mov rcx, qword [rdx+rcx*8]
7833 // mov eax, .tls$:tlsvar
7834 // [rax+rcx] contains the address
7835 // Windows 64bit: gs:0x58
7836 // Windows 32bit: fs:__tls_array
7837
7838 // If GV is an alias then use the aliasee for determining
7839 // thread-localness.
7840 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7841 GV = GA->resolveAliasedGlobal(false);
7842 DebugLoc dl = GA->getDebugLoc();
7843 SDValue Chain = DAG.getEntryNode();
7844
7845 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7846 // %gs:0x58 (64-bit).
7847 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7848 ? Type::getInt8PtrTy(*DAG.getContext(),
7849 256)
7850 : Type::getInt32PtrTy(*DAG.getContext(),
7851 257));
7852
7853 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7854 Subtarget->is64Bit()
7855 ? DAG.getIntPtrConstant(0x58)
7856 : DAG.getExternalSymbol("_tls_array",
7857 getPointerTy()),
7858 MachinePointerInfo(Ptr),
7859 false, false, false, 0);
7860
7861 // Load the _tls_index variable
7862 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7863 if (Subtarget->is64Bit())
7864 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7865 IDX, MachinePointerInfo(), MVT::i32,
7866 false, false, 0);
7867 else
7868 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7869 false, false, false, 0);
7870
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007871 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007872 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007873 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7874
7875 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7876 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7877 false, false, false, 0);
7878
7879 // Get the offset of start of .tls section
7880 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7881 GA->getValueType(0),
7882 GA->getOffset(), X86II::MO_SECREL);
7883 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7884
7885 // The address of the thread local variable is the add of the thread
7886 // pointer with the offset of the variable.
7887 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007888 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007889
David Blaikie4d6ccb52012-01-20 21:51:11 +00007890 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007891}
7892
Chad Rosierb90d2a92012-01-03 23:19:12 +00007893/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7894/// and take a 2 x i32 value to shift plus a shift amount.
7895SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007896 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007897 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007898 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007899 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007900 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007901 SDValue ShOpLo = Op.getOperand(0);
7902 SDValue ShOpHi = Op.getOperand(1);
7903 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007904 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007906 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007907
Dan Gohman475871a2008-07-27 21:46:04 +00007908 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007909 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007910 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7911 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007912 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007913 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7914 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007915 }
Evan Chenge3413162006-01-09 18:33:28 +00007916
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7918 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007919 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007920 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007921
Dan Gohman475871a2008-07-27 21:46:04 +00007922 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007924 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7925 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007926
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007927 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007928 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7929 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007930 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007931 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7932 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007933 }
7934
Dan Gohman475871a2008-07-27 21:46:04 +00007935 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007936 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007937}
Evan Chenga3195e82006-01-12 22:54:21 +00007938
Dan Gohmand858e902010-04-17 15:26:15 +00007939SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7940 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007941 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007942
Dale Johannesen0488fb62010-09-30 23:57:10 +00007943 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007944 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007945
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007947 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007948
Eli Friedman36df4992009-05-27 00:47:34 +00007949 // These are really Legal; return the operand so the caller accepts it as
7950 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007952 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007954 Subtarget->is64Bit()) {
7955 return Op;
7956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007957
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007958 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007959 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007961 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007962 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007963 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007964 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007965 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007966 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007967 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7968}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007969
Owen Andersone50ed302009-08-10 22:56:29 +00007970SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007971 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007972 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007973 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007975 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007976 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007977 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007978 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007979 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007981
Chris Lattner492a43e2010-09-22 01:28:21 +00007982 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007983
Stuart Hastings84be9582011-06-02 15:57:11 +00007984 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7985 MachineMemOperand *MMO;
7986 if (FI) {
7987 int SSFI = FI->getIndex();
7988 MMO =
7989 DAG.getMachineFunction()
7990 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7991 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7992 } else {
7993 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7994 StackSlot = StackSlot.getOperand(1);
7995 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007996 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007997 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7998 X86ISD::FILD, DL,
7999 Tys, Ops, array_lengthof(Ops),
8000 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008001
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008002 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008003 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008004 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008005
8006 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8007 // shouldn't be necessary except that RFP cannot be live across
8008 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008009 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008010 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8011 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008012 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008013 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008014 SDValue Ops[] = {
8015 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8016 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008017 MachineMemOperand *MMO =
8018 DAG.getMachineFunction()
8019 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008020 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008021
Chris Lattner492a43e2010-09-22 01:28:21 +00008022 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8023 Ops, array_lengthof(Ops),
8024 Op.getValueType(), MMO);
8025 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008026 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008027 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008028 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008029
Evan Cheng0db9fe62006-04-25 20:13:52 +00008030 return Result;
8031}
8032
Bill Wendling8b8a6362009-01-17 03:56:04 +00008033// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008034SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8035 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008036 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008037 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008038 movq %rax, %xmm0
8039 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8040 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8041 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008042 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008043 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008044 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008045 addpd %xmm1, %xmm0
8046 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008047 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008048
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008049 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008050 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008051
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008052 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008053 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8054 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008055 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008056
Chris Lattner97484792012-01-25 09:56:22 +00008057 SmallVector<Constant*,2> CV1;
8058 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008059 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8060 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008061 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008062 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8063 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008064 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008065 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008066
Bill Wendling397ae212012-01-05 02:13:20 +00008067 // Load the 64-bit value into an XMM register.
8068 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8069 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008070 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008071 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008072 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008073 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8074 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8075 CLod0);
8076
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008078 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008079 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008080 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008082 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008083
Craig Topperd0a31172012-01-10 06:37:29 +00008084 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008085 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8086 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8087 } else {
8088 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8089 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8090 S2F, 0x4E, DAG);
8091 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8092 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8093 Sub);
8094 }
8095
8096 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008097 DAG.getIntPtrConstant(0));
8098}
8099
Bill Wendling8b8a6362009-01-17 03:56:04 +00008100// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008101SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8102 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008103 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008104 // FP constant to bias correct the final result.
8105 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008107
8108 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008109 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008110 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008111
Eli Friedmanf3704762011-08-29 21:15:46 +00008112 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008113 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008114
Owen Anderson825b72b2009-08-11 20:47:22 +00008115 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008116 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008117 DAG.getIntPtrConstant(0));
8118
8119 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008120 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008121 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008122 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008123 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008124 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008125 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008126 MVT::v2f64, Bias)));
8127 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008128 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008129 DAG.getIntPtrConstant(0));
8130
8131 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008132 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008133
8134 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008135 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008136
Craig Topper69947b92012-04-23 06:57:04 +00008137 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008138 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008139 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008140 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008141 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008142
8143 // Handle final rounding.
8144 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008145}
8146
Michael Liaoa7554632012-10-23 17:36:08 +00008147SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8148 SelectionDAG &DAG) const {
8149 SDValue N0 = Op.getOperand(0);
8150 EVT SVT = N0.getValueType();
8151 DebugLoc dl = Op.getDebugLoc();
8152
8153 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8154 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8155 "Custom UINT_TO_FP is not supported!");
8156
Craig Topperb99bafe2013-01-21 06:21:54 +00008157 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8158 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008159 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8160 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8161}
8162
Dan Gohmand858e902010-04-17 15:26:15 +00008163SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8164 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008165 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008166 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008167
Michael Liaoa7554632012-10-23 17:36:08 +00008168 if (Op.getValueType().isVector())
8169 return lowerUINT_TO_FP_vec(Op, DAG);
8170
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008171 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008172 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8173 // the optimization here.
8174 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008175 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008176
Owen Andersone50ed302009-08-10 22:56:29 +00008177 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008178 EVT DstVT = Op.getValueType();
8179 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008180 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008181 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008182 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008183 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008184 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008185
8186 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008187 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008188 if (SrcVT == MVT::i32) {
8189 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8190 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8191 getPointerTy(), StackSlot, WordOff);
8192 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008193 StackSlot, MachinePointerInfo(),
8194 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008195 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008196 OffsetSlot, MachinePointerInfo(),
8197 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008198 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8199 return Fild;
8200 }
8201
8202 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8203 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008204 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008205 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008206 // For i64 source, we need to add the appropriate power of 2 if the input
8207 // was negative. This is the same as the optimization in
8208 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8209 // we must be careful to do the computation in x87 extended precision, not
8210 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008211 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8212 MachineMemOperand *MMO =
8213 DAG.getMachineFunction()
8214 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8215 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008216
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008217 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8218 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008219 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8220 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008221
8222 APInt FF(32, 0x5F800000ULL);
8223
8224 // Check whether the sign bit is set.
8225 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8226 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8227 ISD::SETLT);
8228
8229 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8230 SDValue FudgePtr = DAG.getConstantPool(
8231 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8232 getPointerTy());
8233
8234 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8235 SDValue Zero = DAG.getIntPtrConstant(0);
8236 SDValue Four = DAG.getIntPtrConstant(4);
8237 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8238 Zero, Four);
8239 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8240
8241 // Load the value out, extending it from f32 to f80.
8242 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008243 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008244 FudgePtr, MachinePointerInfo::getConstantPool(),
8245 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008246 // Extend everything to 80 bits to force it to be done on x87.
8247 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8248 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008249}
8250
Craig Topperb99bafe2013-01-21 06:21:54 +00008251std::pair<SDValue,SDValue>
8252X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8253 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008254 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008255
Owen Andersone50ed302009-08-10 22:56:29 +00008256 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008257
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008258 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8260 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008261 }
8262
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8264 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008265 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008266
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008267 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008268 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008269 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008270 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008271 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008272 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008273 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008274 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008275
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008276 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8277 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008278 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008279 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008280 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008281 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008282
Evan Cheng0db9fe62006-04-25 20:13:52 +00008283 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008284 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8285 Opc = X86ISD::WIN_FTOL;
8286 else
8287 switch (DstTy.getSimpleVT().SimpleTy) {
8288 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8289 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8290 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8291 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8292 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008293
Dan Gohman475871a2008-07-27 21:46:04 +00008294 SDValue Chain = DAG.getEntryNode();
8295 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008296 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008297 // FIXME This causes a redundant load/store if the SSE-class value is already
8298 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008299 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008301 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008302 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008303 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008305 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008306 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008307 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008308
Chris Lattner492a43e2010-09-22 01:28:21 +00008309 MachineMemOperand *MMO =
8310 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8311 MachineMemOperand::MOLoad, MemSize, MemSize);
8312 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8313 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008314 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008315 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008316 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8317 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008318
Chris Lattner07290932010-09-22 01:05:16 +00008319 MachineMemOperand *MMO =
8320 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8321 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008322
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008323 if (Opc != X86ISD::WIN_FTOL) {
8324 // Build the FP_TO_INT*_IN_MEM
8325 SDValue Ops[] = { Chain, Value, StackSlot };
8326 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8327 Ops, 3, DstTy, MMO);
8328 return std::make_pair(FIST, StackSlot);
8329 } else {
8330 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8331 DAG.getVTList(MVT::Other, MVT::Glue),
8332 Chain, Value);
8333 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8334 MVT::i32, ftol.getValue(1));
8335 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8336 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008337 SDValue Ops[] = { eax, edx };
8338 SDValue pair = IsReplace
8339 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8340 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008341 return std::make_pair(pair, SDValue());
8342 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008343}
8344
Nadav Rotem0509db22012-12-28 05:45:24 +00008345static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8346 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008347 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008348 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008349 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008350 DebugLoc dl = Op->getDebugLoc();
8351
8352 // Optimize vectors in AVX mode:
8353 //
8354 // v8i16 -> v8i32
8355 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8356 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8357 // Concat upper and lower parts.
8358 //
8359 // v4i32 -> v4i64
8360 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8361 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8362 // Concat upper and lower parts.
8363 //
8364
8365 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8366 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8367 return SDValue();
8368
8369 if (Subtarget->hasInt256())
8370 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8371
8372 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8373 SDValue Undef = DAG.getUNDEF(InVT);
8374 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8375 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8376 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8377
Craig Toppera080daf2013-01-20 21:50:27 +00008378 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008379 VT.getVectorNumElements()/2);
8380
8381 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8382 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8383
8384 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8385}
8386
8387SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8388 SelectionDAG &DAG) const {
8389 if (Subtarget->hasFp256()) {
8390 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8391 if (Res.getNode())
8392 return Res;
8393 }
8394
8395 return SDValue();
8396}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008397SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8398 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008399 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008400 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008401 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008402 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008403
Nadav Rotem0509db22012-12-28 05:45:24 +00008404 if (Subtarget->hasFp256()) {
8405 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8406 if (Res.getNode())
8407 return Res;
8408 }
8409
Michael Liaoa7554632012-10-23 17:36:08 +00008410 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8411 VT.getVectorNumElements() != SVT.getVectorNumElements())
8412 return SDValue();
8413
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008414 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008415
8416 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008417 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008418 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8419
8420 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8421 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8422 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008423 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8424 DAG.getUNDEF(MVT::v8i16),
8425 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008426
8427 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8428}
8429
Craig Topperd713c0f2013-01-20 21:34:37 +00008430SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008431 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008432 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008433 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008434 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008435
Nadav Rotem3c22a442012-12-27 07:45:10 +00008436 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8437 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8438 if (Subtarget->hasInt256()) {
8439 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8440 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8441 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8442 ShufMask);
8443 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8444 DAG.getIntPtrConstant(0));
8445 }
8446
8447 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8448 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8449 DAG.getIntPtrConstant(0));
8450 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8451 DAG.getIntPtrConstant(2));
8452
8453 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8454 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8455
8456 // The PSHUFD mask:
8457 static const int ShufMask1[] = {0, 2, 0, 0};
8458 SDValue Undef = DAG.getUNDEF(VT);
8459 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8460 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8461
8462 // The MOVLHPS mask:
8463 static const int ShufMask2[] = {0, 1, 4, 5};
8464 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8465 }
8466
8467 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8468 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8469 if (Subtarget->hasInt256()) {
8470 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8471
8472 SmallVector<SDValue,32> pshufbMask;
8473 for (unsigned i = 0; i < 2; ++i) {
8474 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8475 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8476 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8477 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8478 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8479 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8480 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8481 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8482 for (unsigned j = 0; j < 8; ++j)
8483 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8484 }
8485 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8486 &pshufbMask[0], 32);
8487 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8488 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8489
8490 static const int ShufMask[] = {0, 2, -1, -1};
8491 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8492 &ShufMask[0]);
8493 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8494 DAG.getIntPtrConstant(0));
8495 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8496 }
8497
8498 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8499 DAG.getIntPtrConstant(0));
8500
8501 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8502 DAG.getIntPtrConstant(4));
8503
8504 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8505 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8506
8507 // The PSHUFB mask:
8508 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8509 -1, -1, -1, -1, -1, -1, -1, -1};
8510
8511 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8512 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8513 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8514
8515 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8516 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8517
8518 // The MOVLHPS Mask:
8519 static const int ShufMask2[] = {0, 1, 4, 5};
8520 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8521 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8522 }
8523
8524 // Handle truncation of V256 to V128 using shuffles.
8525 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008526 return SDValue();
8527
Nadav Rotem3c22a442012-12-27 07:45:10 +00008528 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8529 "Invalid op");
8530 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008531
8532 unsigned NumElems = VT.getVectorNumElements();
8533 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8534 NumElems * 2);
8535
Michael Liaobedcbd42012-10-16 18:14:11 +00008536 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8537 // Prepare truncation shuffle mask
8538 for (unsigned i = 0; i != NumElems; ++i)
8539 MaskVec[i] = i * 2;
8540 SDValue V = DAG.getVectorShuffle(NVT, DL,
8541 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8542 DAG.getUNDEF(NVT), &MaskVec[0]);
8543 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8544 DAG.getIntPtrConstant(0));
8545}
8546
Dan Gohmand858e902010-04-17 15:26:15 +00008547SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8548 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008549 MVT VT = Op.getValueType().getSimpleVT();
8550 if (VT.isVector()) {
8551 if (VT == MVT::v8i16)
8552 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008553 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8554 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008555 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008556 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008557
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008558 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8559 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008560 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008561 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8562 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008563
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008564 if (StackSlot.getNode())
8565 // Load the result.
8566 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8567 FIST, StackSlot, MachinePointerInfo(),
8568 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008569
8570 // The node is the result.
8571 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008572}
8573
Dan Gohmand858e902010-04-17 15:26:15 +00008574SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8575 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008576 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8577 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008578 SDValue FIST = Vals.first, StackSlot = Vals.second;
8579 assert(FIST.getNode() && "Unexpected failure");
8580
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008581 if (StackSlot.getNode())
8582 // Load the result.
8583 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8584 FIST, StackSlot, MachinePointerInfo(),
8585 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008586
8587 // The node is the result.
8588 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008589}
8590
Craig Topperb84b4232013-01-21 06:13:28 +00008591static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008592 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008593 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008594 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008595 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008596
8597 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8598
8599 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8600 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8601 In, DAG.getUNDEF(SVT)));
8602}
8603
Craig Topper43620672012-09-08 07:31:51 +00008604SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008605 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008606 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008607 MVT VT = Op.getValueType().getSimpleVT();
8608 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008609 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8610 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008611 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008612 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008613 }
Craig Topper43620672012-09-08 07:31:51 +00008614 Constant *C;
8615 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008616 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8617 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008618 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008619 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8620 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008621 C = ConstantVector::getSplat(NumElts, C);
8622 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8623 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008624 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008625 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008626 false, false, false, Alignment);
8627 if (VT.isVector()) {
8628 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8629 return DAG.getNode(ISD::BITCAST, dl, VT,
8630 DAG.getNode(ISD::AND, dl, ANDVT,
8631 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8632 Op.getOperand(0)),
8633 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8634 }
Dale Johannesenace16102009-02-03 19:33:06 +00008635 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008636}
8637
Dan Gohmand858e902010-04-17 15:26:15 +00008638SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008639 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008640 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008641 MVT VT = Op.getValueType().getSimpleVT();
8642 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008643 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8644 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008645 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008646 NumElts = VT.getVectorNumElements();
8647 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008648 Constant *C;
8649 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008650 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8651 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008652 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008653 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8654 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008655 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008656 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8657 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008658 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008659 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008660 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008661 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008662 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008663 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008664 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008665 DAG.getNode(ISD::BITCAST, dl, XORVT,
8666 Op.getOperand(0)),
8667 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008668 }
Craig Topper69947b92012-04-23 06:57:04 +00008669
8670 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008671}
8672
Dan Gohmand858e902010-04-17 15:26:15 +00008673SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008674 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008675 SDValue Op0 = Op.getOperand(0);
8676 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008677 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008678 MVT VT = Op.getValueType().getSimpleVT();
8679 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008680
8681 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008682 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008683 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008684 SrcVT = VT;
8685 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008686 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008687 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008688 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008689 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008690 }
8691
8692 // At this point the operands and the result should have the same
8693 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008694
Evan Cheng68c47cb2007-01-05 07:55:56 +00008695 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008696 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008698 const fltSemantics &Sem = APFloat::IEEEdouble;
8699 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8700 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008701 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008702 const fltSemantics &Sem = APFloat::IEEEsingle;
8703 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8704 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8705 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8706 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008707 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008708 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008709 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008710 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008711 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008712 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008713 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008714
8715 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008716 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008717 // Op0 is MVT::f32, Op1 is MVT::f64.
8718 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8719 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8720 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008721 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008722 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008723 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008724 }
8725
Evan Cheng73d6cf12007-01-05 21:37:56 +00008726 // Clear first operand sign bit.
8727 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008728 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008729 const fltSemantics &Sem = APFloat::IEEEdouble;
8730 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8731 APInt(64, ~(1ULL << 63)))));
8732 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008733 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008734 const fltSemantics &Sem = APFloat::IEEEsingle;
8735 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8736 APInt(32, ~(1U << 31)))));
8737 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8738 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8739 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008740 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008741 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008742 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008743 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008744 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008745 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008746 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008747
8748 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008749 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008750}
8751
Craig Topper55b24052012-09-11 06:15:32 +00008752static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008753 SDValue N0 = Op.getOperand(0);
8754 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008755 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008756
8757 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8758 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8759 DAG.getConstant(1, VT));
8760 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8761}
8762
Michael Liaof966e4e2012-09-13 20:24:54 +00008763// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8764//
Craig Topperb99bafe2013-01-21 06:21:54 +00008765SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8766 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008767 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8768
8769 if (!Subtarget->hasSSE41())
8770 return SDValue();
8771
8772 if (!Op->hasOneUse())
8773 return SDValue();
8774
8775 SDNode *N = Op.getNode();
8776 DebugLoc DL = N->getDebugLoc();
8777
8778 SmallVector<SDValue, 8> Opnds;
8779 DenseMap<SDValue, unsigned> VecInMap;
8780 EVT VT = MVT::Other;
8781
8782 // Recognize a special case where a vector is casted into wide integer to
8783 // test all 0s.
8784 Opnds.push_back(N->getOperand(0));
8785 Opnds.push_back(N->getOperand(1));
8786
8787 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8788 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8789 // BFS traverse all OR'd operands.
8790 if (I->getOpcode() == ISD::OR) {
8791 Opnds.push_back(I->getOperand(0));
8792 Opnds.push_back(I->getOperand(1));
8793 // Re-evaluate the number of nodes to be traversed.
8794 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8795 continue;
8796 }
8797
8798 // Quit if a non-EXTRACT_VECTOR_ELT
8799 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8800 return SDValue();
8801
8802 // Quit if without a constant index.
8803 SDValue Idx = I->getOperand(1);
8804 if (!isa<ConstantSDNode>(Idx))
8805 return SDValue();
8806
8807 SDValue ExtractedFromVec = I->getOperand(0);
8808 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8809 if (M == VecInMap.end()) {
8810 VT = ExtractedFromVec.getValueType();
8811 // Quit if not 128/256-bit vector.
8812 if (!VT.is128BitVector() && !VT.is256BitVector())
8813 return SDValue();
8814 // Quit if not the same type.
8815 if (VecInMap.begin() != VecInMap.end() &&
8816 VT != VecInMap.begin()->first.getValueType())
8817 return SDValue();
8818 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8819 }
8820 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8821 }
8822
8823 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008824 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008825
8826 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8827 SmallVector<SDValue, 8> VecIns;
8828
8829 for (DenseMap<SDValue, unsigned>::const_iterator
8830 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8831 // Quit if not all elements are used.
8832 if (I->second != FullMask)
8833 return SDValue();
8834 VecIns.push_back(I->first);
8835 }
8836
8837 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8838
8839 // Cast all vectors into TestVT for PTEST.
8840 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8841 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8842
8843 // If more than one full vectors are evaluated, OR them first before PTEST.
8844 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8845 // Each iteration will OR 2 nodes and append the result until there is only
8846 // 1 node left, i.e. the final OR'd value of all vectors.
8847 SDValue LHS = VecIns[Slot];
8848 SDValue RHS = VecIns[Slot + 1];
8849 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8850 }
8851
8852 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8853 VecIns.back(), VecIns.back());
8854}
8855
Dan Gohman076aee32009-03-04 19:44:21 +00008856/// Emit nodes that will be selected as "test Op0,Op0", or something
8857/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008858SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008859 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008860 DebugLoc dl = Op.getDebugLoc();
8861
Dan Gohman31125812009-03-07 01:58:32 +00008862 // CF and OF aren't always set the way we want. Determine which
8863 // of these we need.
8864 bool NeedCF = false;
8865 bool NeedOF = false;
8866 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008867 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008868 case X86::COND_A: case X86::COND_AE:
8869 case X86::COND_B: case X86::COND_BE:
8870 NeedCF = true;
8871 break;
8872 case X86::COND_G: case X86::COND_GE:
8873 case X86::COND_L: case X86::COND_LE:
8874 case X86::COND_O: case X86::COND_NO:
8875 NeedOF = true;
8876 break;
Dan Gohman31125812009-03-07 01:58:32 +00008877 }
8878
Dan Gohman076aee32009-03-04 19:44:21 +00008879 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008880 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8881 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008882 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8883 // Emit a CMP with 0, which is the TEST pattern.
8884 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8885 DAG.getConstant(0, Op.getValueType()));
8886
8887 unsigned Opcode = 0;
8888 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008889
8890 // Truncate operations may prevent the merge of the SETCC instruction
8891 // and the arithmetic intruction before it. Attempt to truncate the operands
8892 // of the arithmetic instruction and use a reduced bit-width instruction.
8893 bool NeedTruncation = false;
8894 SDValue ArithOp = Op;
8895 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8896 SDValue Arith = Op->getOperand(0);
8897 // Both the trunc and the arithmetic op need to have one user each.
8898 if (Arith->hasOneUse())
8899 switch (Arith.getOpcode()) {
8900 default: break;
8901 case ISD::ADD:
8902 case ISD::SUB:
8903 case ISD::AND:
8904 case ISD::OR:
8905 case ISD::XOR: {
8906 NeedTruncation = true;
8907 ArithOp = Arith;
8908 }
8909 }
8910 }
8911
8912 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8913 // which may be the result of a CAST. We use the variable 'Op', which is the
8914 // non-casted variable when we check for possible users.
8915 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008916 case ISD::ADD:
8917 // Due to an isel shortcoming, be conservative if this add is likely to be
8918 // selected as part of a load-modify-store instruction. When the root node
8919 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8920 // uses of other nodes in the match, such as the ADD in this case. This
8921 // leads to the ADD being left around and reselected, with the result being
8922 // two adds in the output. Alas, even if none our users are stores, that
8923 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8924 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8925 // climbing the DAG back to the root, and it doesn't seem to be worth the
8926 // effort.
8927 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008928 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8929 if (UI->getOpcode() != ISD::CopyToReg &&
8930 UI->getOpcode() != ISD::SETCC &&
8931 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008932 goto default_case;
8933
8934 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008935 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008936 // An add of one will be selected as an INC.
8937 if (C->getAPIntValue() == 1) {
8938 Opcode = X86ISD::INC;
8939 NumOperands = 1;
8940 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008941 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008942
8943 // An add of negative one (subtract of one) will be selected as a DEC.
8944 if (C->getAPIntValue().isAllOnesValue()) {
8945 Opcode = X86ISD::DEC;
8946 NumOperands = 1;
8947 break;
8948 }
Dan Gohman076aee32009-03-04 19:44:21 +00008949 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008950
8951 // Otherwise use a regular EFLAGS-setting add.
8952 Opcode = X86ISD::ADD;
8953 NumOperands = 2;
8954 break;
8955 case ISD::AND: {
8956 // If the primary and result isn't used, don't bother using X86ISD::AND,
8957 // because a TEST instruction will be better.
8958 bool NonFlagUse = false;
8959 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8960 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8961 SDNode *User = *UI;
8962 unsigned UOpNo = UI.getOperandNo();
8963 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8964 // Look pass truncate.
8965 UOpNo = User->use_begin().getOperandNo();
8966 User = *User->use_begin();
8967 }
8968
8969 if (User->getOpcode() != ISD::BRCOND &&
8970 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008971 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008972 NonFlagUse = true;
8973 break;
8974 }
Dan Gohman076aee32009-03-04 19:44:21 +00008975 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008976
8977 if (!NonFlagUse)
8978 break;
8979 }
8980 // FALL THROUGH
8981 case ISD::SUB:
8982 case ISD::OR:
8983 case ISD::XOR:
8984 // Due to the ISEL shortcoming noted above, be conservative if this op is
8985 // likely to be selected as part of a load-modify-store instruction.
8986 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8987 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8988 if (UI->getOpcode() == ISD::STORE)
8989 goto default_case;
8990
8991 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008992 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008993 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008994 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008995 case ISD::XOR: Opcode = X86ISD::XOR; break;
8996 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008997 case ISD::OR: {
8998 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8999 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9000 if (EFLAGS.getNode())
9001 return EFLAGS;
9002 }
9003 Opcode = X86ISD::OR;
9004 break;
9005 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009006 }
9007
9008 NumOperands = 2;
9009 break;
9010 case X86ISD::ADD:
9011 case X86ISD::SUB:
9012 case X86ISD::INC:
9013 case X86ISD::DEC:
9014 case X86ISD::OR:
9015 case X86ISD::XOR:
9016 case X86ISD::AND:
9017 return SDValue(Op.getNode(), 1);
9018 default:
9019 default_case:
9020 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009021 }
9022
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009023 // If we found that truncation is beneficial, perform the truncation and
9024 // update 'Op'.
9025 if (NeedTruncation) {
9026 EVT VT = Op.getValueType();
9027 SDValue WideVal = Op->getOperand(0);
9028 EVT WideVT = WideVal.getValueType();
9029 unsigned ConvertedOp = 0;
9030 // Use a target machine opcode to prevent further DAGCombine
9031 // optimizations that may separate the arithmetic operations
9032 // from the setcc node.
9033 switch (WideVal.getOpcode()) {
9034 default: break;
9035 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9036 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9037 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9038 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9039 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9040 }
9041
9042 if (ConvertedOp) {
9043 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9044 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9045 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9046 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9047 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9048 }
9049 }
9050 }
9051
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009052 if (Opcode == 0)
9053 // Emit a CMP with 0, which is the TEST pattern.
9054 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9055 DAG.getConstant(0, Op.getValueType()));
9056
9057 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9058 SmallVector<SDValue, 4> Ops;
9059 for (unsigned i = 0; i != NumOperands; ++i)
9060 Ops.push_back(Op.getOperand(i));
9061
9062 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9063 DAG.ReplaceAllUsesWith(Op, New);
9064 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009065}
9066
9067/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9068/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009069SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009070 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009071 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9072 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009073 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009074
9075 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009076 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9077 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9078 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9079 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9080 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9081 Op0, Op1);
9082 return SDValue(Sub.getNode(), 1);
9083 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009084 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009085}
9086
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009087/// Convert a comparison if required by the subtarget.
9088SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9089 SelectionDAG &DAG) const {
9090 // If the subtarget does not support the FUCOMI instruction, floating-point
9091 // comparisons have to be converted.
9092 if (Subtarget->hasCMov() ||
9093 Cmp.getOpcode() != X86ISD::CMP ||
9094 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9095 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9096 return Cmp;
9097
9098 // The instruction selector will select an FUCOM instruction instead of
9099 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9100 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9101 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9102 DebugLoc dl = Cmp.getDebugLoc();
9103 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9104 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9105 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9106 DAG.getConstant(8, MVT::i8));
9107 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9108 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9109}
9110
Evan Cheng4e544802012-12-05 00:10:38 +00009111static bool isAllOnes(SDValue V) {
9112 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9113 return C && C->isAllOnesValue();
9114}
9115
Evan Chengd40d03e2010-01-06 19:38:29 +00009116/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9117/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009118SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9119 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009120 SDValue Op0 = And.getOperand(0);
9121 SDValue Op1 = And.getOperand(1);
9122 if (Op0.getOpcode() == ISD::TRUNCATE)
9123 Op0 = Op0.getOperand(0);
9124 if (Op1.getOpcode() == ISD::TRUNCATE)
9125 Op1 = Op1.getOperand(0);
9126
Evan Chengd40d03e2010-01-06 19:38:29 +00009127 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009128 if (Op1.getOpcode() == ISD::SHL)
9129 std::swap(Op0, Op1);
9130 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009131 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9132 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009133 // If we looked past a truncate, check that it's only truncating away
9134 // known zeros.
9135 unsigned BitWidth = Op0.getValueSizeInBits();
9136 unsigned AndBitWidth = And.getValueSizeInBits();
9137 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009138 APInt Zeros, Ones;
9139 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009140 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9141 return SDValue();
9142 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009143 LHS = Op1;
9144 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009145 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009146 } else if (Op1.getOpcode() == ISD::Constant) {
9147 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009148 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009149 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009150
9151 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009152 LHS = AndLHS.getOperand(0);
9153 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009154 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009155
9156 // Use BT if the immediate can't be encoded in a TEST instruction.
9157 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9158 LHS = AndLHS;
9159 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9160 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009161 }
Evan Cheng0488db92007-09-25 01:57:46 +00009162
Evan Chengd40d03e2010-01-06 19:38:29 +00009163 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009164 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9165 // the condition code later.
9166 bool Invert = false;
9167 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9168 Invert = true;
9169 LHS = LHS.getOperand(0);
9170 }
9171
Evan Chenge5b51ac2010-04-17 06:13:15 +00009172 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009173 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009174 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009175 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009176 // Also promote i16 to i32 for performance / code size reason.
9177 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009178 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009179 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009180
Evan Chengd40d03e2010-01-06 19:38:29 +00009181 // If the operand types disagree, extend the shift amount to match. Since
9182 // BT ignores high bits (like shifts) we can use anyextend.
9183 if (LHS.getValueType() != RHS.getValueType())
9184 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009185
Evan Chengd40d03e2010-01-06 19:38:29 +00009186 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009187 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9188 // Flip the condition if the LHS was a not instruction
9189 if (Invert)
9190 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009191 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9192 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009193 }
9194
Evan Cheng54de3ea2010-01-05 06:52:31 +00009195 return SDValue();
9196}
9197
Craig Topper89af15e2011-09-18 08:03:58 +00009198// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009199// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009200static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009201 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009202
Craig Topper7a9a28b2012-08-12 02:23:29 +00009203 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009204 "Unsupported value type for operation");
9205
Craig Topper66ddd152012-04-27 22:54:43 +00009206 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009207 DebugLoc dl = Op.getDebugLoc();
9208 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009209
9210 // Extract the LHS vectors
9211 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009212 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9213 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009214
9215 // Extract the RHS vectors
9216 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009217 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9218 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009219
9220 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009221 MVT EltVT = VT.getVectorElementType();
9222 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009223 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9224 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9225 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9226}
9227
Craig Topper26827f32013-01-20 09:02:22 +00009228static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9229 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009230 SDValue Cond;
9231 SDValue Op0 = Op.getOperand(0);
9232 SDValue Op1 = Op.getOperand(1);
9233 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009234 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009235 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009236 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009237 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009238
9239 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009240#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009241 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009242 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9243#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009244
Craig Topper523908d2012-08-13 02:34:03 +00009245 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009246 bool Swap = false;
9247
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009248 // SSE Condition code mapping:
9249 // 0 - EQ
9250 // 1 - LT
9251 // 2 - LE
9252 // 3 - UNORD
9253 // 4 - NEQ
9254 // 5 - NLT
9255 // 6 - NLE
9256 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009257 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009258 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009259 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009260 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009261 case ISD::SETOGT:
9262 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009263 case ISD::SETLT:
9264 case ISD::SETOLT: SSECC = 1; break;
9265 case ISD::SETOGE:
9266 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009267 case ISD::SETLE:
9268 case ISD::SETOLE: SSECC = 2; break;
9269 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009270 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009271 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009272 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009273 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009274 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009275 case ISD::SETUGT: SSECC = 6; break;
9276 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009277 case ISD::SETUEQ:
9278 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009279 }
9280 if (Swap)
9281 std::swap(Op0, Op1);
9282
Nate Begemanfb8ead02008-07-25 19:05:58 +00009283 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009284 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009285 unsigned CC0, CC1;
9286 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009287 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009288 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9289 } else {
9290 assert(SetCCOpcode == ISD::SETONE);
9291 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009292 }
Craig Topper523908d2012-08-13 02:34:03 +00009293
9294 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9295 DAG.getConstant(CC0, MVT::i8));
9296 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9297 DAG.getConstant(CC1, MVT::i8));
9298 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009299 }
9300 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009301 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9302 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009304
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009305 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009306 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009307 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009308
Nate Begeman30a0de92008-07-17 16:51:19 +00009309 // We are handling one of the integer comparisons here. Since SSE only has
9310 // GT and EQ comparisons for integer, swapping operands and multiple
9311 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009312 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009313 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009314
Nate Begeman30a0de92008-07-17 16:51:19 +00009315 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009316 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009318 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009319 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009320 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009322 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009323 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009324 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009325 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009326 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009327 }
9328 if (Swap)
9329 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009330
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009331 // Check that the operation in question is available (most are plain SSE2,
9332 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009333 if (VT == MVT::v2i64) {
9334 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9335 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009336 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9337 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009338 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009339 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9340
9341 // First cast everything to the right type,
9342 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9343 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9344
9345 // Do the compare.
9346 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9347
9348 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009349 const int Mask[] = { 1, 0, 3, 2 };
9350 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9351 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009352
9353 if (Invert)
9354 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9355
9356 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9357 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009358 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009359
Nate Begeman30a0de92008-07-17 16:51:19 +00009360 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9361 // bits of the inputs before performing those operations.
9362 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009363 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009364 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9365 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009366 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009367 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9368 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009369 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9370 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009372
Dale Johannesenace16102009-02-03 19:33:06 +00009373 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009374
9375 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009376 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009377 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009378
Nate Begeman30a0de92008-07-17 16:51:19 +00009379 return Result;
9380}
Evan Cheng0488db92007-09-25 01:57:46 +00009381
Craig Topper26827f32013-01-20 09:02:22 +00009382SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9383
9384 MVT VT = Op.getValueType().getSimpleVT();
9385
9386 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9387
9388 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9389 SDValue Op0 = Op.getOperand(0);
9390 SDValue Op1 = Op.getOperand(1);
9391 DebugLoc dl = Op.getDebugLoc();
9392 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9393
9394 // Optimize to BT if possible.
9395 // Lower (X & (1 << N)) == 0 to BT(X, N).
9396 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9397 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9398 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9399 Op1.getOpcode() == ISD::Constant &&
9400 cast<ConstantSDNode>(Op1)->isNullValue() &&
9401 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9402 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9403 if (NewSetCC.getNode())
9404 return NewSetCC;
9405 }
9406
9407 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9408 // these.
9409 if (Op1.getOpcode() == ISD::Constant &&
9410 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9411 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9412 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9413
9414 // If the input is a setcc, then reuse the input setcc or use a new one with
9415 // the inverted condition.
9416 if (Op0.getOpcode() == X86ISD::SETCC) {
9417 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9418 bool Invert = (CC == ISD::SETNE) ^
9419 cast<ConstantSDNode>(Op1)->isNullValue();
9420 if (!Invert) return Op0;
9421
9422 CCode = X86::GetOppositeBranchCondition(CCode);
9423 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9424 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9425 }
9426 }
9427
9428 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9429 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9430 if (X86CC == X86::COND_INVALID)
9431 return SDValue();
9432
9433 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9434 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9435 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9436 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9437}
9438
Evan Cheng370e5342008-12-03 08:38:43 +00009439// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009440static bool isX86LogicalCmp(SDValue Op) {
9441 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009442 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9443 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009444 return true;
9445 if (Op.getResNo() == 1 &&
9446 (Opc == X86ISD::ADD ||
9447 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009448 Opc == X86ISD::ADC ||
9449 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009450 Opc == X86ISD::SMUL ||
9451 Opc == X86ISD::UMUL ||
9452 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009453 Opc == X86ISD::DEC ||
9454 Opc == X86ISD::OR ||
9455 Opc == X86ISD::XOR ||
9456 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009457 return true;
9458
Chris Lattner9637d5b2010-12-05 07:49:54 +00009459 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9460 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009461
Dan Gohman076aee32009-03-04 19:44:21 +00009462 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009463}
9464
Chris Lattnera2b56002010-12-05 01:23:24 +00009465static bool isZero(SDValue V) {
9466 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9467 return C && C->isNullValue();
9468}
9469
Evan Chengb64dd5f2012-08-07 22:21:00 +00009470static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9471 if (V.getOpcode() != ISD::TRUNCATE)
9472 return false;
9473
9474 SDValue VOp0 = V.getOperand(0);
9475 unsigned InBits = VOp0.getValueSizeInBits();
9476 unsigned Bits = V.getValueSizeInBits();
9477 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9478}
9479
Dan Gohmand858e902010-04-17 15:26:15 +00009480SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009481 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009482 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009483 SDValue Op1 = Op.getOperand(1);
9484 SDValue Op2 = Op.getOperand(2);
9485 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009486 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009487
Dan Gohman1a492952009-10-20 16:22:37 +00009488 if (Cond.getOpcode() == ISD::SETCC) {
9489 SDValue NewCond = LowerSETCC(Cond, DAG);
9490 if (NewCond.getNode())
9491 Cond = NewCond;
9492 }
Evan Cheng734503b2006-09-11 02:19:56 +00009493
Chris Lattnera2b56002010-12-05 01:23:24 +00009494 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009495 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009496 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009497 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009498 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009499 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9500 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009501 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009502
Chris Lattnera2b56002010-12-05 01:23:24 +00009503 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009504
9505 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009506 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9507 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009508
9509 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009510 // Apply further optimizations for special cases
9511 // (select (x != 0), -1, 0) -> neg & sbb
9512 // (select (x == 0), 0, -1) -> neg & sbb
9513 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009514 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009515 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9516 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009517 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9518 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009519 CmpOp0);
9520 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9521 DAG.getConstant(X86::COND_B, MVT::i8),
9522 SDValue(Neg.getNode(), 1));
9523 return Res;
9524 }
9525
Chris Lattnera2b56002010-12-05 01:23:24 +00009526 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9527 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009528 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009529
Chris Lattner96908b12010-12-05 02:00:51 +00009530 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009531 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9532 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009533
Chris Lattner96908b12010-12-05 02:00:51 +00009534 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9535 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009536
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009537 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009538 if (N2C == 0 || !N2C->isNullValue())
9539 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9540 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009541 }
9542 }
9543
Chris Lattnera2b56002010-12-05 01:23:24 +00009544 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009545 if (Cond.getOpcode() == ISD::AND &&
9546 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9547 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009548 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009549 Cond = Cond.getOperand(0);
9550 }
9551
Evan Cheng3f41d662007-10-08 22:16:29 +00009552 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9553 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009554 unsigned CondOpcode = Cond.getOpcode();
9555 if (CondOpcode == X86ISD::SETCC ||
9556 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009557 CC = Cond.getOperand(0);
9558
Dan Gohman475871a2008-07-27 21:46:04 +00009559 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009560 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009561 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009562
Evan Cheng3f41d662007-10-08 22:16:29 +00009563 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009564 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009565 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009566 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009567
Chris Lattnerd1980a52009-03-12 06:52:53 +00009568 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9569 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009570 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009571 addTest = false;
9572 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009573 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9574 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9575 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9576 Cond.getOperand(0).getValueType() != MVT::i8)) {
9577 SDValue LHS = Cond.getOperand(0);
9578 SDValue RHS = Cond.getOperand(1);
9579 unsigned X86Opcode;
9580 unsigned X86Cond;
9581 SDVTList VTs;
9582 switch (CondOpcode) {
9583 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9584 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9585 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9586 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9587 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9588 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9589 default: llvm_unreachable("unexpected overflowing operator");
9590 }
9591 if (CondOpcode == ISD::UMULO)
9592 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9593 MVT::i32);
9594 else
9595 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9596
9597 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9598
9599 if (CondOpcode == ISD::UMULO)
9600 Cond = X86Op.getValue(2);
9601 else
9602 Cond = X86Op.getValue(1);
9603
9604 CC = DAG.getConstant(X86Cond, MVT::i8);
9605 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009606 }
9607
9608 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009609 // Look pass the truncate if the high bits are known zero.
9610 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9611 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009612
9613 // We know the result of AND is compared against zero. Try to match
9614 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009615 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009616 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009617 if (NewSetCC.getNode()) {
9618 CC = NewSetCC.getOperand(0);
9619 Cond = NewSetCC.getOperand(1);
9620 addTest = false;
9621 }
9622 }
9623 }
9624
9625 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009626 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009627 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009628 }
9629
Benjamin Kramere915ff32010-12-22 23:09:28 +00009630 // a < b ? -1 : 0 -> RES = ~setcc_carry
9631 // a < b ? 0 : -1 -> RES = setcc_carry
9632 // a >= b ? -1 : 0 -> RES = setcc_carry
9633 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009634 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009635 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009636 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9637
9638 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9639 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9640 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9641 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9642 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9643 return DAG.getNOT(DL, Res, Res.getValueType());
9644 return Res;
9645 }
9646 }
9647
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009648 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9649 // widen the cmov and push the truncate through. This avoids introducing a new
9650 // branch during isel and doesn't add any extensions.
9651 if (Op.getValueType() == MVT::i8 &&
9652 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9653 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9654 if (T1.getValueType() == T2.getValueType() &&
9655 // Blacklist CopyFromReg to avoid partial register stalls.
9656 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9657 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009658 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009659 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9660 }
9661 }
9662
Evan Cheng0488db92007-09-25 01:57:46 +00009663 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9664 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009665 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009666 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009667 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009668}
9669
Nadav Rotem1a330af2012-12-27 22:47:16 +00009670SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9671 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009672 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009673 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009674 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009675 DebugLoc dl = Op->getDebugLoc();
9676
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009677 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9678 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9679 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009680
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009681 if (Subtarget->hasInt256())
9682 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009683
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009684 // Optimize vectors in AVX mode
9685 // Sign extend v8i16 to v8i32 and
9686 // v4i32 to v4i64
9687 //
9688 // Divide input vector into two parts
9689 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9690 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9691 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009692
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009693 unsigned NumElems = InVT.getVectorNumElements();
9694 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009695
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009696 SmallVector<int,8> ShufMask1(NumElems, -1);
9697 for (unsigned i = 0; i != NumElems/2; ++i)
9698 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009699
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009700 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009701
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009702 SmallVector<int,8> ShufMask2(NumElems, -1);
9703 for (unsigned i = 0; i != NumElems/2; ++i)
9704 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009705
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009706 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009707
Craig Toppera080daf2013-01-20 21:50:27 +00009708 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009709 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009710
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009711 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9712 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009713
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009714 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009715}
9716
Evan Cheng370e5342008-12-03 08:38:43 +00009717// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9718// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9719// from the AND / OR.
9720static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9721 Opc = Op.getOpcode();
9722 if (Opc != ISD::OR && Opc != ISD::AND)
9723 return false;
9724 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9725 Op.getOperand(0).hasOneUse() &&
9726 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9727 Op.getOperand(1).hasOneUse());
9728}
9729
Evan Cheng961d6d42009-02-02 08:19:07 +00009730// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9731// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009732static bool isXor1OfSetCC(SDValue Op) {
9733 if (Op.getOpcode() != ISD::XOR)
9734 return false;
9735 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9736 if (N1C && N1C->getAPIntValue() == 1) {
9737 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9738 Op.getOperand(0).hasOneUse();
9739 }
9740 return false;
9741}
9742
Dan Gohmand858e902010-04-17 15:26:15 +00009743SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009744 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009745 SDValue Chain = Op.getOperand(0);
9746 SDValue Cond = Op.getOperand(1);
9747 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009748 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009750 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009751
Dan Gohman1a492952009-10-20 16:22:37 +00009752 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009753 // Check for setcc([su]{add,sub,mul}o == 0).
9754 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9755 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9756 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9757 Cond.getOperand(0).getResNo() == 1 &&
9758 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9759 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9760 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9761 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9762 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9763 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9764 Inverted = true;
9765 Cond = Cond.getOperand(0);
9766 } else {
9767 SDValue NewCond = LowerSETCC(Cond, DAG);
9768 if (NewCond.getNode())
9769 Cond = NewCond;
9770 }
Dan Gohman1a492952009-10-20 16:22:37 +00009771 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009772#if 0
9773 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009774 else if (Cond.getOpcode() == X86ISD::ADD ||
9775 Cond.getOpcode() == X86ISD::SUB ||
9776 Cond.getOpcode() == X86ISD::SMUL ||
9777 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009778 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009779#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009780
Evan Chengad9c0a32009-12-15 00:53:42 +00009781 // Look pass (and (setcc_carry (cmp ...)), 1).
9782 if (Cond.getOpcode() == ISD::AND &&
9783 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9784 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009785 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009786 Cond = Cond.getOperand(0);
9787 }
9788
Evan Cheng3f41d662007-10-08 22:16:29 +00009789 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9790 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009791 unsigned CondOpcode = Cond.getOpcode();
9792 if (CondOpcode == X86ISD::SETCC ||
9793 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009794 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009795
Dan Gohman475871a2008-07-27 21:46:04 +00009796 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009797 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009798 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009799 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009800 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009801 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009802 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009803 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009804 default: break;
9805 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009806 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009807 // These can only come from an arithmetic instruction with overflow,
9808 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009809 Cond = Cond.getNode()->getOperand(1);
9810 addTest = false;
9811 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009812 }
Evan Cheng0488db92007-09-25 01:57:46 +00009813 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009814 }
9815 CondOpcode = Cond.getOpcode();
9816 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9817 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9818 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9819 Cond.getOperand(0).getValueType() != MVT::i8)) {
9820 SDValue LHS = Cond.getOperand(0);
9821 SDValue RHS = Cond.getOperand(1);
9822 unsigned X86Opcode;
9823 unsigned X86Cond;
9824 SDVTList VTs;
9825 switch (CondOpcode) {
9826 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9827 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9828 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9829 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9830 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9831 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9832 default: llvm_unreachable("unexpected overflowing operator");
9833 }
9834 if (Inverted)
9835 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9836 if (CondOpcode == ISD::UMULO)
9837 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9838 MVT::i32);
9839 else
9840 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9841
9842 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9843
9844 if (CondOpcode == ISD::UMULO)
9845 Cond = X86Op.getValue(2);
9846 else
9847 Cond = X86Op.getValue(1);
9848
9849 CC = DAG.getConstant(X86Cond, MVT::i8);
9850 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009851 } else {
9852 unsigned CondOpc;
9853 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9854 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009855 if (CondOpc == ISD::OR) {
9856 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9857 // two branches instead of an explicit OR instruction with a
9858 // separate test.
9859 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009860 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009861 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009862 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009863 Chain, Dest, CC, Cmp);
9864 CC = Cond.getOperand(1).getOperand(0);
9865 Cond = Cmp;
9866 addTest = false;
9867 }
9868 } else { // ISD::AND
9869 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9870 // two branches instead of an explicit AND instruction with a
9871 // separate test. However, we only do this if this block doesn't
9872 // have a fall-through edge, because this requires an explicit
9873 // jmp when the condition is false.
9874 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009875 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009876 Op.getNode()->hasOneUse()) {
9877 X86::CondCode CCode =
9878 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9879 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009880 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009881 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009882 // Look for an unconditional branch following this conditional branch.
9883 // We need this because we need to reverse the successors in order
9884 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009885 if (User->getOpcode() == ISD::BR) {
9886 SDValue FalseBB = User->getOperand(1);
9887 SDNode *NewBR =
9888 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009889 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009890 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009891 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009892
Dale Johannesene4d209d2009-02-03 20:21:25 +00009893 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009894 Chain, Dest, CC, Cmp);
9895 X86::CondCode CCode =
9896 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9897 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009898 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009899 Cond = Cmp;
9900 addTest = false;
9901 }
9902 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009903 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009904 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9905 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9906 // It should be transformed during dag combiner except when the condition
9907 // is set by a arithmetics with overflow node.
9908 X86::CondCode CCode =
9909 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9910 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009911 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009912 Cond = Cond.getOperand(0).getOperand(1);
9913 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009914 } else if (Cond.getOpcode() == ISD::SETCC &&
9915 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9916 // For FCMP_OEQ, we can emit
9917 // two branches instead of an explicit AND instruction with a
9918 // separate test. However, we only do this if this block doesn't
9919 // have a fall-through edge, because this requires an explicit
9920 // jmp when the condition is false.
9921 if (Op.getNode()->hasOneUse()) {
9922 SDNode *User = *Op.getNode()->use_begin();
9923 // Look for an unconditional branch following this conditional branch.
9924 // We need this because we need to reverse the successors in order
9925 // to implement FCMP_OEQ.
9926 if (User->getOpcode() == ISD::BR) {
9927 SDValue FalseBB = User->getOperand(1);
9928 SDNode *NewBR =
9929 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9930 assert(NewBR == User);
9931 (void)NewBR;
9932 Dest = FalseBB;
9933
9934 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9935 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009936 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009937 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9938 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9939 Chain, Dest, CC, Cmp);
9940 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9941 Cond = Cmp;
9942 addTest = false;
9943 }
9944 }
9945 } else if (Cond.getOpcode() == ISD::SETCC &&
9946 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9947 // For FCMP_UNE, we can emit
9948 // two branches instead of an explicit AND instruction with a
9949 // separate test. However, we only do this if this block doesn't
9950 // have a fall-through edge, because this requires an explicit
9951 // jmp when the condition is false.
9952 if (Op.getNode()->hasOneUse()) {
9953 SDNode *User = *Op.getNode()->use_begin();
9954 // Look for an unconditional branch following this conditional branch.
9955 // We need this because we need to reverse the successors in order
9956 // to implement FCMP_UNE.
9957 if (User->getOpcode() == ISD::BR) {
9958 SDValue FalseBB = User->getOperand(1);
9959 SDNode *NewBR =
9960 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9961 assert(NewBR == User);
9962 (void)NewBR;
9963
9964 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9965 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009966 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009967 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9968 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9969 Chain, Dest, CC, Cmp);
9970 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9971 Cond = Cmp;
9972 addTest = false;
9973 Dest = FalseBB;
9974 }
9975 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009976 }
Evan Cheng0488db92007-09-25 01:57:46 +00009977 }
9978
9979 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009980 // Look pass the truncate if the high bits are known zero.
9981 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9982 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009983
9984 // We know the result of AND is compared against zero. Try to match
9985 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009986 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009987 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9988 if (NewSetCC.getNode()) {
9989 CC = NewSetCC.getOperand(0);
9990 Cond = NewSetCC.getOperand(1);
9991 addTest = false;
9992 }
9993 }
9994 }
9995
9996 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009997 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009998 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009999 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010000 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010001 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010002 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010003}
10004
Anton Korobeynikove060b532007-04-17 19:34:00 +000010005// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10006// Calls to _alloca is needed to probe the stack when allocating more than 4k
10007// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10008// that the guard pages used by the OS virtual memory manager are allocated in
10009// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010010SDValue
10011X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010012 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010013 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010014 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010015 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010016 "are being used");
10017 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010018 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010019
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010020 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010021 SDValue Chain = Op.getOperand(0);
10022 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010023 // FIXME: Ensure alignment here
10024
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010025 bool Is64Bit = Subtarget->is64Bit();
10026 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010027
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010028 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010029 MachineFunction &MF = DAG.getMachineFunction();
10030 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010031
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010032 if (Is64Bit) {
10033 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010034 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010035 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010036
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010037 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010038 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010039 if (I->hasNestAttr())
10040 report_fatal_error("Cannot use segmented stacks with functions that "
10041 "have nested arguments.");
10042 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010043
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010044 const TargetRegisterClass *AddrRegClass =
10045 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10046 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10047 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10048 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10049 DAG.getRegister(Vreg, SPTy));
10050 SDValue Ops1[2] = { Value, Chain };
10051 return DAG.getMergeValues(Ops1, 2, dl);
10052 } else {
10053 SDValue Flag;
10054 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010055
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010056 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10057 Flag = Chain.getValue(1);
10058 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010059
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010060 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10061 Flag = Chain.getValue(1);
10062
Michael Liaoc5c970e2012-10-31 04:14:09 +000010063 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10064 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010065
10066 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10067 return DAG.getMergeValues(Ops1, 2, dl);
10068 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010069}
10070
Dan Gohmand858e902010-04-17 15:26:15 +000010071SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010072 MachineFunction &MF = DAG.getMachineFunction();
10073 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10074
Dan Gohman69de1932008-02-06 22:27:42 +000010075 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010076 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010077
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010078 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010079 // vastart just stores the address of the VarArgsFrameIndex slot into the
10080 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010081 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10082 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010083 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10084 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010085 }
10086
10087 // __va_list_tag:
10088 // gp_offset (0 - 6 * 8)
10089 // fp_offset (48 - 48 + 8 * 16)
10090 // overflow_arg_area (point to parameters coming in memory).
10091 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010092 SmallVector<SDValue, 8> MemOps;
10093 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010094 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010095 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010096 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10097 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010098 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010099 MemOps.push_back(Store);
10100
10101 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010102 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010103 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010104 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010105 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10106 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010107 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010108 MemOps.push_back(Store);
10109
10110 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010111 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010112 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010113 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10114 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010115 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10116 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010117 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010118 MemOps.push_back(Store);
10119
10120 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010121 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010122 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010123 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10124 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010125 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10126 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010127 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010128 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010129 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010130}
10131
Dan Gohmand858e902010-04-17 15:26:15 +000010132SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010133 assert(Subtarget->is64Bit() &&
10134 "LowerVAARG only handles 64-bit va_arg!");
10135 assert((Subtarget->isTargetLinux() ||
10136 Subtarget->isTargetDarwin()) &&
10137 "Unhandled target in LowerVAARG");
10138 assert(Op.getNode()->getNumOperands() == 4);
10139 SDValue Chain = Op.getOperand(0);
10140 SDValue SrcPtr = Op.getOperand(1);
10141 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10142 unsigned Align = Op.getConstantOperandVal(3);
10143 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010144
Dan Gohman320afb82010-10-12 18:00:49 +000010145 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010146 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010147 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010148 uint8_t ArgMode;
10149
10150 // Decide which area this value should be read from.
10151 // TODO: Implement the AMD64 ABI in its entirety. This simple
10152 // selection mechanism works only for the basic types.
10153 if (ArgVT == MVT::f80) {
10154 llvm_unreachable("va_arg for f80 not yet implemented");
10155 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10156 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10157 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10158 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10159 } else {
10160 llvm_unreachable("Unhandled argument type in LowerVAARG");
10161 }
10162
10163 if (ArgMode == 2) {
10164 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010165 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010166 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010167 .getFunction()->getAttributes()
10168 .hasAttribute(AttributeSet::FunctionIndex,
10169 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010170 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010171 }
10172
10173 // Insert VAARG_64 node into the DAG
10174 // VAARG_64 returns two values: Variable Argument Address, Chain
10175 SmallVector<SDValue, 11> InstOps;
10176 InstOps.push_back(Chain);
10177 InstOps.push_back(SrcPtr);
10178 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10179 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10180 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10181 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10182 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10183 VTs, &InstOps[0], InstOps.size(),
10184 MVT::i64,
10185 MachinePointerInfo(SV),
10186 /*Align=*/0,
10187 /*Volatile=*/false,
10188 /*ReadMem=*/true,
10189 /*WriteMem=*/true);
10190 Chain = VAARG.getValue(1);
10191
10192 // Load the next argument and return it
10193 return DAG.getLoad(ArgVT, dl,
10194 Chain,
10195 VAARG,
10196 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010197 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010198}
10199
Craig Topper55b24052012-09-11 06:15:32 +000010200static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10201 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010202 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010203 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010204 SDValue Chain = Op.getOperand(0);
10205 SDValue DstPtr = Op.getOperand(1);
10206 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010207 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10208 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010209 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010210
Chris Lattnere72f2022010-09-21 05:40:29 +000010211 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010212 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010213 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010214 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010215}
10216
Craig Topperff3139f2013-02-19 07:43:59 +000010217// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010218// may or may not be a constant. Takes immediate version of shift as input.
10219static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10220 SDValue SrcOp, SDValue ShAmt,
10221 SelectionDAG &DAG) {
10222 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10223
10224 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010225 // Constant may be a TargetConstant. Use a regular constant.
10226 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010227 switch (Opc) {
10228 default: llvm_unreachable("Unknown target vector shift node");
10229 case X86ISD::VSHLI:
10230 case X86ISD::VSRLI:
10231 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010232 return DAG.getNode(Opc, dl, VT, SrcOp,
10233 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010234 }
10235 }
10236
10237 // Change opcode to non-immediate version
10238 switch (Opc) {
10239 default: llvm_unreachable("Unknown target vector shift node");
10240 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10241 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10242 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10243 }
10244
10245 // Need to build a vector containing shift amount
10246 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10247 SDValue ShOps[4];
10248 ShOps[0] = ShAmt;
10249 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010250 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010251 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010252
10253 // The return type has to be a 128-bit type with the same element
10254 // type as the input type.
10255 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10256 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10257
10258 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010259 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10260}
10261
Craig Topper55b24052012-09-11 06:15:32 +000010262static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010263 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010264 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010265 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010266 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010267 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010268 case Intrinsic::x86_sse_comieq_ss:
10269 case Intrinsic::x86_sse_comilt_ss:
10270 case Intrinsic::x86_sse_comile_ss:
10271 case Intrinsic::x86_sse_comigt_ss:
10272 case Intrinsic::x86_sse_comige_ss:
10273 case Intrinsic::x86_sse_comineq_ss:
10274 case Intrinsic::x86_sse_ucomieq_ss:
10275 case Intrinsic::x86_sse_ucomilt_ss:
10276 case Intrinsic::x86_sse_ucomile_ss:
10277 case Intrinsic::x86_sse_ucomigt_ss:
10278 case Intrinsic::x86_sse_ucomige_ss:
10279 case Intrinsic::x86_sse_ucomineq_ss:
10280 case Intrinsic::x86_sse2_comieq_sd:
10281 case Intrinsic::x86_sse2_comilt_sd:
10282 case Intrinsic::x86_sse2_comile_sd:
10283 case Intrinsic::x86_sse2_comigt_sd:
10284 case Intrinsic::x86_sse2_comige_sd:
10285 case Intrinsic::x86_sse2_comineq_sd:
10286 case Intrinsic::x86_sse2_ucomieq_sd:
10287 case Intrinsic::x86_sse2_ucomilt_sd:
10288 case Intrinsic::x86_sse2_ucomile_sd:
10289 case Intrinsic::x86_sse2_ucomigt_sd:
10290 case Intrinsic::x86_sse2_ucomige_sd:
10291 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010292 unsigned Opc;
10293 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010294 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010295 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010296 case Intrinsic::x86_sse_comieq_ss:
10297 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010298 Opc = X86ISD::COMI;
10299 CC = ISD::SETEQ;
10300 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010301 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010302 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010303 Opc = X86ISD::COMI;
10304 CC = ISD::SETLT;
10305 break;
10306 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010307 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010308 Opc = X86ISD::COMI;
10309 CC = ISD::SETLE;
10310 break;
10311 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010312 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010313 Opc = X86ISD::COMI;
10314 CC = ISD::SETGT;
10315 break;
10316 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010317 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010318 Opc = X86ISD::COMI;
10319 CC = ISD::SETGE;
10320 break;
10321 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010322 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010323 Opc = X86ISD::COMI;
10324 CC = ISD::SETNE;
10325 break;
10326 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010327 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010328 Opc = X86ISD::UCOMI;
10329 CC = ISD::SETEQ;
10330 break;
10331 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010332 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010333 Opc = X86ISD::UCOMI;
10334 CC = ISD::SETLT;
10335 break;
10336 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010337 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010338 Opc = X86ISD::UCOMI;
10339 CC = ISD::SETLE;
10340 break;
10341 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010342 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010343 Opc = X86ISD::UCOMI;
10344 CC = ISD::SETGT;
10345 break;
10346 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010347 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010348 Opc = X86ISD::UCOMI;
10349 CC = ISD::SETGE;
10350 break;
10351 case Intrinsic::x86_sse_ucomineq_ss:
10352 case Intrinsic::x86_sse2_ucomineq_sd:
10353 Opc = X86ISD::UCOMI;
10354 CC = ISD::SETNE;
10355 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010356 }
Evan Cheng734503b2006-09-11 02:19:56 +000010357
Dan Gohman475871a2008-07-27 21:46:04 +000010358 SDValue LHS = Op.getOperand(1);
10359 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010360 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010361 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010362 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10363 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10364 DAG.getConstant(X86CC, MVT::i8), Cond);
10365 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010366 }
Craig Topper6d688152012-08-14 07:43:25 +000010367
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010368 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010369 case Intrinsic::x86_sse2_pmulu_dq:
10370 case Intrinsic::x86_avx2_pmulu_dq:
10371 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10372 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010373
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010374 // SSE2/AVX2 sub with unsigned saturation intrinsics
10375 case Intrinsic::x86_sse2_psubus_b:
10376 case Intrinsic::x86_sse2_psubus_w:
10377 case Intrinsic::x86_avx2_psubus_b:
10378 case Intrinsic::x86_avx2_psubus_w:
10379 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10380 Op.getOperand(1), Op.getOperand(2));
10381
Craig Topper6d688152012-08-14 07:43:25 +000010382 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010383 case Intrinsic::x86_sse3_hadd_ps:
10384 case Intrinsic::x86_sse3_hadd_pd:
10385 case Intrinsic::x86_avx_hadd_ps_256:
10386 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010387 case Intrinsic::x86_sse3_hsub_ps:
10388 case Intrinsic::x86_sse3_hsub_pd:
10389 case Intrinsic::x86_avx_hsub_ps_256:
10390 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010391 case Intrinsic::x86_ssse3_phadd_w_128:
10392 case Intrinsic::x86_ssse3_phadd_d_128:
10393 case Intrinsic::x86_avx2_phadd_w:
10394 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010395 case Intrinsic::x86_ssse3_phsub_w_128:
10396 case Intrinsic::x86_ssse3_phsub_d_128:
10397 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010398 case Intrinsic::x86_avx2_phsub_d: {
10399 unsigned Opcode;
10400 switch (IntNo) {
10401 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10402 case Intrinsic::x86_sse3_hadd_ps:
10403 case Intrinsic::x86_sse3_hadd_pd:
10404 case Intrinsic::x86_avx_hadd_ps_256:
10405 case Intrinsic::x86_avx_hadd_pd_256:
10406 Opcode = X86ISD::FHADD;
10407 break;
10408 case Intrinsic::x86_sse3_hsub_ps:
10409 case Intrinsic::x86_sse3_hsub_pd:
10410 case Intrinsic::x86_avx_hsub_ps_256:
10411 case Intrinsic::x86_avx_hsub_pd_256:
10412 Opcode = X86ISD::FHSUB;
10413 break;
10414 case Intrinsic::x86_ssse3_phadd_w_128:
10415 case Intrinsic::x86_ssse3_phadd_d_128:
10416 case Intrinsic::x86_avx2_phadd_w:
10417 case Intrinsic::x86_avx2_phadd_d:
10418 Opcode = X86ISD::HADD;
10419 break;
10420 case Intrinsic::x86_ssse3_phsub_w_128:
10421 case Intrinsic::x86_ssse3_phsub_d_128:
10422 case Intrinsic::x86_avx2_phsub_w:
10423 case Intrinsic::x86_avx2_phsub_d:
10424 Opcode = X86ISD::HSUB;
10425 break;
10426 }
10427 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010428 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010429 }
10430
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010431 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10432 case Intrinsic::x86_sse2_pmaxu_b:
10433 case Intrinsic::x86_sse41_pmaxuw:
10434 case Intrinsic::x86_sse41_pmaxud:
10435 case Intrinsic::x86_avx2_pmaxu_b:
10436 case Intrinsic::x86_avx2_pmaxu_w:
10437 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010438 case Intrinsic::x86_sse2_pminu_b:
10439 case Intrinsic::x86_sse41_pminuw:
10440 case Intrinsic::x86_sse41_pminud:
10441 case Intrinsic::x86_avx2_pminu_b:
10442 case Intrinsic::x86_avx2_pminu_w:
10443 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010444 case Intrinsic::x86_sse41_pmaxsb:
10445 case Intrinsic::x86_sse2_pmaxs_w:
10446 case Intrinsic::x86_sse41_pmaxsd:
10447 case Intrinsic::x86_avx2_pmaxs_b:
10448 case Intrinsic::x86_avx2_pmaxs_w:
10449 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010450 case Intrinsic::x86_sse41_pminsb:
10451 case Intrinsic::x86_sse2_pmins_w:
10452 case Intrinsic::x86_sse41_pminsd:
10453 case Intrinsic::x86_avx2_pmins_b:
10454 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010455 case Intrinsic::x86_avx2_pmins_d: {
10456 unsigned Opcode;
10457 switch (IntNo) {
10458 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10459 case Intrinsic::x86_sse2_pmaxu_b:
10460 case Intrinsic::x86_sse41_pmaxuw:
10461 case Intrinsic::x86_sse41_pmaxud:
10462 case Intrinsic::x86_avx2_pmaxu_b:
10463 case Intrinsic::x86_avx2_pmaxu_w:
10464 case Intrinsic::x86_avx2_pmaxu_d:
10465 Opcode = X86ISD::UMAX;
10466 break;
10467 case Intrinsic::x86_sse2_pminu_b:
10468 case Intrinsic::x86_sse41_pminuw:
10469 case Intrinsic::x86_sse41_pminud:
10470 case Intrinsic::x86_avx2_pminu_b:
10471 case Intrinsic::x86_avx2_pminu_w:
10472 case Intrinsic::x86_avx2_pminu_d:
10473 Opcode = X86ISD::UMIN;
10474 break;
10475 case Intrinsic::x86_sse41_pmaxsb:
10476 case Intrinsic::x86_sse2_pmaxs_w:
10477 case Intrinsic::x86_sse41_pmaxsd:
10478 case Intrinsic::x86_avx2_pmaxs_b:
10479 case Intrinsic::x86_avx2_pmaxs_w:
10480 case Intrinsic::x86_avx2_pmaxs_d:
10481 Opcode = X86ISD::SMAX;
10482 break;
10483 case Intrinsic::x86_sse41_pminsb:
10484 case Intrinsic::x86_sse2_pmins_w:
10485 case Intrinsic::x86_sse41_pminsd:
10486 case Intrinsic::x86_avx2_pmins_b:
10487 case Intrinsic::x86_avx2_pmins_w:
10488 case Intrinsic::x86_avx2_pmins_d:
10489 Opcode = X86ISD::SMIN;
10490 break;
10491 }
10492 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010493 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010494 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010495
Craig Topper6d183e42012-12-29 16:44:25 +000010496 // SSE/SSE2/AVX floating point max/min intrinsics.
10497 case Intrinsic::x86_sse_max_ps:
10498 case Intrinsic::x86_sse2_max_pd:
10499 case Intrinsic::x86_avx_max_ps_256:
10500 case Intrinsic::x86_avx_max_pd_256:
10501 case Intrinsic::x86_sse_min_ps:
10502 case Intrinsic::x86_sse2_min_pd:
10503 case Intrinsic::x86_avx_min_ps_256:
10504 case Intrinsic::x86_avx_min_pd_256: {
10505 unsigned Opcode;
10506 switch (IntNo) {
10507 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10508 case Intrinsic::x86_sse_max_ps:
10509 case Intrinsic::x86_sse2_max_pd:
10510 case Intrinsic::x86_avx_max_ps_256:
10511 case Intrinsic::x86_avx_max_pd_256:
10512 Opcode = X86ISD::FMAX;
10513 break;
10514 case Intrinsic::x86_sse_min_ps:
10515 case Intrinsic::x86_sse2_min_pd:
10516 case Intrinsic::x86_avx_min_ps_256:
10517 case Intrinsic::x86_avx_min_pd_256:
10518 Opcode = X86ISD::FMIN;
10519 break;
10520 }
10521 return DAG.getNode(Opcode, dl, Op.getValueType(),
10522 Op.getOperand(1), Op.getOperand(2));
10523 }
10524
Craig Topper6d688152012-08-14 07:43:25 +000010525 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010526 case Intrinsic::x86_avx2_psllv_d:
10527 case Intrinsic::x86_avx2_psllv_q:
10528 case Intrinsic::x86_avx2_psllv_d_256:
10529 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010530 case Intrinsic::x86_avx2_psrlv_d:
10531 case Intrinsic::x86_avx2_psrlv_q:
10532 case Intrinsic::x86_avx2_psrlv_d_256:
10533 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010534 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010535 case Intrinsic::x86_avx2_psrav_d_256: {
10536 unsigned Opcode;
10537 switch (IntNo) {
10538 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10539 case Intrinsic::x86_avx2_psllv_d:
10540 case Intrinsic::x86_avx2_psllv_q:
10541 case Intrinsic::x86_avx2_psllv_d_256:
10542 case Intrinsic::x86_avx2_psllv_q_256:
10543 Opcode = ISD::SHL;
10544 break;
10545 case Intrinsic::x86_avx2_psrlv_d:
10546 case Intrinsic::x86_avx2_psrlv_q:
10547 case Intrinsic::x86_avx2_psrlv_d_256:
10548 case Intrinsic::x86_avx2_psrlv_q_256:
10549 Opcode = ISD::SRL;
10550 break;
10551 case Intrinsic::x86_avx2_psrav_d:
10552 case Intrinsic::x86_avx2_psrav_d_256:
10553 Opcode = ISD::SRA;
10554 break;
10555 }
10556 return DAG.getNode(Opcode, dl, Op.getValueType(),
10557 Op.getOperand(1), Op.getOperand(2));
10558 }
10559
Craig Topper969ba282012-01-25 06:43:11 +000010560 case Intrinsic::x86_ssse3_pshuf_b_128:
10561 case Intrinsic::x86_avx2_pshuf_b:
10562 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10563 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010564
Craig Topper969ba282012-01-25 06:43:11 +000010565 case Intrinsic::x86_ssse3_psign_b_128:
10566 case Intrinsic::x86_ssse3_psign_w_128:
10567 case Intrinsic::x86_ssse3_psign_d_128:
10568 case Intrinsic::x86_avx2_psign_b:
10569 case Intrinsic::x86_avx2_psign_w:
10570 case Intrinsic::x86_avx2_psign_d:
10571 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10572 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010573
Craig Toppere566cd02012-01-26 07:18:03 +000010574 case Intrinsic::x86_sse41_insertps:
10575 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10576 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010577
Craig Toppere566cd02012-01-26 07:18:03 +000010578 case Intrinsic::x86_avx_vperm2f128_ps_256:
10579 case Intrinsic::x86_avx_vperm2f128_pd_256:
10580 case Intrinsic::x86_avx_vperm2f128_si_256:
10581 case Intrinsic::x86_avx2_vperm2i128:
10582 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10583 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010584
Craig Topperffa6c402012-04-16 07:13:00 +000010585 case Intrinsic::x86_avx2_permd:
10586 case Intrinsic::x86_avx2_permps:
10587 // Operands intentionally swapped. Mask is last operand to intrinsic,
10588 // but second operand for node/intruction.
10589 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10590 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010591
Craig Topper22d8f0d2012-12-29 18:18:20 +000010592 case Intrinsic::x86_sse_sqrt_ps:
10593 case Intrinsic::x86_sse2_sqrt_pd:
10594 case Intrinsic::x86_avx_sqrt_ps_256:
10595 case Intrinsic::x86_avx_sqrt_pd_256:
10596 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10597
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010598 // ptest and testp intrinsics. The intrinsic these come from are designed to
10599 // return an integer value, not just an instruction so lower it to the ptest
10600 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010601 case Intrinsic::x86_sse41_ptestz:
10602 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010603 case Intrinsic::x86_sse41_ptestnzc:
10604 case Intrinsic::x86_avx_ptestz_256:
10605 case Intrinsic::x86_avx_ptestc_256:
10606 case Intrinsic::x86_avx_ptestnzc_256:
10607 case Intrinsic::x86_avx_vtestz_ps:
10608 case Intrinsic::x86_avx_vtestc_ps:
10609 case Intrinsic::x86_avx_vtestnzc_ps:
10610 case Intrinsic::x86_avx_vtestz_pd:
10611 case Intrinsic::x86_avx_vtestc_pd:
10612 case Intrinsic::x86_avx_vtestnzc_pd:
10613 case Intrinsic::x86_avx_vtestz_ps_256:
10614 case Intrinsic::x86_avx_vtestc_ps_256:
10615 case Intrinsic::x86_avx_vtestnzc_ps_256:
10616 case Intrinsic::x86_avx_vtestz_pd_256:
10617 case Intrinsic::x86_avx_vtestc_pd_256:
10618 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10619 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010620 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010621 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010622 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010623 case Intrinsic::x86_avx_vtestz_ps:
10624 case Intrinsic::x86_avx_vtestz_pd:
10625 case Intrinsic::x86_avx_vtestz_ps_256:
10626 case Intrinsic::x86_avx_vtestz_pd_256:
10627 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010628 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010629 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010630 // ZF = 1
10631 X86CC = X86::COND_E;
10632 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010633 case Intrinsic::x86_avx_vtestc_ps:
10634 case Intrinsic::x86_avx_vtestc_pd:
10635 case Intrinsic::x86_avx_vtestc_ps_256:
10636 case Intrinsic::x86_avx_vtestc_pd_256:
10637 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010638 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010639 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010640 // CF = 1
10641 X86CC = X86::COND_B;
10642 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010643 case Intrinsic::x86_avx_vtestnzc_ps:
10644 case Intrinsic::x86_avx_vtestnzc_pd:
10645 case Intrinsic::x86_avx_vtestnzc_ps_256:
10646 case Intrinsic::x86_avx_vtestnzc_pd_256:
10647 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010648 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010649 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010650 // ZF and CF = 0
10651 X86CC = X86::COND_A;
10652 break;
10653 }
Eric Christopherfd179292009-08-27 18:07:15 +000010654
Eric Christopher71c67532009-07-29 00:28:05 +000010655 SDValue LHS = Op.getOperand(1);
10656 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010657 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10658 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010659 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10660 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10661 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010662 }
Evan Cheng5759f972008-05-04 09:15:50 +000010663
Craig Topper80e46362012-01-23 06:16:53 +000010664 // SSE/AVX shift intrinsics
10665 case Intrinsic::x86_sse2_psll_w:
10666 case Intrinsic::x86_sse2_psll_d:
10667 case Intrinsic::x86_sse2_psll_q:
10668 case Intrinsic::x86_avx2_psll_w:
10669 case Intrinsic::x86_avx2_psll_d:
10670 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010671 case Intrinsic::x86_sse2_psrl_w:
10672 case Intrinsic::x86_sse2_psrl_d:
10673 case Intrinsic::x86_sse2_psrl_q:
10674 case Intrinsic::x86_avx2_psrl_w:
10675 case Intrinsic::x86_avx2_psrl_d:
10676 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010677 case Intrinsic::x86_sse2_psra_w:
10678 case Intrinsic::x86_sse2_psra_d:
10679 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010680 case Intrinsic::x86_avx2_psra_d: {
10681 unsigned Opcode;
10682 switch (IntNo) {
10683 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10684 case Intrinsic::x86_sse2_psll_w:
10685 case Intrinsic::x86_sse2_psll_d:
10686 case Intrinsic::x86_sse2_psll_q:
10687 case Intrinsic::x86_avx2_psll_w:
10688 case Intrinsic::x86_avx2_psll_d:
10689 case Intrinsic::x86_avx2_psll_q:
10690 Opcode = X86ISD::VSHL;
10691 break;
10692 case Intrinsic::x86_sse2_psrl_w:
10693 case Intrinsic::x86_sse2_psrl_d:
10694 case Intrinsic::x86_sse2_psrl_q:
10695 case Intrinsic::x86_avx2_psrl_w:
10696 case Intrinsic::x86_avx2_psrl_d:
10697 case Intrinsic::x86_avx2_psrl_q:
10698 Opcode = X86ISD::VSRL;
10699 break;
10700 case Intrinsic::x86_sse2_psra_w:
10701 case Intrinsic::x86_sse2_psra_d:
10702 case Intrinsic::x86_avx2_psra_w:
10703 case Intrinsic::x86_avx2_psra_d:
10704 Opcode = X86ISD::VSRA;
10705 break;
10706 }
10707 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010708 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010709 }
10710
10711 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010712 case Intrinsic::x86_sse2_pslli_w:
10713 case Intrinsic::x86_sse2_pslli_d:
10714 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010715 case Intrinsic::x86_avx2_pslli_w:
10716 case Intrinsic::x86_avx2_pslli_d:
10717 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010718 case Intrinsic::x86_sse2_psrli_w:
10719 case Intrinsic::x86_sse2_psrli_d:
10720 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010721 case Intrinsic::x86_avx2_psrli_w:
10722 case Intrinsic::x86_avx2_psrli_d:
10723 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010724 case Intrinsic::x86_sse2_psrai_w:
10725 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010726 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010727 case Intrinsic::x86_avx2_psrai_d: {
10728 unsigned Opcode;
10729 switch (IntNo) {
10730 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10731 case Intrinsic::x86_sse2_pslli_w:
10732 case Intrinsic::x86_sse2_pslli_d:
10733 case Intrinsic::x86_sse2_pslli_q:
10734 case Intrinsic::x86_avx2_pslli_w:
10735 case Intrinsic::x86_avx2_pslli_d:
10736 case Intrinsic::x86_avx2_pslli_q:
10737 Opcode = X86ISD::VSHLI;
10738 break;
10739 case Intrinsic::x86_sse2_psrli_w:
10740 case Intrinsic::x86_sse2_psrli_d:
10741 case Intrinsic::x86_sse2_psrli_q:
10742 case Intrinsic::x86_avx2_psrli_w:
10743 case Intrinsic::x86_avx2_psrli_d:
10744 case Intrinsic::x86_avx2_psrli_q:
10745 Opcode = X86ISD::VSRLI;
10746 break;
10747 case Intrinsic::x86_sse2_psrai_w:
10748 case Intrinsic::x86_sse2_psrai_d:
10749 case Intrinsic::x86_avx2_psrai_w:
10750 case Intrinsic::x86_avx2_psrai_d:
10751 Opcode = X86ISD::VSRAI;
10752 break;
10753 }
10754 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010755 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010756 }
10757
Craig Topper4feb6472012-08-06 06:22:36 +000010758 case Intrinsic::x86_sse42_pcmpistria128:
10759 case Intrinsic::x86_sse42_pcmpestria128:
10760 case Intrinsic::x86_sse42_pcmpistric128:
10761 case Intrinsic::x86_sse42_pcmpestric128:
10762 case Intrinsic::x86_sse42_pcmpistrio128:
10763 case Intrinsic::x86_sse42_pcmpestrio128:
10764 case Intrinsic::x86_sse42_pcmpistris128:
10765 case Intrinsic::x86_sse42_pcmpestris128:
10766 case Intrinsic::x86_sse42_pcmpistriz128:
10767 case Intrinsic::x86_sse42_pcmpestriz128: {
10768 unsigned Opcode;
10769 unsigned X86CC;
10770 switch (IntNo) {
10771 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10772 case Intrinsic::x86_sse42_pcmpistria128:
10773 Opcode = X86ISD::PCMPISTRI;
10774 X86CC = X86::COND_A;
10775 break;
10776 case Intrinsic::x86_sse42_pcmpestria128:
10777 Opcode = X86ISD::PCMPESTRI;
10778 X86CC = X86::COND_A;
10779 break;
10780 case Intrinsic::x86_sse42_pcmpistric128:
10781 Opcode = X86ISD::PCMPISTRI;
10782 X86CC = X86::COND_B;
10783 break;
10784 case Intrinsic::x86_sse42_pcmpestric128:
10785 Opcode = X86ISD::PCMPESTRI;
10786 X86CC = X86::COND_B;
10787 break;
10788 case Intrinsic::x86_sse42_pcmpistrio128:
10789 Opcode = X86ISD::PCMPISTRI;
10790 X86CC = X86::COND_O;
10791 break;
10792 case Intrinsic::x86_sse42_pcmpestrio128:
10793 Opcode = X86ISD::PCMPESTRI;
10794 X86CC = X86::COND_O;
10795 break;
10796 case Intrinsic::x86_sse42_pcmpistris128:
10797 Opcode = X86ISD::PCMPISTRI;
10798 X86CC = X86::COND_S;
10799 break;
10800 case Intrinsic::x86_sse42_pcmpestris128:
10801 Opcode = X86ISD::PCMPESTRI;
10802 X86CC = X86::COND_S;
10803 break;
10804 case Intrinsic::x86_sse42_pcmpistriz128:
10805 Opcode = X86ISD::PCMPISTRI;
10806 X86CC = X86::COND_E;
10807 break;
10808 case Intrinsic::x86_sse42_pcmpestriz128:
10809 Opcode = X86ISD::PCMPESTRI;
10810 X86CC = X86::COND_E;
10811 break;
10812 }
10813 SmallVector<SDValue, 5> NewOps;
10814 NewOps.append(Op->op_begin()+1, Op->op_end());
10815 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10816 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10817 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10818 DAG.getConstant(X86CC, MVT::i8),
10819 SDValue(PCMP.getNode(), 1));
10820 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10821 }
Craig Topper6d688152012-08-14 07:43:25 +000010822
Craig Topper4feb6472012-08-06 06:22:36 +000010823 case Intrinsic::x86_sse42_pcmpistri128:
10824 case Intrinsic::x86_sse42_pcmpestri128: {
10825 unsigned Opcode;
10826 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10827 Opcode = X86ISD::PCMPISTRI;
10828 else
10829 Opcode = X86ISD::PCMPESTRI;
10830
10831 SmallVector<SDValue, 5> NewOps;
10832 NewOps.append(Op->op_begin()+1, Op->op_end());
10833 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10834 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10835 }
Craig Topper0e292372012-08-24 04:03:22 +000010836 case Intrinsic::x86_fma_vfmadd_ps:
10837 case Intrinsic::x86_fma_vfmadd_pd:
10838 case Intrinsic::x86_fma_vfmsub_ps:
10839 case Intrinsic::x86_fma_vfmsub_pd:
10840 case Intrinsic::x86_fma_vfnmadd_ps:
10841 case Intrinsic::x86_fma_vfnmadd_pd:
10842 case Intrinsic::x86_fma_vfnmsub_ps:
10843 case Intrinsic::x86_fma_vfnmsub_pd:
10844 case Intrinsic::x86_fma_vfmaddsub_ps:
10845 case Intrinsic::x86_fma_vfmaddsub_pd:
10846 case Intrinsic::x86_fma_vfmsubadd_ps:
10847 case Intrinsic::x86_fma_vfmsubadd_pd:
10848 case Intrinsic::x86_fma_vfmadd_ps_256:
10849 case Intrinsic::x86_fma_vfmadd_pd_256:
10850 case Intrinsic::x86_fma_vfmsub_ps_256:
10851 case Intrinsic::x86_fma_vfmsub_pd_256:
10852 case Intrinsic::x86_fma_vfnmadd_ps_256:
10853 case Intrinsic::x86_fma_vfnmadd_pd_256:
10854 case Intrinsic::x86_fma_vfnmsub_ps_256:
10855 case Intrinsic::x86_fma_vfnmsub_pd_256:
10856 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10857 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10858 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10859 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010860 unsigned Opc;
10861 switch (IntNo) {
10862 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10863 case Intrinsic::x86_fma_vfmadd_ps:
10864 case Intrinsic::x86_fma_vfmadd_pd:
10865 case Intrinsic::x86_fma_vfmadd_ps_256:
10866 case Intrinsic::x86_fma_vfmadd_pd_256:
10867 Opc = X86ISD::FMADD;
10868 break;
10869 case Intrinsic::x86_fma_vfmsub_ps:
10870 case Intrinsic::x86_fma_vfmsub_pd:
10871 case Intrinsic::x86_fma_vfmsub_ps_256:
10872 case Intrinsic::x86_fma_vfmsub_pd_256:
10873 Opc = X86ISD::FMSUB;
10874 break;
10875 case Intrinsic::x86_fma_vfnmadd_ps:
10876 case Intrinsic::x86_fma_vfnmadd_pd:
10877 case Intrinsic::x86_fma_vfnmadd_ps_256:
10878 case Intrinsic::x86_fma_vfnmadd_pd_256:
10879 Opc = X86ISD::FNMADD;
10880 break;
10881 case Intrinsic::x86_fma_vfnmsub_ps:
10882 case Intrinsic::x86_fma_vfnmsub_pd:
10883 case Intrinsic::x86_fma_vfnmsub_ps_256:
10884 case Intrinsic::x86_fma_vfnmsub_pd_256:
10885 Opc = X86ISD::FNMSUB;
10886 break;
10887 case Intrinsic::x86_fma_vfmaddsub_ps:
10888 case Intrinsic::x86_fma_vfmaddsub_pd:
10889 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10890 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10891 Opc = X86ISD::FMADDSUB;
10892 break;
10893 case Intrinsic::x86_fma_vfmsubadd_ps:
10894 case Intrinsic::x86_fma_vfmsubadd_pd:
10895 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10896 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10897 Opc = X86ISD::FMSUBADD;
10898 break;
10899 }
10900
10901 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10902 Op.getOperand(2), Op.getOperand(3));
10903 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010904 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010905}
Evan Cheng72261582005-12-20 06:22:03 +000010906
Craig Topper55b24052012-09-11 06:15:32 +000010907static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010908 DebugLoc dl = Op.getDebugLoc();
10909 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10910 switch (IntNo) {
10911 default: return SDValue(); // Don't custom lower most intrinsics.
10912
10913 // RDRAND intrinsics.
10914 case Intrinsic::x86_rdrand_16:
10915 case Intrinsic::x86_rdrand_32:
10916 case Intrinsic::x86_rdrand_64: {
10917 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010918 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10919 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010920
10921 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10922 // return the value from Rand, which is always 0, casted to i32.
10923 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10924 DAG.getConstant(1, Op->getValueType(1)),
10925 DAG.getConstant(X86::COND_B, MVT::i32),
10926 SDValue(Result.getNode(), 1) };
10927 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10928 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10929 Ops, 4);
10930
10931 // Return { result, isValid, chain }.
10932 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010933 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010934 }
10935 }
10936}
10937
Dan Gohmand858e902010-04-17 15:26:15 +000010938SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10939 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010940 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10941 MFI->setReturnAddressIsTaken(true);
10942
Bill Wendling64e87322009-01-16 19:25:27 +000010943 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010944 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010945 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010946
10947 if (Depth > 0) {
10948 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10949 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010950 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10951 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10952 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010953 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010954 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010955 }
10956
10957 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010958 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010959 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010960 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010961}
10962
Dan Gohmand858e902010-04-17 15:26:15 +000010963SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010964 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10965 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010966
Owen Andersone50ed302009-08-10 22:56:29 +000010967 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010968 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010969 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10970 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010971 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010972 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010973 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10974 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010975 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010976 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010977}
10978
Dan Gohman475871a2008-07-27 21:46:04 +000010979SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010980 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010981 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010982}
10983
Dan Gohmand858e902010-04-17 15:26:15 +000010984SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010985 SDValue Chain = Op.getOperand(0);
10986 SDValue Offset = Op.getOperand(1);
10987 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010988 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010989
Dan Gohmand8816272010-08-11 18:14:00 +000010990 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10991 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10992 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010993 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010994
Dan Gohmand8816272010-08-11 18:14:00 +000010995 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010996 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010997 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010998 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10999 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011000 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011001
Dale Johannesene4d209d2009-02-03 20:21:25 +000011002 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011003 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011004 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011005}
11006
Michael Liao6c0e04c2012-10-15 22:39:43 +000011007SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11008 SelectionDAG &DAG) const {
11009 DebugLoc DL = Op.getDebugLoc();
11010 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11011 DAG.getVTList(MVT::i32, MVT::Other),
11012 Op.getOperand(0), Op.getOperand(1));
11013}
11014
11015SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11016 SelectionDAG &DAG) const {
11017 DebugLoc DL = Op.getDebugLoc();
11018 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11019 Op.getOperand(0), Op.getOperand(1));
11020}
11021
Craig Topper55b24052012-09-11 06:15:32 +000011022static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011023 return Op.getOperand(0);
11024}
11025
11026SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11027 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011028 SDValue Root = Op.getOperand(0);
11029 SDValue Trmp = Op.getOperand(1); // trampoline
11030 SDValue FPtr = Op.getOperand(2); // nested function
11031 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011032 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011033
Dan Gohman69de1932008-02-06 22:27:42 +000011034 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011035 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011036
11037 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011038 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011039
11040 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011041 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11042 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011043
Michael Liao7abf67a2012-10-04 19:50:43 +000011044 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11045 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011046
11047 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11048
11049 // Load the pointer to the nested function into R11.
11050 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011051 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011052 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011053 Addr, MachinePointerInfo(TrmpAddr),
11054 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011055
Owen Anderson825b72b2009-08-11 20:47:22 +000011056 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11057 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011058 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11059 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011060 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011061
11062 // Load the 'nest' parameter value into R10.
11063 // R10 is specified in X86CallingConv.td
11064 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011065 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11066 DAG.getConstant(10, MVT::i64));
11067 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011068 Addr, MachinePointerInfo(TrmpAddr, 10),
11069 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011070
Owen Anderson825b72b2009-08-11 20:47:22 +000011071 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11072 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011073 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11074 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011075 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011076
11077 // Jump to the nested function.
11078 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011079 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11080 DAG.getConstant(20, MVT::i64));
11081 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011082 Addr, MachinePointerInfo(TrmpAddr, 20),
11083 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011084
11085 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011086 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11087 DAG.getConstant(22, MVT::i64));
11088 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011089 MachinePointerInfo(TrmpAddr, 22),
11090 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011091
Duncan Sands4a544a72011-09-06 13:37:06 +000011092 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011093 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011094 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011095 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011096 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011097 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011098
11099 switch (CC) {
11100 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011101 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011102 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011103 case CallingConv::X86_StdCall: {
11104 // Pass 'nest' parameter in ECX.
11105 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011106 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011107
11108 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011109 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011110 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011111
Chris Lattner58d74912008-03-12 17:45:29 +000011112 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011113 unsigned InRegCount = 0;
11114 unsigned Idx = 1;
11115
11116 for (FunctionType::param_iterator I = FTy->param_begin(),
11117 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011118 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011119 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011120 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011121
11122 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011123 report_fatal_error("Nest register in use - reduce number of inreg"
11124 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011125 }
11126 }
11127 break;
11128 }
11129 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011130 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011131 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011132 // Pass 'nest' parameter in EAX.
11133 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011134 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011135 break;
11136 }
11137
Dan Gohman475871a2008-07-27 21:46:04 +000011138 SDValue OutChains[4];
11139 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011140
Owen Anderson825b72b2009-08-11 20:47:22 +000011141 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11142 DAG.getConstant(10, MVT::i32));
11143 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011144
Chris Lattnera62fe662010-02-05 19:20:30 +000011145 // This is storing the opcode for MOV32ri.
11146 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011147 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011148 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011149 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011150 Trmp, MachinePointerInfo(TrmpAddr),
11151 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011152
Owen Anderson825b72b2009-08-11 20:47:22 +000011153 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11154 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011155 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11156 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011157 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011158
Chris Lattnera62fe662010-02-05 19:20:30 +000011159 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011160 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11161 DAG.getConstant(5, MVT::i32));
11162 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011163 MachinePointerInfo(TrmpAddr, 5),
11164 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011165
Owen Anderson825b72b2009-08-11 20:47:22 +000011166 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11167 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011168 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11169 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011170 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011171
Duncan Sands4a544a72011-09-06 13:37:06 +000011172 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011173 }
11174}
11175
Dan Gohmand858e902010-04-17 15:26:15 +000011176SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11177 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011178 /*
11179 The rounding mode is in bits 11:10 of FPSR, and has the following
11180 settings:
11181 00 Round to nearest
11182 01 Round to -inf
11183 10 Round to +inf
11184 11 Round to 0
11185
11186 FLT_ROUNDS, on the other hand, expects the following:
11187 -1 Undefined
11188 0 Round to 0
11189 1 Round to nearest
11190 2 Round to +inf
11191 3 Round to -inf
11192
11193 To perform the conversion, we do:
11194 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11195 */
11196
11197 MachineFunction &MF = DAG.getMachineFunction();
11198 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011199 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011200 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011201 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011202 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011203
11204 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011205 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011206 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011207
Chris Lattner2156b792010-09-22 01:11:26 +000011208 MachineMemOperand *MMO =
11209 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11210 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011211
Chris Lattner2156b792010-09-22 01:11:26 +000011212 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11213 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11214 DAG.getVTList(MVT::Other),
11215 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011216
11217 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011218 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011219 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011220
11221 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011222 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011223 DAG.getNode(ISD::SRL, DL, MVT::i16,
11224 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011225 CWD, DAG.getConstant(0x800, MVT::i16)),
11226 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011227 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011228 DAG.getNode(ISD::SRL, DL, MVT::i16,
11229 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 CWD, DAG.getConstant(0x400, MVT::i16)),
11231 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011232
Dan Gohman475871a2008-07-27 21:46:04 +000011233 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011234 DAG.getNode(ISD::AND, DL, MVT::i16,
11235 DAG.getNode(ISD::ADD, DL, MVT::i16,
11236 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011237 DAG.getConstant(1, MVT::i16)),
11238 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011239
Duncan Sands83ec4b62008-06-06 12:08:01 +000011240 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011241 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011242}
11243
Craig Topper55b24052012-09-11 06:15:32 +000011244static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011245 EVT VT = Op.getValueType();
11246 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011247 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011248 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011249
11250 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011251 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011252 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011253 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011254 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011255 }
Evan Cheng18efe262007-12-14 02:13:44 +000011256
Evan Cheng152804e2007-12-14 08:30:15 +000011257 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011258 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011259 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011260
11261 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011262 SDValue Ops[] = {
11263 Op,
11264 DAG.getConstant(NumBits+NumBits-1, OpVT),
11265 DAG.getConstant(X86::COND_E, MVT::i8),
11266 Op.getValue(1)
11267 };
11268 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011269
11270 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011271 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011272
Owen Anderson825b72b2009-08-11 20:47:22 +000011273 if (VT == MVT::i8)
11274 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011275 return Op;
11276}
11277
Craig Topper55b24052012-09-11 06:15:32 +000011278static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011279 EVT VT = Op.getValueType();
11280 EVT OpVT = VT;
11281 unsigned NumBits = VT.getSizeInBits();
11282 DebugLoc dl = Op.getDebugLoc();
11283
11284 Op = Op.getOperand(0);
11285 if (VT == MVT::i8) {
11286 // Zero extend to i32 since there is not an i8 bsr.
11287 OpVT = MVT::i32;
11288 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11289 }
11290
11291 // Issue a bsr (scan bits in reverse).
11292 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11293 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11294
11295 // And xor with NumBits-1.
11296 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11297
11298 if (VT == MVT::i8)
11299 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11300 return Op;
11301}
11302
Craig Topper55b24052012-09-11 06:15:32 +000011303static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011304 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011305 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011306 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011307 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011308
11309 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011310 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011311 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011312
11313 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011314 SDValue Ops[] = {
11315 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011316 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011317 DAG.getConstant(X86::COND_E, MVT::i8),
11318 Op.getValue(1)
11319 };
Chandler Carruth77821022011-12-24 12:12:34 +000011320 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011321}
11322
Craig Topper13894fa2011-08-24 06:14:18 +000011323// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11324// ones, and then concatenate the result back.
11325static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011326 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011327
Craig Topper7a9a28b2012-08-12 02:23:29 +000011328 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011329 "Unsupported value type for operation");
11330
Craig Topper66ddd152012-04-27 22:54:43 +000011331 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011332 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011333
11334 // Extract the LHS vectors
11335 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011336 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11337 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011338
11339 // Extract the RHS vectors
11340 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011341 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11342 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011343
11344 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11345 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11346
11347 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11348 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11349 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11350}
11351
Craig Topper55b24052012-09-11 06:15:32 +000011352static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011353 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011354 Op.getValueType().isInteger() &&
11355 "Only handle AVX 256-bit vector integer operation");
11356 return Lower256IntArith(Op, DAG);
11357}
11358
Craig Topper55b24052012-09-11 06:15:32 +000011359static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011360 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011361 Op.getValueType().isInteger() &&
11362 "Only handle AVX 256-bit vector integer operation");
11363 return Lower256IntArith(Op, DAG);
11364}
11365
Craig Topper55b24052012-09-11 06:15:32 +000011366static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11367 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011368 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011369 EVT VT = Op.getValueType();
11370
11371 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011372 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011373 return Lower256IntArith(Op, DAG);
11374
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011375 SDValue A = Op.getOperand(0);
11376 SDValue B = Op.getOperand(1);
11377
11378 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11379 if (VT == MVT::v4i32) {
11380 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11381 "Should not custom lower when pmuldq is available!");
11382
11383 // Extract the odd parts.
11384 const int UnpackMask[] = { 1, -1, 3, -1 };
11385 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11386 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11387
11388 // Multiply the even parts.
11389 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11390 // Now multiply odd parts.
11391 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11392
11393 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11394 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11395
11396 // Merge the two vectors back together with a shuffle. This expands into 2
11397 // shuffles.
11398 const int ShufMask[] = { 0, 4, 2, 6 };
11399 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11400 }
11401
Craig Topper5b209e82012-02-05 03:14:49 +000011402 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11403 "Only know how to lower V2I64/V4I64 multiply");
11404
Craig Topper5b209e82012-02-05 03:14:49 +000011405 // Ahi = psrlqi(a, 32);
11406 // Bhi = psrlqi(b, 32);
11407 //
11408 // AloBlo = pmuludq(a, b);
11409 // AloBhi = pmuludq(a, Bhi);
11410 // AhiBlo = pmuludq(Ahi, b);
11411
11412 // AloBhi = psllqi(AloBhi, 32);
11413 // AhiBlo = psllqi(AhiBlo, 32);
11414 // return AloBlo + AloBhi + AhiBlo;
11415
Craig Topper5b209e82012-02-05 03:14:49 +000011416 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011417
Craig Topper5b209e82012-02-05 03:14:49 +000011418 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11419 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011420
Craig Topper5b209e82012-02-05 03:14:49 +000011421 // Bit cast to 32-bit vectors for MULUDQ
11422 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11423 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11424 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11425 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11426 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011427
Craig Topper5b209e82012-02-05 03:14:49 +000011428 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11429 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11430 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011431
Craig Topper5b209e82012-02-05 03:14:49 +000011432 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11433 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011434
Dale Johannesene4d209d2009-02-03 20:21:25 +000011435 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011436 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011437}
11438
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011439SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11440 EVT VT = Op.getValueType();
11441 EVT EltTy = VT.getVectorElementType();
11442 unsigned NumElts = VT.getVectorNumElements();
11443 SDValue N0 = Op.getOperand(0);
11444 DebugLoc dl = Op.getDebugLoc();
11445
11446 // Lower sdiv X, pow2-const.
11447 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11448 if (!C)
11449 return SDValue();
11450
11451 APInt SplatValue, SplatUndef;
11452 unsigned MinSplatBits;
11453 bool HasAnyUndefs;
11454 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11455 return SDValue();
11456
11457 if ((SplatValue != 0) &&
11458 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11459 unsigned lg2 = SplatValue.countTrailingZeros();
11460 // Splat the sign bit.
11461 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11462 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11463 // Add (N0 < 0) ? abs2 - 1 : 0;
11464 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11465 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11466 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11467 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11468 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11469
11470 // If we're dividing by a positive value, we're done. Otherwise, we must
11471 // negate the result.
11472 if (SplatValue.isNonNegative())
11473 return SRA;
11474
11475 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11476 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11477 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11478 }
11479 return SDValue();
11480}
11481
Nadav Rotem43012222011-05-11 08:12:09 +000011482SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11483
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011484 EVT VT = Op.getValueType();
11485 DebugLoc dl = Op.getDebugLoc();
11486 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011487 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011488
Craig Topper1accb7e2012-01-10 06:54:16 +000011489 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011490 return SDValue();
11491
Nadav Rotem43012222011-05-11 08:12:09 +000011492 // Optimize shl/srl/sra with constant shift amount.
11493 if (isSplatVector(Amt.getNode())) {
11494 SDValue SclrAmt = Amt->getOperand(0);
11495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11496 uint64_t ShiftAmt = C->getZExtValue();
11497
Craig Toppered2e13d2012-01-22 19:15:14 +000011498 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011499 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011500 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11501 if (Op.getOpcode() == ISD::SHL)
11502 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11503 DAG.getConstant(ShiftAmt, MVT::i32));
11504 if (Op.getOpcode() == ISD::SRL)
11505 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11506 DAG.getConstant(ShiftAmt, MVT::i32));
11507 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11508 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11509 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011510 }
11511
Craig Toppered2e13d2012-01-22 19:15:14 +000011512 if (VT == MVT::v16i8) {
11513 if (Op.getOpcode() == ISD::SHL) {
11514 // Make a large shift.
11515 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11516 DAG.getConstant(ShiftAmt, MVT::i32));
11517 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11518 // Zero out the rightmost bits.
11519 SmallVector<SDValue, 16> V(16,
11520 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11521 MVT::i8));
11522 return DAG.getNode(ISD::AND, dl, VT, SHL,
11523 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011524 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011525 if (Op.getOpcode() == ISD::SRL) {
11526 // Make a large shift.
11527 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11528 DAG.getConstant(ShiftAmt, MVT::i32));
11529 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11530 // Zero out the leftmost bits.
11531 SmallVector<SDValue, 16> V(16,
11532 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11533 MVT::i8));
11534 return DAG.getNode(ISD::AND, dl, VT, SRL,
11535 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11536 }
11537 if (Op.getOpcode() == ISD::SRA) {
11538 if (ShiftAmt == 7) {
11539 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011540 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011541 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011542 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011543
Craig Toppered2e13d2012-01-22 19:15:14 +000011544 // R s>> a === ((R u>> a) ^ m) - m
11545 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11546 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11547 MVT::i8));
11548 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11549 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11550 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11551 return Res;
11552 }
Craig Topper731dfd02012-04-23 03:42:40 +000011553 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011554 }
Craig Topper46154eb2011-11-11 07:39:23 +000011555
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011556 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011557 if (Op.getOpcode() == ISD::SHL) {
11558 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011559 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11560 DAG.getConstant(ShiftAmt, MVT::i32));
11561 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011562 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011563 SmallVector<SDValue, 32> V(32,
11564 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11565 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011566 return DAG.getNode(ISD::AND, dl, VT, SHL,
11567 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011568 }
Craig Topper0d86d462011-11-20 00:12:05 +000011569 if (Op.getOpcode() == ISD::SRL) {
11570 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011571 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11572 DAG.getConstant(ShiftAmt, MVT::i32));
11573 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011574 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011575 SmallVector<SDValue, 32> V(32,
11576 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11577 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011578 return DAG.getNode(ISD::AND, dl, VT, SRL,
11579 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11580 }
11581 if (Op.getOpcode() == ISD::SRA) {
11582 if (ShiftAmt == 7) {
11583 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011584 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011585 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011586 }
11587
11588 // R s>> a === ((R u>> a) ^ m) - m
11589 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11590 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11591 MVT::i8));
11592 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11593 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11594 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11595 return Res;
11596 }
Craig Topper731dfd02012-04-23 03:42:40 +000011597 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011598 }
Nadav Rotem43012222011-05-11 08:12:09 +000011599 }
11600 }
11601
11602 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011603 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011604 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011605
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011606 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011607 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011608 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11609 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11610 }
Nadav Rotem43012222011-05-11 08:12:09 +000011611 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011612 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011613
Nate Begeman51409212010-07-28 00:21:48 +000011614 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011615 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011616 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011617
Lang Hames8b99c1e2011-12-17 01:08:46 +000011618 // Turn 'a' into a mask suitable for VSELECT
11619 SDValue VSelM = DAG.getConstant(0x80, VT);
11620 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011621 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011622
Lang Hames8b99c1e2011-12-17 01:08:46 +000011623 SDValue CM1 = DAG.getConstant(0x0f, VT);
11624 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011625
Lang Hames8b99c1e2011-12-17 01:08:46 +000011626 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11627 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011628 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11629 DAG.getConstant(4, MVT::i32), DAG);
11630 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011631 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11632
Nate Begeman51409212010-07-28 00:21:48 +000011633 // a += a
11634 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011635 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011636 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011637
Lang Hames8b99c1e2011-12-17 01:08:46 +000011638 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11639 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011640 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11641 DAG.getConstant(2, MVT::i32), DAG);
11642 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011643 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11644
Nate Begeman51409212010-07-28 00:21:48 +000011645 // a += a
11646 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011647 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011648 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011649
Lang Hames8b99c1e2011-12-17 01:08:46 +000011650 // return VSELECT(r, r+r, a);
11651 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011652 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011653 return R;
11654 }
Craig Topper46154eb2011-11-11 07:39:23 +000011655
11656 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011657 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011658 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011659 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11660 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11661
11662 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011663 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11664 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011665
11666 // Recreate the shift amount vectors
11667 SDValue Amt1, Amt2;
11668 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11669 // Constant shift amount
11670 SmallVector<SDValue, 4> Amt1Csts;
11671 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011672 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011673 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011674 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011675 Amt2Csts.push_back(Amt->getOperand(i));
11676
11677 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11678 &Amt1Csts[0], NumElems/2);
11679 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11680 &Amt2Csts[0], NumElems/2);
11681 } else {
11682 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011683 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11684 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011685 }
11686
11687 // Issue new vector shifts for the smaller types
11688 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11689 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11690
11691 // Concatenate the result back
11692 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11693 }
11694
Nate Begeman51409212010-07-28 00:21:48 +000011695 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011696}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011697
Craig Topper55b24052012-09-11 06:15:32 +000011698static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011699 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11700 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011701 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11702 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011703 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011704 SDValue LHS = N->getOperand(0);
11705 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011706 unsigned BaseOp = 0;
11707 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011708 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011709 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011710 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011711 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011712 // A subtract of one will be selected as a INC. Note that INC doesn't
11713 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011714 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11715 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011716 BaseOp = X86ISD::INC;
11717 Cond = X86::COND_O;
11718 break;
11719 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011720 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011721 Cond = X86::COND_O;
11722 break;
11723 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011724 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011725 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011726 break;
11727 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011728 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11729 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11731 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011732 BaseOp = X86ISD::DEC;
11733 Cond = X86::COND_O;
11734 break;
11735 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011736 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011737 Cond = X86::COND_O;
11738 break;
11739 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011740 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011741 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011742 break;
11743 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011744 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011745 Cond = X86::COND_O;
11746 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011747 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11748 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11749 MVT::i32);
11750 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011751
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011752 SDValue SetCC =
11753 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11754 DAG.getConstant(X86::COND_O, MVT::i32),
11755 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011756
Dan Gohman6e5fda22011-07-22 18:45:15 +000011757 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011758 }
Bill Wendling74c37652008-12-09 22:08:41 +000011759 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011760
Bill Wendling61edeb52008-12-02 01:06:39 +000011761 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011763 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011764
Bill Wendling61edeb52008-12-02 01:06:39 +000011765 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011766 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11767 DAG.getConstant(Cond, MVT::i32),
11768 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011769
Dan Gohman6e5fda22011-07-22 18:45:15 +000011770 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011771}
11772
Chad Rosier30450e82011-12-22 22:35:21 +000011773SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11774 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011775 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011776 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11777 EVT VT = Op.getValueType();
11778
Craig Toppered2e13d2012-01-22 19:15:14 +000011779 if (!Subtarget->hasSSE2() || !VT.isVector())
11780 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011781
Craig Toppered2e13d2012-01-22 19:15:14 +000011782 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11783 ExtraVT.getScalarType().getSizeInBits();
11784 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11785
11786 switch (VT.getSimpleVT().SimpleTy) {
11787 default: return SDValue();
11788 case MVT::v8i32:
11789 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011790 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011791 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011792 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011793 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011794 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011795
Craig Toppered2e13d2012-01-22 19:15:14 +000011796 // Extract the LHS vectors
11797 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011798 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11799 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011800
Craig Toppered2e13d2012-01-22 19:15:14 +000011801 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11802 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011803
Craig Toppered2e13d2012-01-22 19:15:14 +000011804 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011805 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011806 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11807 ExtraNumElems/2);
11808 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011809
Craig Toppered2e13d2012-01-22 19:15:14 +000011810 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11811 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011812
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011813 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011814 }
11815 // fall through
11816 case MVT::v4i32:
11817 case MVT::v8i16: {
11818 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11819 Op.getOperand(0), ShAmt, DAG);
11820 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011821 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011822 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011823}
11824
Craig Topper55b24052012-09-11 06:15:32 +000011825static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11826 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011827 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011828
Eric Christopher77ed1352011-07-08 00:04:56 +000011829 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11830 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011831 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011832 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011833 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011834 SDValue Ops[] = {
11835 DAG.getRegister(X86::ESP, MVT::i32), // Base
11836 DAG.getTargetConstant(1, MVT::i8), // Scale
11837 DAG.getRegister(0, MVT::i32), // Index
11838 DAG.getTargetConstant(0, MVT::i32), // Disp
11839 DAG.getRegister(0, MVT::i32), // Segment.
11840 Zero,
11841 Chain
11842 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011843 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011844 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11845 array_lengthof(Ops));
11846 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011847 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011848
Eric Christopher9a9d2752010-07-22 02:48:34 +000011849 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011850 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011851 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011852
Chris Lattner132929a2010-08-14 17:26:09 +000011853 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11854 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11855 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11856 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011857
Chris Lattner132929a2010-08-14 17:26:09 +000011858 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11859 if (!Op1 && !Op2 && !Op3 && Op4)
11860 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011861
Chris Lattner132929a2010-08-14 17:26:09 +000011862 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11863 if (Op1 && !Op2 && !Op3 && !Op4)
11864 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011865
11866 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011867 // (MFENCE)>;
11868 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011869}
11870
Craig Topper55b24052012-09-11 06:15:32 +000011871static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11872 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011873 DebugLoc dl = Op.getDebugLoc();
11874 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11875 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11876 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11877 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11878
11879 // The only fence that needs an instruction is a sequentially-consistent
11880 // cross-thread fence.
11881 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11882 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11883 // no-sse2). There isn't any reason to disable it if the target processor
11884 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011885 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011886 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11887
11888 SDValue Chain = Op.getOperand(0);
11889 SDValue Zero = DAG.getConstant(0, MVT::i32);
11890 SDValue Ops[] = {
11891 DAG.getRegister(X86::ESP, MVT::i32), // Base
11892 DAG.getTargetConstant(1, MVT::i8), // Scale
11893 DAG.getRegister(0, MVT::i32), // Index
11894 DAG.getTargetConstant(0, MVT::i32), // Disp
11895 DAG.getRegister(0, MVT::i32), // Segment.
11896 Zero,
11897 Chain
11898 };
11899 SDNode *Res =
11900 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11901 array_lengthof(Ops));
11902 return SDValue(Res, 0);
11903 }
11904
11905 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11906 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11907}
11908
Craig Topper55b24052012-09-11 06:15:32 +000011909static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11910 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011911 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011912 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011913 unsigned Reg = 0;
11914 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011915 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011916 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011917 case MVT::i8: Reg = X86::AL; size = 1; break;
11918 case MVT::i16: Reg = X86::AX; size = 2; break;
11919 case MVT::i32: Reg = X86::EAX; size = 4; break;
11920 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011921 assert(Subtarget->is64Bit() && "Node not type legal!");
11922 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011923 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011924 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011925 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011926 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011927 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011928 Op.getOperand(1),
11929 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011930 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011931 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011932 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011933 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11934 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11935 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011936 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011937 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011938 return cpOut;
11939}
11940
Craig Topper55b24052012-09-11 06:15:32 +000011941static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11942 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011943 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011944 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011945 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011946 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011947 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011948 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11949 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011950 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011951 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11952 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011953 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011954 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011955 rdx.getValue(1)
11956 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011957 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011958}
11959
Craig Topper55b24052012-09-11 06:15:32 +000011960SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011961 EVT SrcVT = Op.getOperand(0).getValueType();
11962 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011963 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011964 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011965 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011966 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011967 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011968 // i64 <=> MMX conversions are Legal.
11969 if (SrcVT==MVT::i64 && DstVT.isVector())
11970 return Op;
11971 if (DstVT==MVT::i64 && SrcVT.isVector())
11972 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011973 // MMX <=> MMX conversions are Legal.
11974 if (SrcVT.isVector() && DstVT.isVector())
11975 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011976 // All other conversions need to be expanded.
11977 return SDValue();
11978}
Chris Lattner5b856542010-12-20 00:59:46 +000011979
Craig Topper55b24052012-09-11 06:15:32 +000011980static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011981 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011982 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011983 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011984 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011985 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011986 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011987 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011988 Node->getOperand(0),
11989 Node->getOperand(1), negOp,
11990 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011991 cast<AtomicSDNode>(Node)->getAlignment(),
11992 cast<AtomicSDNode>(Node)->getOrdering(),
11993 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011994}
11995
Eli Friedman327236c2011-08-24 20:50:09 +000011996static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11997 SDNode *Node = Op.getNode();
11998 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011999 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012000
12001 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012002 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12003 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12004 // (The only way to get a 16-byte store is cmpxchg16b)
12005 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12006 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12007 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012008 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12009 cast<AtomicSDNode>(Node)->getMemoryVT(),
12010 Node->getOperand(0),
12011 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012012 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012013 cast<AtomicSDNode>(Node)->getOrdering(),
12014 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012015 return Swap.getValue(1);
12016 }
12017 // Other atomic stores have a simple pattern.
12018 return Op;
12019}
12020
Chris Lattner5b856542010-12-20 00:59:46 +000012021static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12022 EVT VT = Op.getNode()->getValueType(0);
12023
12024 // Let legalize expand this if it isn't a legal type yet.
12025 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12026 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012027
Chris Lattner5b856542010-12-20 00:59:46 +000012028 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012029
Chris Lattner5b856542010-12-20 00:59:46 +000012030 unsigned Opc;
12031 bool ExtraOp = false;
12032 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012033 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012034 case ISD::ADDC: Opc = X86ISD::ADD; break;
12035 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12036 case ISD::SUBC: Opc = X86ISD::SUB; break;
12037 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12038 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012039
Chris Lattner5b856542010-12-20 00:59:46 +000012040 if (!ExtraOp)
12041 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12042 Op.getOperand(1));
12043 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12044 Op.getOperand(1), Op.getOperand(2));
12045}
12046
Evan Cheng8688a582013-01-29 02:32:37 +000012047SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012048 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012049
Evan Cheng8688a582013-01-29 02:32:37 +000012050 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12051 // which returns the values in two XMM registers.
12052 DebugLoc dl = Op.getDebugLoc();
12053 SDValue Arg = Op.getOperand(0);
12054 EVT ArgVT = Arg.getValueType();
12055 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012056
Evan Cheng8688a582013-01-29 02:32:37 +000012057 ArgListTy Args;
12058 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012059
Evan Cheng8688a582013-01-29 02:32:37 +000012060 Entry.Node = Arg;
12061 Entry.Ty = ArgTy;
12062 Entry.isSExt = false;
12063 Entry.isZExt = false;
12064 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012065
12066 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12067 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12068 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012069 const char *LibcallName = (ArgVT == MVT::f64)
12070 ? "__sincos_stret" : "__sincosf_stret";
12071 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012072
Evan Cheng8688a582013-01-29 02:32:37 +000012073 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12074 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012075 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12076 false, false, false, false, 0,
12077 CallingConv::C, /*isTaillCall=*/false,
12078 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12079 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012080 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012081 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012082}
12083
Evan Cheng0db9fe62006-04-25 20:13:52 +000012084/// LowerOperation - Provide custom lowering hooks for some operations.
12085///
Dan Gohmand858e902010-04-17 15:26:15 +000012086SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012087 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012088 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012089 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012090 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12091 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12092 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012093 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012094 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012095 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012096 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012097 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12098 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12099 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012100 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12101 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012102 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12103 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12104 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012105 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012106 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012107 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012108 case ISD::SHL_PARTS:
12109 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012110 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012111 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012112 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012113 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012114 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12115 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12116 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012117 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012118 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012119 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012120 case ISD::FABS: return LowerFABS(Op, DAG);
12121 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012122 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012123 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012124 case ISD::SETCC: return LowerSETCC(Op, DAG);
12125 case ISD::SELECT: return LowerSELECT(Op, DAG);
12126 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012127 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012128 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012129 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012130 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012131 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012132 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012133 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12134 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012135 case ISD::FRAME_TO_ARGS_OFFSET:
12136 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012137 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012138 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012139 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12140 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012141 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12142 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012143 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012144 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012145 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012146 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012147 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012148 case ISD::SRA:
12149 case ISD::SRL:
12150 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012151 case ISD::SADDO:
12152 case ISD::UADDO:
12153 case ISD::SSUBO:
12154 case ISD::USUBO:
12155 case ISD::SMULO:
12156 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012157 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012158 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012159 case ISD::ADDC:
12160 case ISD::ADDE:
12161 case ISD::SUBC:
12162 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012163 case ISD::ADD: return LowerADD(Op, DAG);
12164 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012165 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012166 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012167 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012168}
12169
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012170static void ReplaceATOMIC_LOAD(SDNode *Node,
12171 SmallVectorImpl<SDValue> &Results,
12172 SelectionDAG &DAG) {
12173 DebugLoc dl = Node->getDebugLoc();
12174 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12175
12176 // Convert wide load -> cmpxchg8b/cmpxchg16b
12177 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12178 // (The only way to get a 16-byte load is cmpxchg16b)
12179 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012180 SDValue Zero = DAG.getConstant(0, VT);
12181 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012182 Node->getOperand(0),
12183 Node->getOperand(1), Zero, Zero,
12184 cast<AtomicSDNode>(Node)->getMemOperand(),
12185 cast<AtomicSDNode>(Node)->getOrdering(),
12186 cast<AtomicSDNode>(Node)->getSynchScope());
12187 Results.push_back(Swap.getValue(0));
12188 Results.push_back(Swap.getValue(1));
12189}
12190
Craig Topperc0878702012-08-17 06:55:11 +000012191static void
Duncan Sands1607f052008-12-01 11:39:25 +000012192ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012193 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012194 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012195 assert (Node->getValueType(0) == MVT::i64 &&
12196 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012197
12198 SDValue Chain = Node->getOperand(0);
12199 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012200 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012201 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012202 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012203 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012204 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012205 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012206 SDValue Result =
12207 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12208 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012209 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012210 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012211 Results.push_back(Result.getValue(2));
12212}
12213
Duncan Sands126d9072008-07-04 11:47:58 +000012214/// ReplaceNodeResults - Replace a node with an illegal result type
12215/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012216void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12217 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012218 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012219 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012220 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012221 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012222 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012223 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012224 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012225 case ISD::ADDC:
12226 case ISD::ADDE:
12227 case ISD::SUBC:
12228 case ISD::SUBE:
12229 // We don't want to expand or promote these.
12230 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012231 case ISD::FP_TO_SINT:
12232 case ISD::FP_TO_UINT: {
12233 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12234
12235 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12236 return;
12237
Eli Friedman948e95a2009-05-23 09:59:16 +000012238 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012239 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012240 SDValue FIST = Vals.first, StackSlot = Vals.second;
12241 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012242 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012243 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012244 if (StackSlot.getNode() != 0)
12245 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12246 MachinePointerInfo(),
12247 false, false, false, 0));
12248 else
12249 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012250 }
12251 return;
12252 }
Michael Liao991b6a22012-10-24 04:09:32 +000012253 case ISD::UINT_TO_FP: {
12254 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12255 N->getValueType(0) != MVT::v2f32)
12256 return;
12257 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12258 N->getOperand(0));
12259 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12260 MVT::f64);
12261 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12262 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12263 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12264 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12265 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12266 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12267 return;
12268 }
Michael Liao44c2d612012-10-10 16:53:28 +000012269 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012270 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12271 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012272 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12273 Results.push_back(V);
12274 return;
12275 }
Duncan Sands1607f052008-12-01 11:39:25 +000012276 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012277 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012278 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012279 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012280 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012281 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012282 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012283 eax.getValue(2));
12284 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12285 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012286 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012287 Results.push_back(edx.getValue(1));
12288 return;
12289 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012290 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012291 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012292 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012293 bool Regs64bit = T == MVT::i128;
12294 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012295 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012296 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12297 DAG.getConstant(0, HalfT));
12298 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12299 DAG.getConstant(1, HalfT));
12300 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12301 Regs64bit ? X86::RAX : X86::EAX,
12302 cpInL, SDValue());
12303 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12304 Regs64bit ? X86::RDX : X86::EDX,
12305 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012306 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012307 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12308 DAG.getConstant(0, HalfT));
12309 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12310 DAG.getConstant(1, HalfT));
12311 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12312 Regs64bit ? X86::RBX : X86::EBX,
12313 swapInL, cpInH.getValue(1));
12314 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012315 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012316 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012317 SDValue Ops[] = { swapInH.getValue(0),
12318 N->getOperand(1),
12319 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012320 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012321 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012322 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12323 X86ISD::LCMPXCHG8_DAG;
12324 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012325 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012326 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12327 Regs64bit ? X86::RAX : X86::EAX,
12328 HalfT, Result.getValue(1));
12329 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12330 Regs64bit ? X86::RDX : X86::EDX,
12331 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012332 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012333 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012334 Results.push_back(cpOutH.getValue(1));
12335 return;
12336 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012337 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012338 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012339 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012340 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012341 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012342 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012343 case ISD::ATOMIC_LOAD_MAX:
12344 case ISD::ATOMIC_LOAD_MIN:
12345 case ISD::ATOMIC_LOAD_UMAX:
12346 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012347 case ISD::ATOMIC_SWAP: {
12348 unsigned Opc;
12349 switch (N->getOpcode()) {
12350 default: llvm_unreachable("Unexpected opcode");
12351 case ISD::ATOMIC_LOAD_ADD:
12352 Opc = X86ISD::ATOMADD64_DAG;
12353 break;
12354 case ISD::ATOMIC_LOAD_AND:
12355 Opc = X86ISD::ATOMAND64_DAG;
12356 break;
12357 case ISD::ATOMIC_LOAD_NAND:
12358 Opc = X86ISD::ATOMNAND64_DAG;
12359 break;
12360 case ISD::ATOMIC_LOAD_OR:
12361 Opc = X86ISD::ATOMOR64_DAG;
12362 break;
12363 case ISD::ATOMIC_LOAD_SUB:
12364 Opc = X86ISD::ATOMSUB64_DAG;
12365 break;
12366 case ISD::ATOMIC_LOAD_XOR:
12367 Opc = X86ISD::ATOMXOR64_DAG;
12368 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012369 case ISD::ATOMIC_LOAD_MAX:
12370 Opc = X86ISD::ATOMMAX64_DAG;
12371 break;
12372 case ISD::ATOMIC_LOAD_MIN:
12373 Opc = X86ISD::ATOMMIN64_DAG;
12374 break;
12375 case ISD::ATOMIC_LOAD_UMAX:
12376 Opc = X86ISD::ATOMUMAX64_DAG;
12377 break;
12378 case ISD::ATOMIC_LOAD_UMIN:
12379 Opc = X86ISD::ATOMUMIN64_DAG;
12380 break;
Craig Topperc0878702012-08-17 06:55:11 +000012381 case ISD::ATOMIC_SWAP:
12382 Opc = X86ISD::ATOMSWAP64_DAG;
12383 break;
12384 }
12385 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012386 return;
Craig Topperc0878702012-08-17 06:55:11 +000012387 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012388 case ISD::ATOMIC_LOAD:
12389 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012390 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012391}
12392
Evan Cheng72261582005-12-20 06:22:03 +000012393const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12394 switch (Opcode) {
12395 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012396 case X86ISD::BSF: return "X86ISD::BSF";
12397 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012398 case X86ISD::SHLD: return "X86ISD::SHLD";
12399 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012400 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012401 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012402 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012403 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012404 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012405 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012406 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12407 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12408 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012409 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012410 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012411 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012412 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012413 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012414 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012415 case X86ISD::COMI: return "X86ISD::COMI";
12416 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012417 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012418 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012419 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12420 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012421 case X86ISD::CMOV: return "X86ISD::CMOV";
12422 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012423 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012424 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12425 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012426 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012427 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012428 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012429 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012430 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012431 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12432 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012433 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012434 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012435 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012436 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012437 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012438 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012439 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012440 case X86ISD::HADD: return "X86ISD::HADD";
12441 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012442 case X86ISD::FHADD: return "X86ISD::FHADD";
12443 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012444 case X86ISD::UMAX: return "X86ISD::UMAX";
12445 case X86ISD::UMIN: return "X86ISD::UMIN";
12446 case X86ISD::SMAX: return "X86ISD::SMAX";
12447 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012448 case X86ISD::FMAX: return "X86ISD::FMAX";
12449 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012450 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12451 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012452 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12453 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012454 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012455 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012456 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012457 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12458 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012459 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012460 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012461 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012462 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012463 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12464 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012465 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12466 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12467 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12468 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12469 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12470 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012471 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012472 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012473 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012474 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12475 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012476 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012477 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012478 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12479 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012480 case X86ISD::VSHL: return "X86ISD::VSHL";
12481 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012482 case X86ISD::VSRA: return "X86ISD::VSRA";
12483 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12484 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12485 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012486 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012487 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12488 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012489 case X86ISD::ADD: return "X86ISD::ADD";
12490 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012491 case X86ISD::ADC: return "X86ISD::ADC";
12492 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012493 case X86ISD::SMUL: return "X86ISD::SMUL";
12494 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012495 case X86ISD::INC: return "X86ISD::INC";
12496 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012497 case X86ISD::OR: return "X86ISD::OR";
12498 case X86ISD::XOR: return "X86ISD::XOR";
12499 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012500 case X86ISD::BLSI: return "X86ISD::BLSI";
12501 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12502 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012503 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012504 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012505 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012506 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012507 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12508 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012509 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012510 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012511 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012512 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012513 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012514 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12515 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012516 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12517 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12518 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012519 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12520 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012521 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12522 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012523 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012524 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012525 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012526 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12527 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012528 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012529 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012530 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012531 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012532 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012533 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012534 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012535 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012536 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012537 case X86ISD::FMADD: return "X86ISD::FMADD";
12538 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12539 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12540 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12541 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12542 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012543 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12544 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012545 }
12546}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012547
Chris Lattnerc9addb72007-03-30 23:15:24 +000012548// isLegalAddressingMode - Return true if the addressing mode represented
12549// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012550bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012551 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012552 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012553 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012554 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012555
Chris Lattnerc9addb72007-03-30 23:15:24 +000012556 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012557 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012558 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012559
Chris Lattnerc9addb72007-03-30 23:15:24 +000012560 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012561 unsigned GVFlags =
12562 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012563
Chris Lattnerdfed4132009-07-10 07:38:24 +000012564 // If a reference to this global requires an extra load, we can't fold it.
12565 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012566 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012567
Chris Lattnerdfed4132009-07-10 07:38:24 +000012568 // If BaseGV requires a register for the PIC base, we cannot also have a
12569 // BaseReg specified.
12570 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012571 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012572
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012573 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012574 if ((M != CodeModel::Small || R != Reloc::Static) &&
12575 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012576 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012577 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012578
Chris Lattnerc9addb72007-03-30 23:15:24 +000012579 switch (AM.Scale) {
12580 case 0:
12581 case 1:
12582 case 2:
12583 case 4:
12584 case 8:
12585 // These scales always work.
12586 break;
12587 case 3:
12588 case 5:
12589 case 9:
12590 // These scales are formed with basereg+scalereg. Only accept if there is
12591 // no basereg yet.
12592 if (AM.HasBaseReg)
12593 return false;
12594 break;
12595 default: // Other stuff never works.
12596 return false;
12597 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012598
Chris Lattnerc9addb72007-03-30 23:15:24 +000012599 return true;
12600}
12601
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012602bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012603 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012604 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012605 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12606 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012607 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012608}
12609
Evan Cheng70e10d32012-07-17 06:53:39 +000012610bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012611 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012612}
12613
12614bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012615 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012616 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012617}
12618
Owen Andersone50ed302009-08-10 22:56:29 +000012619bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012620 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012621 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012622 unsigned NumBits1 = VT1.getSizeInBits();
12623 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012624 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012625}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012626
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012627bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012628 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012629 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012630}
12631
Owen Andersone50ed302009-08-10 22:56:29 +000012632bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012633 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012634 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012635}
12636
Evan Cheng2766a472012-12-06 19:13:27 +000012637bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12638 EVT VT1 = Val.getValueType();
12639 if (isZExtFree(VT1, VT2))
12640 return true;
12641
12642 if (Val.getOpcode() != ISD::LOAD)
12643 return false;
12644
12645 if (!VT1.isSimple() || !VT1.isInteger() ||
12646 !VT2.isSimple() || !VT2.isInteger())
12647 return false;
12648
12649 switch (VT1.getSimpleVT().SimpleTy) {
12650 default: break;
12651 case MVT::i8:
12652 case MVT::i16:
12653 case MVT::i32:
12654 // X86 has 8, 16, and 32-bit zero-extending loads.
12655 return true;
12656 }
12657
12658 return false;
12659}
12660
Owen Andersone50ed302009-08-10 22:56:29 +000012661bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012662 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012663 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012664}
12665
Evan Cheng60c07e12006-07-05 22:17:51 +000012666/// isShuffleMaskLegal - Targets can use this to indicate that they only
12667/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12668/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12669/// are assumed to be legal.
12670bool
Eric Christopherfd179292009-08-27 18:07:15 +000012671X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012672 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012673 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012674 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012675 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012676
Nate Begemana09008b2009-10-19 02:17:23 +000012677 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012678 return (VT.getVectorNumElements() == 2 ||
12679 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12680 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012681 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012682 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012683 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12684 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012685 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012686 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12687 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12688 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12689 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012690}
12691
Dan Gohman7d8143f2008-04-09 20:09:42 +000012692bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012693X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012694 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012695 unsigned NumElts = VT.getVectorNumElements();
12696 // FIXME: This collection of masks seems suspect.
12697 if (NumElts == 2)
12698 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012699 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012700 return (isMOVLMask(Mask, VT) ||
12701 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012702 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12703 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012704 }
12705 return false;
12706}
12707
12708//===----------------------------------------------------------------------===//
12709// X86 Scheduler Hooks
12710//===----------------------------------------------------------------------===//
12711
Michael Liaobe02a902012-11-08 07:28:54 +000012712/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012713static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12714 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012715 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012716
12717 const BasicBlock *BB = MBB->getBasicBlock();
12718 MachineFunction::iterator I = MBB;
12719 ++I;
12720
12721 // For the v = xbegin(), we generate
12722 //
12723 // thisMBB:
12724 // xbegin sinkMBB
12725 //
12726 // mainMBB:
12727 // eax = -1
12728 //
12729 // sinkMBB:
12730 // v = eax
12731
12732 MachineBasicBlock *thisMBB = MBB;
12733 MachineFunction *MF = MBB->getParent();
12734 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12735 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12736 MF->insert(I, mainMBB);
12737 MF->insert(I, sinkMBB);
12738
12739 // Transfer the remainder of BB and its successor edges to sinkMBB.
12740 sinkMBB->splice(sinkMBB->begin(), MBB,
12741 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12742 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12743
12744 // thisMBB:
12745 // xbegin sinkMBB
12746 // # fallthrough to mainMBB
12747 // # abortion to sinkMBB
12748 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12749 thisMBB->addSuccessor(mainMBB);
12750 thisMBB->addSuccessor(sinkMBB);
12751
12752 // mainMBB:
12753 // EAX = -1
12754 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12755 mainMBB->addSuccessor(sinkMBB);
12756
12757 // sinkMBB:
12758 // EAX is live into the sinkMBB
12759 sinkMBB->addLiveIn(X86::EAX);
12760 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12761 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12762 .addReg(X86::EAX);
12763
12764 MI->eraseFromParent();
12765 return sinkMBB;
12766}
12767
Michael Liaob118a072012-09-20 03:06:15 +000012768// Get CMPXCHG opcode for the specified data type.
12769static unsigned getCmpXChgOpcode(EVT VT) {
12770 switch (VT.getSimpleVT().SimpleTy) {
12771 case MVT::i8: return X86::LCMPXCHG8;
12772 case MVT::i16: return X86::LCMPXCHG16;
12773 case MVT::i32: return X86::LCMPXCHG32;
12774 case MVT::i64: return X86::LCMPXCHG64;
12775 default:
12776 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012777 }
Michael Liaob118a072012-09-20 03:06:15 +000012778 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012779}
12780
Michael Liaob118a072012-09-20 03:06:15 +000012781// Get LOAD opcode for the specified data type.
12782static unsigned getLoadOpcode(EVT VT) {
12783 switch (VT.getSimpleVT().SimpleTy) {
12784 case MVT::i8: return X86::MOV8rm;
12785 case MVT::i16: return X86::MOV16rm;
12786 case MVT::i32: return X86::MOV32rm;
12787 case MVT::i64: return X86::MOV64rm;
12788 default:
12789 break;
12790 }
12791 llvm_unreachable("Invalid operand size!");
12792}
12793
12794// Get opcode of the non-atomic one from the specified atomic instruction.
12795static unsigned getNonAtomicOpcode(unsigned Opc) {
12796 switch (Opc) {
12797 case X86::ATOMAND8: return X86::AND8rr;
12798 case X86::ATOMAND16: return X86::AND16rr;
12799 case X86::ATOMAND32: return X86::AND32rr;
12800 case X86::ATOMAND64: return X86::AND64rr;
12801 case X86::ATOMOR8: return X86::OR8rr;
12802 case X86::ATOMOR16: return X86::OR16rr;
12803 case X86::ATOMOR32: return X86::OR32rr;
12804 case X86::ATOMOR64: return X86::OR64rr;
12805 case X86::ATOMXOR8: return X86::XOR8rr;
12806 case X86::ATOMXOR16: return X86::XOR16rr;
12807 case X86::ATOMXOR32: return X86::XOR32rr;
12808 case X86::ATOMXOR64: return X86::XOR64rr;
12809 }
12810 llvm_unreachable("Unhandled atomic-load-op opcode!");
12811}
12812
12813// Get opcode of the non-atomic one from the specified atomic instruction with
12814// extra opcode.
12815static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12816 unsigned &ExtraOpc) {
12817 switch (Opc) {
12818 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12819 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12820 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12821 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012822 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012823 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12824 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12825 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012826 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012827 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12828 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12829 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012830 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012831 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12832 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12833 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012834 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012835 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12836 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12837 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12838 }
12839 llvm_unreachable("Unhandled atomic-load-op opcode!");
12840}
12841
12842// Get opcode of the non-atomic one from the specified atomic instruction for
12843// 64-bit data type on 32-bit target.
12844static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12845 switch (Opc) {
12846 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12847 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12848 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12849 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12850 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12851 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012852 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12853 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12854 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12855 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012856 }
12857 llvm_unreachable("Unhandled atomic-load-op opcode!");
12858}
12859
12860// Get opcode of the non-atomic one from the specified atomic instruction for
12861// 64-bit data type on 32-bit target with extra opcode.
12862static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12863 unsigned &HiOpc,
12864 unsigned &ExtraOpc) {
12865 switch (Opc) {
12866 case X86::ATOMNAND6432:
12867 ExtraOpc = X86::NOT32r;
12868 HiOpc = X86::AND32rr;
12869 return X86::AND32rr;
12870 }
12871 llvm_unreachable("Unhandled atomic-load-op opcode!");
12872}
12873
12874// Get pseudo CMOV opcode from the specified data type.
12875static unsigned getPseudoCMOVOpc(EVT VT) {
12876 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012877 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012878 case MVT::i16: return X86::CMOV_GR16;
12879 case MVT::i32: return X86::CMOV_GR32;
12880 default:
12881 break;
12882 }
12883 llvm_unreachable("Unknown CMOV opcode!");
12884}
12885
12886// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12887// They will be translated into a spin-loop or compare-exchange loop from
12888//
12889// ...
12890// dst = atomic-fetch-op MI.addr, MI.val
12891// ...
12892//
12893// to
12894//
12895// ...
Michael Liaoc537f792013-03-06 00:17:04 +000012896// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000012897// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000012898// t4 = phi(t1, t3 / loop)
12899// t2 = OP MI.val, t4
12900// EAX = t4
12901// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
12902// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000012903// JNE loop
12904// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000012905// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000012906// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012907MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012908X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12909 MachineBasicBlock *MBB) const {
12910 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12911 DebugLoc DL = MI->getDebugLoc();
12912
12913 MachineFunction *MF = MBB->getParent();
12914 MachineRegisterInfo &MRI = MF->getRegInfo();
12915
12916 const BasicBlock *BB = MBB->getBasicBlock();
12917 MachineFunction::iterator I = MBB;
12918 ++I;
12919
Michael Liao13d08bf2013-01-22 21:47:38 +000012920 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000012921 "Unexpected number of operands");
12922
12923 assert(MI->hasOneMemOperand() &&
12924 "Expected atomic-load-op to have one memoperand");
12925
12926 // Memory Reference
12927 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12928 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12929
12930 unsigned DstReg, SrcReg;
12931 unsigned MemOpndSlot;
12932
12933 unsigned CurOp = 0;
12934
12935 DstReg = MI->getOperand(CurOp++).getReg();
12936 MemOpndSlot = CurOp;
12937 CurOp += X86::AddrNumOperands;
12938 SrcReg = MI->getOperand(CurOp++).getReg();
12939
12940 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012941 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000012942 unsigned t1 = MRI.createVirtualRegister(RC);
12943 unsigned t2 = MRI.createVirtualRegister(RC);
12944 unsigned t3 = MRI.createVirtualRegister(RC);
12945 unsigned t4 = MRI.createVirtualRegister(RC);
12946 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000012947
12948 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12949 unsigned LOADOpc = getLoadOpcode(VT);
12950
12951 // For the atomic load-arith operator, we generate
12952 //
12953 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012954 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000012955 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012956 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000012957 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000012958 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000012959 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000012960 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000012961 // JNE mainMBB
12962 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012963 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000012964
12965 MachineBasicBlock *thisMBB = MBB;
12966 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12967 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12968 MF->insert(I, mainMBB);
12969 MF->insert(I, sinkMBB);
12970
12971 MachineInstrBuilder MIB;
12972
12973 // Transfer the remainder of BB and its successor edges to sinkMBB.
12974 sinkMBB->splice(sinkMBB->begin(), MBB,
12975 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12976 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12977
12978 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012979 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
12980 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
12981 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
12982 if (NewMO.isReg())
12983 NewMO.setIsKill(false);
12984 MIB.addOperand(NewMO);
12985 }
12986 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
12987 unsigned flags = (*MMOI)->getFlags();
12988 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
12989 MachineMemOperand *MMO =
12990 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
12991 (*MMOI)->getSize(),
12992 (*MMOI)->getBaseAlignment(),
12993 (*MMOI)->getTBAAInfo(),
12994 (*MMOI)->getRanges());
12995 MIB.addMemOperand(MMO);
12996 }
Michael Liaob118a072012-09-20 03:06:15 +000012997
12998 thisMBB->addSuccessor(mainMBB);
12999
13000 // mainMBB:
13001 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013002
Michael Liaoc537f792013-03-06 00:17:04 +000013003 // Add a PHI.
13004 BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13005 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013006
Michael Liaob118a072012-09-20 03:06:15 +000013007 unsigned Opc = MI->getOpcode();
13008 switch (Opc) {
13009 default:
13010 llvm_unreachable("Unhandled atomic-load-op opcode!");
13011 case X86::ATOMAND8:
13012 case X86::ATOMAND16:
13013 case X86::ATOMAND32:
13014 case X86::ATOMAND64:
13015 case X86::ATOMOR8:
13016 case X86::ATOMOR16:
13017 case X86::ATOMOR32:
13018 case X86::ATOMOR64:
13019 case X86::ATOMXOR8:
13020 case X86::ATOMXOR16:
13021 case X86::ATOMXOR32:
13022 case X86::ATOMXOR64: {
13023 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013024 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13025 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013026 break;
13027 }
13028 case X86::ATOMNAND8:
13029 case X86::ATOMNAND16:
13030 case X86::ATOMNAND32:
13031 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013032 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013033 unsigned NOTOpc;
13034 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013035 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13036 .addReg(t4);
13037 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013038 break;
13039 }
Michael Liao08382492012-09-21 03:00:17 +000013040 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013041 case X86::ATOMMAX16:
13042 case X86::ATOMMAX32:
13043 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013044 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013045 case X86::ATOMMIN16:
13046 case X86::ATOMMIN32:
13047 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013048 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013049 case X86::ATOMUMAX16:
13050 case X86::ATOMUMAX32:
13051 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013052 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013053 case X86::ATOMUMIN16:
13054 case X86::ATOMUMIN32:
13055 case X86::ATOMUMIN64: {
13056 unsigned CMPOpc;
13057 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13058
13059 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13060 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013061 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013062
13063 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013064 if (VT != MVT::i8) {
13065 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013066 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013067 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013068 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013069 } else {
13070 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013071 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13072 const TargetRegisterClass *RC32 =
13073 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013074 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13075 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013076 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013077
13078 unsigned Undef = MRI.createVirtualRegister(RC32);
13079 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13080
13081 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13082 .addReg(Undef)
13083 .addReg(SrcReg)
13084 .addImm(X86::sub_8bit);
13085 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13086 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013087 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013088 .addImm(X86::sub_8bit);
13089
Michael Liaoc537f792013-03-06 00:17:04 +000013090 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013091 .addReg(SrcReg32)
13092 .addReg(AccReg32);
13093
Michael Liaoc537f792013-03-06 00:17:04 +000013094 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13095 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013096 }
Michael Liaob118a072012-09-20 03:06:15 +000013097 } else {
13098 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013099 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013100 "Invalid atomic-load-op transformation!");
13101 unsigned SelOpc = getPseudoCMOVOpc(VT);
13102 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13103 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013104 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13105 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013106 .addImm(CC);
13107 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13108 }
13109 break;
13110 }
13111 }
13112
Michael Liaoc537f792013-03-06 00:17:04 +000013113 // Copy PhyReg back from virtual register.
13114 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13115 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013116
13117 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013118 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13119 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13120 if (NewMO.isReg())
13121 NewMO.setIsKill(false);
13122 MIB.addOperand(NewMO);
13123 }
13124 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013125 MIB.setMemRefs(MMOBegin, MMOEnd);
13126
Michael Liaoc537f792013-03-06 00:17:04 +000013127 // Copy PhyReg back to virtual register.
13128 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13129 .addReg(PhyReg);
13130
Michael Liaob118a072012-09-20 03:06:15 +000013131 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13132
13133 mainMBB->addSuccessor(origMainMBB);
13134 mainMBB->addSuccessor(sinkMBB);
13135
13136 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013137 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13138 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013139 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013140
13141 MI->eraseFromParent();
13142 return sinkMBB;
13143}
13144
13145// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13146// instructions. They will be translated into a spin-loop or compare-exchange
13147// loop from
13148//
13149// ...
13150// dst = atomic-fetch-op MI.addr, MI.val
13151// ...
13152//
13153// to
13154//
13155// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013156// t1L = LOAD [MI.addr + 0]
13157// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013158// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013159// t4L = phi(t1L, t3L / loop)
13160// t4H = phi(t1H, t3H / loop)
13161// t2L = OP MI.val.lo, t4L
13162// t2H = OP MI.val.hi, t4H
13163// EAX = t4L
13164// EDX = t4H
13165// EBX = t2L
13166// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013167// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013168// t3L = EAX
13169// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013170// JNE loop
13171// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013172// dstL = t3L
13173// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013174// ...
13175MachineBasicBlock *
13176X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13177 MachineBasicBlock *MBB) const {
13178 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13179 DebugLoc DL = MI->getDebugLoc();
13180
13181 MachineFunction *MF = MBB->getParent();
13182 MachineRegisterInfo &MRI = MF->getRegInfo();
13183
13184 const BasicBlock *BB = MBB->getBasicBlock();
13185 MachineFunction::iterator I = MBB;
13186 ++I;
13187
Michael Liao13d08bf2013-01-22 21:47:38 +000013188 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013189 "Unexpected number of operands");
13190
13191 assert(MI->hasOneMemOperand() &&
13192 "Expected atomic-load-op32 to have one memoperand");
13193
13194 // Memory Reference
13195 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13196 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13197
13198 unsigned DstLoReg, DstHiReg;
13199 unsigned SrcLoReg, SrcHiReg;
13200 unsigned MemOpndSlot;
13201
13202 unsigned CurOp = 0;
13203
13204 DstLoReg = MI->getOperand(CurOp++).getReg();
13205 DstHiReg = MI->getOperand(CurOp++).getReg();
13206 MemOpndSlot = CurOp;
13207 CurOp += X86::AddrNumOperands;
13208 SrcLoReg = MI->getOperand(CurOp++).getReg();
13209 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013210
Craig Topperc9099502012-04-20 06:31:50 +000013211 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013212 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013213
Michael Liaoc537f792013-03-06 00:17:04 +000013214 unsigned t1L = MRI.createVirtualRegister(RC);
13215 unsigned t1H = MRI.createVirtualRegister(RC);
13216 unsigned t2L = MRI.createVirtualRegister(RC);
13217 unsigned t2H = MRI.createVirtualRegister(RC);
13218 unsigned t3L = MRI.createVirtualRegister(RC);
13219 unsigned t3H = MRI.createVirtualRegister(RC);
13220 unsigned t4L = MRI.createVirtualRegister(RC);
13221 unsigned t4H = MRI.createVirtualRegister(RC);
13222
Michael Liaob118a072012-09-20 03:06:15 +000013223 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13224 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013225
Michael Liaob118a072012-09-20 03:06:15 +000013226 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013227 //
Michael Liaob118a072012-09-20 03:06:15 +000013228 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013229 // t1L = LOAD [MI.addr + 0]
13230 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013231 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013232 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13233 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13234 // t2L = OP MI.val.lo, t4L
13235 // t2H = OP MI.val.hi, t4H
13236 // EBX = t2L
13237 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013238 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013239 // t3L = EAX
13240 // t3H = EDX
13241 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013242 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013243 // dstL = t3L
13244 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013245
Mon P Wang63307c32008-05-05 19:05:59 +000013246 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013247 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13248 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13249 MF->insert(I, mainMBB);
13250 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013251
Michael Liaob118a072012-09-20 03:06:15 +000013252 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013253
Michael Liaob118a072012-09-20 03:06:15 +000013254 // Transfer the remainder of BB and its successor edges to sinkMBB.
13255 sinkMBB->splice(sinkMBB->begin(), MBB,
13256 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13257 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013258
Michael Liaob118a072012-09-20 03:06:15 +000013259 // thisMBB:
13260 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013261 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013262 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013263 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13264 if (NewMO.isReg())
13265 NewMO.setIsKill(false);
13266 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013267 }
Michael Liaoc537f792013-03-06 00:17:04 +000013268 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13269 unsigned flags = (*MMOI)->getFlags();
13270 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13271 MachineMemOperand *MMO =
13272 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13273 (*MMOI)->getSize(),
13274 (*MMOI)->getBaseAlignment(),
13275 (*MMOI)->getTBAAInfo(),
13276 (*MMOI)->getRanges());
13277 MIB.addMemOperand(MMO);
13278 };
13279 MachineInstr *LowMI = MIB;
13280
13281 // Hi
13282 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13283 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13284 if (i == X86::AddrDisp) {
13285 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13286 } else {
13287 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13288 if (NewMO.isReg())
13289 NewMO.setIsKill(false);
13290 MIB.addOperand(NewMO);
13291 }
13292 }
13293 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013294
Michael Liaob118a072012-09-20 03:06:15 +000013295 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013296
Michael Liaob118a072012-09-20 03:06:15 +000013297 // mainMBB:
13298 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013299
Michael Liaoc537f792013-03-06 00:17:04 +000013300 // Add PHIs.
13301 BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13302 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13303 BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13304 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013305
Michael Liaob118a072012-09-20 03:06:15 +000013306 unsigned Opc = MI->getOpcode();
13307 switch (Opc) {
13308 default:
13309 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13310 case X86::ATOMAND6432:
13311 case X86::ATOMOR6432:
13312 case X86::ATOMXOR6432:
13313 case X86::ATOMADD6432:
13314 case X86::ATOMSUB6432: {
13315 unsigned HiOpc;
13316 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013317 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13318 .addReg(SrcLoReg);
13319 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13320 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013321 break;
13322 }
13323 case X86::ATOMNAND6432: {
13324 unsigned HiOpc, NOTOpc;
13325 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013326 unsigned TmpL = MRI.createVirtualRegister(RC);
13327 unsigned TmpH = MRI.createVirtualRegister(RC);
13328 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13329 .addReg(t4L);
13330 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13331 .addReg(t4H);
13332 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13333 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013334 break;
13335 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013336 case X86::ATOMMAX6432:
13337 case X86::ATOMMIN6432:
13338 case X86::ATOMUMAX6432:
13339 case X86::ATOMUMIN6432: {
13340 unsigned HiOpc;
13341 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13342 unsigned cL = MRI.createVirtualRegister(RC8);
13343 unsigned cH = MRI.createVirtualRegister(RC8);
13344 unsigned cL32 = MRI.createVirtualRegister(RC);
13345 unsigned cH32 = MRI.createVirtualRegister(RC);
13346 unsigned cc = MRI.createVirtualRegister(RC);
13347 // cl := cmp src_lo, lo
13348 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013349 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013350 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13351 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13352 // ch := cmp src_hi, hi
13353 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013354 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013355 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13356 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13357 // cc := if (src_hi == hi) ? cl : ch;
13358 if (Subtarget->hasCMov()) {
13359 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13360 .addReg(cH32).addReg(cL32);
13361 } else {
13362 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13363 .addReg(cH32).addReg(cL32)
13364 .addImm(X86::COND_E);
13365 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13366 }
13367 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13368 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013369 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13370 .addReg(SrcLoReg).addReg(t4L);
13371 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13372 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013373 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013374 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13375 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013376 .addImm(X86::COND_NE);
13377 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaoc537f792013-03-06 00:17:04 +000013378 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13379 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013380 .addImm(X86::COND_NE);
13381 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13382 }
13383 break;
13384 }
Michael Liaob118a072012-09-20 03:06:15 +000013385 case X86::ATOMSWAP6432: {
13386 unsigned HiOpc;
13387 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013388 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13389 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013390 break;
13391 }
13392 }
Mon P Wang63307c32008-05-05 19:05:59 +000013393
Michael Liaob118a072012-09-20 03:06:15 +000013394 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013395 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13396 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013397 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013398 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13399 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013400
Michael Liaob118a072012-09-20 03:06:15 +000013401 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013402 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13403 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13404 if (NewMO.isReg())
13405 NewMO.setIsKill(false);
13406 MIB.addOperand(NewMO);
13407 }
Michael Liaob118a072012-09-20 03:06:15 +000013408 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013409
Michael Liaoc537f792013-03-06 00:17:04 +000013410 // Copy EDX:EAX back to t3H:t3L
13411 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13412 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13413
Michael Liaob118a072012-09-20 03:06:15 +000013414 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013415
Michael Liaob118a072012-09-20 03:06:15 +000013416 mainMBB->addSuccessor(origMainMBB);
13417 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013418
Michael Liaob118a072012-09-20 03:06:15 +000013419 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013420 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13421 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013422 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013423 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13424 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013425 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013426
Michael Liaob118a072012-09-20 03:06:15 +000013427 MI->eraseFromParent();
13428 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013429}
13430
Eric Christopherf83a5de2009-08-27 18:08:16 +000013431// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013432// or XMM0_V32I8 in AVX all of this code can be replaced with that
13433// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013434static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13435 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013436 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013437 switch (MI->getOpcode()) {
13438 default: llvm_unreachable("illegal opcode!");
13439 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13440 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13441 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13442 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13443 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13444 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13445 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13446 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013447 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013448
Craig Topper8aae8dd2012-11-10 08:57:41 +000013449 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013450 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013451
Craig Topper52ea2452012-11-10 09:25:36 +000013452 unsigned NumArgs = MI->getNumOperands();
13453 for (unsigned i = 1; i < NumArgs; ++i) {
13454 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013455 if (!(Op.isReg() && Op.isImplicit()))
13456 MIB.addOperand(Op);
13457 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013458 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013459 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13460
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013461 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013462 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013463 .addReg(X86::XMM0);
13464
Dan Gohman14152b42010-07-06 20:24:04 +000013465 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013466 return BB;
13467}
13468
Craig Topper9c7ae012012-11-10 01:23:36 +000013469// FIXME: Custom handling because TableGen doesn't support multiple implicit
13470// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013471static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13472 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013473 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013474 switch (MI->getOpcode()) {
13475 default: llvm_unreachable("illegal opcode!");
13476 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13477 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13478 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13479 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13480 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13481 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13482 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13483 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013484 }
13485
Craig Topper8aae8dd2012-11-10 08:57:41 +000013486 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013487 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013488
Craig Topper52ea2452012-11-10 09:25:36 +000013489 unsigned NumArgs = MI->getNumOperands(); // remove the results
13490 for (unsigned i = 1; i < NumArgs; ++i) {
13491 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013492 if (!(Op.isReg() && Op.isImplicit()))
13493 MIB.addOperand(Op);
13494 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013495 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013496 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13497
13498 BuildMI(*BB, MI, dl,
13499 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13500 .addReg(X86::ECX);
13501
13502 MI->eraseFromParent();
13503 return BB;
13504}
13505
Craig Topper2da36912012-11-11 22:45:02 +000013506static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13507 const TargetInstrInfo *TII,
13508 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013509 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013510
Eric Christopher228232b2010-11-30 07:20:12 +000013511 // Address into RAX/EAX, other two args into ECX, EDX.
13512 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13513 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13514 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13515 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013516 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013517
Eric Christopher228232b2010-11-30 07:20:12 +000013518 unsigned ValOps = X86::AddrNumOperands;
13519 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13520 .addReg(MI->getOperand(ValOps).getReg());
13521 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13522 .addReg(MI->getOperand(ValOps+1).getReg());
13523
13524 // The instruction doesn't actually take any operands though.
13525 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013526
Eric Christopher228232b2010-11-30 07:20:12 +000013527 MI->eraseFromParent(); // The pseudo is gone now.
13528 return BB;
13529}
13530
13531MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013532X86TargetLowering::EmitVAARG64WithCustomInserter(
13533 MachineInstr *MI,
13534 MachineBasicBlock *MBB) const {
13535 // Emit va_arg instruction on X86-64.
13536
13537 // Operands to this pseudo-instruction:
13538 // 0 ) Output : destination address (reg)
13539 // 1-5) Input : va_list address (addr, i64mem)
13540 // 6 ) ArgSize : Size (in bytes) of vararg type
13541 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13542 // 8 ) Align : Alignment of type
13543 // 9 ) EFLAGS (implicit-def)
13544
13545 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13546 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13547
13548 unsigned DestReg = MI->getOperand(0).getReg();
13549 MachineOperand &Base = MI->getOperand(1);
13550 MachineOperand &Scale = MI->getOperand(2);
13551 MachineOperand &Index = MI->getOperand(3);
13552 MachineOperand &Disp = MI->getOperand(4);
13553 MachineOperand &Segment = MI->getOperand(5);
13554 unsigned ArgSize = MI->getOperand(6).getImm();
13555 unsigned ArgMode = MI->getOperand(7).getImm();
13556 unsigned Align = MI->getOperand(8).getImm();
13557
13558 // Memory Reference
13559 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13560 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13561 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13562
13563 // Machine Information
13564 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13565 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13566 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13567 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13568 DebugLoc DL = MI->getDebugLoc();
13569
13570 // struct va_list {
13571 // i32 gp_offset
13572 // i32 fp_offset
13573 // i64 overflow_area (address)
13574 // i64 reg_save_area (address)
13575 // }
13576 // sizeof(va_list) = 24
13577 // alignment(va_list) = 8
13578
13579 unsigned TotalNumIntRegs = 6;
13580 unsigned TotalNumXMMRegs = 8;
13581 bool UseGPOffset = (ArgMode == 1);
13582 bool UseFPOffset = (ArgMode == 2);
13583 unsigned MaxOffset = TotalNumIntRegs * 8 +
13584 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13585
13586 /* Align ArgSize to a multiple of 8 */
13587 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13588 bool NeedsAlign = (Align > 8);
13589
13590 MachineBasicBlock *thisMBB = MBB;
13591 MachineBasicBlock *overflowMBB;
13592 MachineBasicBlock *offsetMBB;
13593 MachineBasicBlock *endMBB;
13594
13595 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13596 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13597 unsigned OffsetReg = 0;
13598
13599 if (!UseGPOffset && !UseFPOffset) {
13600 // If we only pull from the overflow region, we don't create a branch.
13601 // We don't need to alter control flow.
13602 OffsetDestReg = 0; // unused
13603 OverflowDestReg = DestReg;
13604
13605 offsetMBB = NULL;
13606 overflowMBB = thisMBB;
13607 endMBB = thisMBB;
13608 } else {
13609 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13610 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13611 // If not, pull from overflow_area. (branch to overflowMBB)
13612 //
13613 // thisMBB
13614 // | .
13615 // | .
13616 // offsetMBB overflowMBB
13617 // | .
13618 // | .
13619 // endMBB
13620
13621 // Registers for the PHI in endMBB
13622 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13623 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13624
13625 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13626 MachineFunction *MF = MBB->getParent();
13627 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13628 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13629 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13630
13631 MachineFunction::iterator MBBIter = MBB;
13632 ++MBBIter;
13633
13634 // Insert the new basic blocks
13635 MF->insert(MBBIter, offsetMBB);
13636 MF->insert(MBBIter, overflowMBB);
13637 MF->insert(MBBIter, endMBB);
13638
13639 // Transfer the remainder of MBB and its successor edges to endMBB.
13640 endMBB->splice(endMBB->begin(), thisMBB,
13641 llvm::next(MachineBasicBlock::iterator(MI)),
13642 thisMBB->end());
13643 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13644
13645 // Make offsetMBB and overflowMBB successors of thisMBB
13646 thisMBB->addSuccessor(offsetMBB);
13647 thisMBB->addSuccessor(overflowMBB);
13648
13649 // endMBB is a successor of both offsetMBB and overflowMBB
13650 offsetMBB->addSuccessor(endMBB);
13651 overflowMBB->addSuccessor(endMBB);
13652
13653 // Load the offset value into a register
13654 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13655 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13656 .addOperand(Base)
13657 .addOperand(Scale)
13658 .addOperand(Index)
13659 .addDisp(Disp, UseFPOffset ? 4 : 0)
13660 .addOperand(Segment)
13661 .setMemRefs(MMOBegin, MMOEnd);
13662
13663 // Check if there is enough room left to pull this argument.
13664 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13665 .addReg(OffsetReg)
13666 .addImm(MaxOffset + 8 - ArgSizeA8);
13667
13668 // Branch to "overflowMBB" if offset >= max
13669 // Fall through to "offsetMBB" otherwise
13670 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13671 .addMBB(overflowMBB);
13672 }
13673
13674 // In offsetMBB, emit code to use the reg_save_area.
13675 if (offsetMBB) {
13676 assert(OffsetReg != 0);
13677
13678 // Read the reg_save_area address.
13679 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13680 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13681 .addOperand(Base)
13682 .addOperand(Scale)
13683 .addOperand(Index)
13684 .addDisp(Disp, 16)
13685 .addOperand(Segment)
13686 .setMemRefs(MMOBegin, MMOEnd);
13687
13688 // Zero-extend the offset
13689 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13690 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13691 .addImm(0)
13692 .addReg(OffsetReg)
13693 .addImm(X86::sub_32bit);
13694
13695 // Add the offset to the reg_save_area to get the final address.
13696 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13697 .addReg(OffsetReg64)
13698 .addReg(RegSaveReg);
13699
13700 // Compute the offset for the next argument
13701 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13702 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13703 .addReg(OffsetReg)
13704 .addImm(UseFPOffset ? 16 : 8);
13705
13706 // Store it back into the va_list.
13707 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13708 .addOperand(Base)
13709 .addOperand(Scale)
13710 .addOperand(Index)
13711 .addDisp(Disp, UseFPOffset ? 4 : 0)
13712 .addOperand(Segment)
13713 .addReg(NextOffsetReg)
13714 .setMemRefs(MMOBegin, MMOEnd);
13715
13716 // Jump to endMBB
13717 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13718 .addMBB(endMBB);
13719 }
13720
13721 //
13722 // Emit code to use overflow area
13723 //
13724
13725 // Load the overflow_area address into a register.
13726 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13727 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13728 .addOperand(Base)
13729 .addOperand(Scale)
13730 .addOperand(Index)
13731 .addDisp(Disp, 8)
13732 .addOperand(Segment)
13733 .setMemRefs(MMOBegin, MMOEnd);
13734
13735 // If we need to align it, do so. Otherwise, just copy the address
13736 // to OverflowDestReg.
13737 if (NeedsAlign) {
13738 // Align the overflow address
13739 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13740 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13741
13742 // aligned_addr = (addr + (align-1)) & ~(align-1)
13743 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13744 .addReg(OverflowAddrReg)
13745 .addImm(Align-1);
13746
13747 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13748 .addReg(TmpReg)
13749 .addImm(~(uint64_t)(Align-1));
13750 } else {
13751 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13752 .addReg(OverflowAddrReg);
13753 }
13754
13755 // Compute the next overflow address after this argument.
13756 // (the overflow address should be kept 8-byte aligned)
13757 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13758 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13759 .addReg(OverflowDestReg)
13760 .addImm(ArgSizeA8);
13761
13762 // Store the new overflow address.
13763 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13764 .addOperand(Base)
13765 .addOperand(Scale)
13766 .addOperand(Index)
13767 .addDisp(Disp, 8)
13768 .addOperand(Segment)
13769 .addReg(NextAddrReg)
13770 .setMemRefs(MMOBegin, MMOEnd);
13771
13772 // If we branched, emit the PHI to the front of endMBB.
13773 if (offsetMBB) {
13774 BuildMI(*endMBB, endMBB->begin(), DL,
13775 TII->get(X86::PHI), DestReg)
13776 .addReg(OffsetDestReg).addMBB(offsetMBB)
13777 .addReg(OverflowDestReg).addMBB(overflowMBB);
13778 }
13779
13780 // Erase the pseudo instruction
13781 MI->eraseFromParent();
13782
13783 return endMBB;
13784}
13785
13786MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013787X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13788 MachineInstr *MI,
13789 MachineBasicBlock *MBB) const {
13790 // Emit code to save XMM registers to the stack. The ABI says that the
13791 // number of registers to save is given in %al, so it's theoretically
13792 // possible to do an indirect jump trick to avoid saving all of them,
13793 // however this code takes a simpler approach and just executes all
13794 // of the stores if %al is non-zero. It's less code, and it's probably
13795 // easier on the hardware branch predictor, and stores aren't all that
13796 // expensive anyway.
13797
13798 // Create the new basic blocks. One block contains all the XMM stores,
13799 // and one block is the final destination regardless of whether any
13800 // stores were performed.
13801 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13802 MachineFunction *F = MBB->getParent();
13803 MachineFunction::iterator MBBIter = MBB;
13804 ++MBBIter;
13805 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13806 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13807 F->insert(MBBIter, XMMSaveMBB);
13808 F->insert(MBBIter, EndMBB);
13809
Dan Gohman14152b42010-07-06 20:24:04 +000013810 // Transfer the remainder of MBB and its successor edges to EndMBB.
13811 EndMBB->splice(EndMBB->begin(), MBB,
13812 llvm::next(MachineBasicBlock::iterator(MI)),
13813 MBB->end());
13814 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13815
Dan Gohmand6708ea2009-08-15 01:38:56 +000013816 // The original block will now fall through to the XMM save block.
13817 MBB->addSuccessor(XMMSaveMBB);
13818 // The XMMSaveMBB will fall through to the end block.
13819 XMMSaveMBB->addSuccessor(EndMBB);
13820
13821 // Now add the instructions.
13822 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13823 DebugLoc DL = MI->getDebugLoc();
13824
13825 unsigned CountReg = MI->getOperand(0).getReg();
13826 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13827 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13828
13829 if (!Subtarget->isTargetWin64()) {
13830 // If %al is 0, branch around the XMM save block.
13831 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013832 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013833 MBB->addSuccessor(EndMBB);
13834 }
13835
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013836 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013837 // In the XMM save block, save all the XMM argument registers.
13838 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13839 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013840 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013841 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013842 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013843 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013844 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013845 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013846 .addFrameIndex(RegSaveFrameIndex)
13847 .addImm(/*Scale=*/1)
13848 .addReg(/*IndexReg=*/0)
13849 .addImm(/*Disp=*/Offset)
13850 .addReg(/*Segment=*/0)
13851 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013852 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013853 }
13854
Dan Gohman14152b42010-07-06 20:24:04 +000013855 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013856
13857 return EndMBB;
13858}
Mon P Wang63307c32008-05-05 19:05:59 +000013859
Lang Hames6e3f7e42012-02-03 01:13:49 +000013860// The EFLAGS operand of SelectItr might be missing a kill marker
13861// because there were multiple uses of EFLAGS, and ISel didn't know
13862// which to mark. Figure out whether SelectItr should have had a
13863// kill marker, and set it if it should. Returns the correct kill
13864// marker value.
13865static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13866 MachineBasicBlock* BB,
13867 const TargetRegisterInfo* TRI) {
13868 // Scan forward through BB for a use/def of EFLAGS.
13869 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13870 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013871 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013872 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013873 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013874 if (mi.definesRegister(X86::EFLAGS))
13875 break; // Should have kill-flag - update below.
13876 }
13877
13878 // If we hit the end of the block, check whether EFLAGS is live into a
13879 // successor.
13880 if (miI == BB->end()) {
13881 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13882 sEnd = BB->succ_end();
13883 sItr != sEnd; ++sItr) {
13884 MachineBasicBlock* succ = *sItr;
13885 if (succ->isLiveIn(X86::EFLAGS))
13886 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013887 }
13888 }
13889
Lang Hames6e3f7e42012-02-03 01:13:49 +000013890 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13891 // out. SelectMI should have a kill flag on EFLAGS.
13892 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013893 return true;
13894}
13895
Evan Cheng60c07e12006-07-05 22:17:51 +000013896MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013897X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013898 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013899 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13900 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013901
Chris Lattner52600972009-09-02 05:57:00 +000013902 // To "insert" a SELECT_CC instruction, we actually have to insert the
13903 // diamond control-flow pattern. The incoming instruction knows the
13904 // destination vreg to set, the condition code register to branch on, the
13905 // true/false values to select between, and a branch opcode to use.
13906 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13907 MachineFunction::iterator It = BB;
13908 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013909
Chris Lattner52600972009-09-02 05:57:00 +000013910 // thisMBB:
13911 // ...
13912 // TrueVal = ...
13913 // cmpTY ccX, r1, r2
13914 // bCC copy1MBB
13915 // fallthrough --> copy0MBB
13916 MachineBasicBlock *thisMBB = BB;
13917 MachineFunction *F = BB->getParent();
13918 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13919 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013920 F->insert(It, copy0MBB);
13921 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013922
Bill Wendling730c07e2010-06-25 20:48:10 +000013923 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13924 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013925 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13926 if (!MI->killsRegister(X86::EFLAGS) &&
13927 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13928 copy0MBB->addLiveIn(X86::EFLAGS);
13929 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013930 }
13931
Dan Gohman14152b42010-07-06 20:24:04 +000013932 // Transfer the remainder of BB and its successor edges to sinkMBB.
13933 sinkMBB->splice(sinkMBB->begin(), BB,
13934 llvm::next(MachineBasicBlock::iterator(MI)),
13935 BB->end());
13936 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13937
13938 // Add the true and fallthrough blocks as its successors.
13939 BB->addSuccessor(copy0MBB);
13940 BB->addSuccessor(sinkMBB);
13941
13942 // Create the conditional branch instruction.
13943 unsigned Opc =
13944 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13945 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13946
Chris Lattner52600972009-09-02 05:57:00 +000013947 // copy0MBB:
13948 // %FalseValue = ...
13949 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013950 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013951
Chris Lattner52600972009-09-02 05:57:00 +000013952 // sinkMBB:
13953 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13954 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013955 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13956 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013957 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13958 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13959
Dan Gohman14152b42010-07-06 20:24:04 +000013960 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013961 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013962}
13963
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013964MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013965X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13966 bool Is64Bit) const {
13967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13968 DebugLoc DL = MI->getDebugLoc();
13969 MachineFunction *MF = BB->getParent();
13970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13971
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013972 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013973
13974 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13975 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13976
13977 // BB:
13978 // ... [Till the alloca]
13979 // If stacklet is not large enough, jump to mallocMBB
13980 //
13981 // bumpMBB:
13982 // Allocate by subtracting from RSP
13983 // Jump to continueMBB
13984 //
13985 // mallocMBB:
13986 // Allocate by call to runtime
13987 //
13988 // continueMBB:
13989 // ...
13990 // [rest of original BB]
13991 //
13992
13993 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13994 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13995 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13996
13997 MachineRegisterInfo &MRI = MF->getRegInfo();
13998 const TargetRegisterClass *AddrRegClass =
13999 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14000
14001 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14002 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14003 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014004 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014005 sizeVReg = MI->getOperand(1).getReg(),
14006 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14007
14008 MachineFunction::iterator MBBIter = BB;
14009 ++MBBIter;
14010
14011 MF->insert(MBBIter, bumpMBB);
14012 MF->insert(MBBIter, mallocMBB);
14013 MF->insert(MBBIter, continueMBB);
14014
14015 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14016 (MachineBasicBlock::iterator(MI)), BB->end());
14017 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14018
14019 // Add code to the main basic block to check if the stack limit has been hit,
14020 // and if so, jump to mallocMBB otherwise to bumpMBB.
14021 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014022 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014023 .addReg(tmpSPVReg).addReg(sizeVReg);
14024 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014025 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014026 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014027 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14028
14029 // bumpMBB simply decreases the stack pointer, since we know the current
14030 // stacklet has enough space.
14031 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014032 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014033 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014034 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014035 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14036
14037 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014038 const uint32_t *RegMask =
14039 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014040 if (Is64Bit) {
14041 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14042 .addReg(sizeVReg);
14043 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014044 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014045 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014046 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014047 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014048 } else {
14049 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14050 .addImm(12);
14051 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14052 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014053 .addExternalSymbol("__morestack_allocate_stack_space")
14054 .addRegMask(RegMask)
14055 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014056 }
14057
14058 if (!Is64Bit)
14059 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14060 .addImm(16);
14061
14062 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14063 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14064 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14065
14066 // Set up the CFG correctly.
14067 BB->addSuccessor(bumpMBB);
14068 BB->addSuccessor(mallocMBB);
14069 mallocMBB->addSuccessor(continueMBB);
14070 bumpMBB->addSuccessor(continueMBB);
14071
14072 // Take care of the PHI nodes.
14073 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14074 MI->getOperand(0).getReg())
14075 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14076 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14077
14078 // Delete the original pseudo instruction.
14079 MI->eraseFromParent();
14080
14081 // And we're done.
14082 return continueMBB;
14083}
14084
14085MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014086X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014087 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014088 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14089 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014090
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014091 assert(!Subtarget->isTargetEnvMacho());
14092
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014093 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14094 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014095
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014096 if (Subtarget->isTargetWin64()) {
14097 if (Subtarget->isTargetCygMing()) {
14098 // ___chkstk(Mingw64):
14099 // Clobbers R10, R11, RAX and EFLAGS.
14100 // Updates RSP.
14101 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14102 .addExternalSymbol("___chkstk")
14103 .addReg(X86::RAX, RegState::Implicit)
14104 .addReg(X86::RSP, RegState::Implicit)
14105 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14106 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14107 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14108 } else {
14109 // __chkstk(MSVCRT): does not update stack pointer.
14110 // Clobbers R10, R11 and EFLAGS.
14111 // FIXME: RAX(allocated size) might be reused and not killed.
14112 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14113 .addExternalSymbol("__chkstk")
14114 .addReg(X86::RAX, RegState::Implicit)
14115 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14116 // RAX has the offset to subtracted from RSP.
14117 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14118 .addReg(X86::RSP)
14119 .addReg(X86::RAX);
14120 }
14121 } else {
14122 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014123 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14124
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014125 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14126 .addExternalSymbol(StackProbeSymbol)
14127 .addReg(X86::EAX, RegState::Implicit)
14128 .addReg(X86::ESP, RegState::Implicit)
14129 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14130 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14131 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14132 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014133
Dan Gohman14152b42010-07-06 20:24:04 +000014134 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014135 return BB;
14136}
Chris Lattner52600972009-09-02 05:57:00 +000014137
14138MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014139X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14140 MachineBasicBlock *BB) const {
14141 // This is pretty easy. We're taking the value that we received from
14142 // our load from the relocation, sticking it in either RDI (x86-64)
14143 // or EAX and doing an indirect call. The return value will then
14144 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014145 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014146 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014147 DebugLoc DL = MI->getDebugLoc();
14148 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014149
14150 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014151 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014152
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014153 // Get a register mask for the lowered call.
14154 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14155 // proper register mask.
14156 const uint32_t *RegMask =
14157 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014158 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014159 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14160 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014161 .addReg(X86::RIP)
14162 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014163 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014164 MI->getOperand(3).getTargetFlags())
14165 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014166 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014167 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014168 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014169 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014170 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14171 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014172 .addReg(0)
14173 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014174 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014175 MI->getOperand(3).getTargetFlags())
14176 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014177 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014178 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014179 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014180 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014181 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14182 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014183 .addReg(TII->getGlobalBaseReg(F))
14184 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014185 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014186 MI->getOperand(3).getTargetFlags())
14187 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014188 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014189 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014190 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014191 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014192
Dan Gohman14152b42010-07-06 20:24:04 +000014193 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014194 return BB;
14195}
14196
14197MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014198X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14199 MachineBasicBlock *MBB) const {
14200 DebugLoc DL = MI->getDebugLoc();
14201 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14202
14203 MachineFunction *MF = MBB->getParent();
14204 MachineRegisterInfo &MRI = MF->getRegInfo();
14205
14206 const BasicBlock *BB = MBB->getBasicBlock();
14207 MachineFunction::iterator I = MBB;
14208 ++I;
14209
14210 // Memory Reference
14211 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14212 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14213
14214 unsigned DstReg;
14215 unsigned MemOpndSlot = 0;
14216
14217 unsigned CurOp = 0;
14218
14219 DstReg = MI->getOperand(CurOp++).getReg();
14220 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14221 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14222 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14223 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14224
14225 MemOpndSlot = CurOp;
14226
14227 MVT PVT = getPointerTy();
14228 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14229 "Invalid Pointer Size!");
14230
14231 // For v = setjmp(buf), we generate
14232 //
14233 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014234 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014235 // SjLjSetup restoreMBB
14236 //
14237 // mainMBB:
14238 // v_main = 0
14239 //
14240 // sinkMBB:
14241 // v = phi(main, restore)
14242 //
14243 // restoreMBB:
14244 // v_restore = 1
14245
14246 MachineBasicBlock *thisMBB = MBB;
14247 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14248 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14249 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14250 MF->insert(I, mainMBB);
14251 MF->insert(I, sinkMBB);
14252 MF->push_back(restoreMBB);
14253
14254 MachineInstrBuilder MIB;
14255
14256 // Transfer the remainder of BB and its successor edges to sinkMBB.
14257 sinkMBB->splice(sinkMBB->begin(), MBB,
14258 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14259 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14260
14261 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014262 unsigned PtrStoreOpc = 0;
14263 unsigned LabelReg = 0;
14264 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14265 Reloc::Model RM = getTargetMachine().getRelocationModel();
14266 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14267 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014268
Michael Liao281ae5a2012-10-17 02:22:27 +000014269 // Prepare IP either in reg or imm.
14270 if (!UseImmLabel) {
14271 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14272 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14273 LabelReg = MRI.createVirtualRegister(PtrRC);
14274 if (Subtarget->is64Bit()) {
14275 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14276 .addReg(X86::RIP)
14277 .addImm(0)
14278 .addReg(0)
14279 .addMBB(restoreMBB)
14280 .addReg(0);
14281 } else {
14282 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14283 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14284 .addReg(XII->getGlobalBaseReg(MF))
14285 .addImm(0)
14286 .addReg(0)
14287 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14288 .addReg(0);
14289 }
14290 } else
14291 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014292 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014293 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014294 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14295 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014296 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014297 else
14298 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14299 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014300 if (!UseImmLabel)
14301 MIB.addReg(LabelReg);
14302 else
14303 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014304 MIB.setMemRefs(MMOBegin, MMOEnd);
14305 // Setup
14306 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14307 .addMBB(restoreMBB);
14308 MIB.addRegMask(RegInfo->getNoPreservedMask());
14309 thisMBB->addSuccessor(mainMBB);
14310 thisMBB->addSuccessor(restoreMBB);
14311
14312 // mainMBB:
14313 // EAX = 0
14314 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14315 mainMBB->addSuccessor(sinkMBB);
14316
14317 // sinkMBB:
14318 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14319 TII->get(X86::PHI), DstReg)
14320 .addReg(mainDstReg).addMBB(mainMBB)
14321 .addReg(restoreDstReg).addMBB(restoreMBB);
14322
14323 // restoreMBB:
14324 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14325 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14326 restoreMBB->addSuccessor(sinkMBB);
14327
14328 MI->eraseFromParent();
14329 return sinkMBB;
14330}
14331
14332MachineBasicBlock *
14333X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14334 MachineBasicBlock *MBB) const {
14335 DebugLoc DL = MI->getDebugLoc();
14336 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14337
14338 MachineFunction *MF = MBB->getParent();
14339 MachineRegisterInfo &MRI = MF->getRegInfo();
14340
14341 // Memory Reference
14342 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14343 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14344
14345 MVT PVT = getPointerTy();
14346 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14347 "Invalid Pointer Size!");
14348
14349 const TargetRegisterClass *RC =
14350 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14351 unsigned Tmp = MRI.createVirtualRegister(RC);
14352 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14353 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14354 unsigned SP = RegInfo->getStackRegister();
14355
14356 MachineInstrBuilder MIB;
14357
Michael Liao281ae5a2012-10-17 02:22:27 +000014358 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14359 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014360
14361 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14362 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14363
14364 // Reload FP
14365 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14366 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14367 MIB.addOperand(MI->getOperand(i));
14368 MIB.setMemRefs(MMOBegin, MMOEnd);
14369 // Reload IP
14370 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14371 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14372 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014373 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014374 else
14375 MIB.addOperand(MI->getOperand(i));
14376 }
14377 MIB.setMemRefs(MMOBegin, MMOEnd);
14378 // Reload SP
14379 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14380 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14381 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014382 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014383 else
14384 MIB.addOperand(MI->getOperand(i));
14385 }
14386 MIB.setMemRefs(MMOBegin, MMOEnd);
14387 // Jump
14388 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14389
14390 MI->eraseFromParent();
14391 return MBB;
14392}
14393
14394MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014395X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014396 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014397 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014398 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014399 case X86::TAILJMPd64:
14400 case X86::TAILJMPr64:
14401 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014402 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014403 case X86::TCRETURNdi64:
14404 case X86::TCRETURNri64:
14405 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014406 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014407 case X86::WIN_ALLOCA:
14408 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014409 case X86::SEG_ALLOCA_32:
14410 return EmitLoweredSegAlloca(MI, BB, false);
14411 case X86::SEG_ALLOCA_64:
14412 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014413 case X86::TLSCall_32:
14414 case X86::TLSCall_64:
14415 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014416 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014417 case X86::CMOV_FR32:
14418 case X86::CMOV_FR64:
14419 case X86::CMOV_V4F32:
14420 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014421 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014422 case X86::CMOV_V8F32:
14423 case X86::CMOV_V4F64:
14424 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014425 case X86::CMOV_GR16:
14426 case X86::CMOV_GR32:
14427 case X86::CMOV_RFP32:
14428 case X86::CMOV_RFP64:
14429 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014430 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014431
Dale Johannesen849f2142007-07-03 00:53:03 +000014432 case X86::FP32_TO_INT16_IN_MEM:
14433 case X86::FP32_TO_INT32_IN_MEM:
14434 case X86::FP32_TO_INT64_IN_MEM:
14435 case X86::FP64_TO_INT16_IN_MEM:
14436 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014437 case X86::FP64_TO_INT64_IN_MEM:
14438 case X86::FP80_TO_INT16_IN_MEM:
14439 case X86::FP80_TO_INT32_IN_MEM:
14440 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014441 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14442 DebugLoc DL = MI->getDebugLoc();
14443
Evan Cheng60c07e12006-07-05 22:17:51 +000014444 // Change the floating point control register to use "round towards zero"
14445 // mode when truncating to an integer value.
14446 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014447 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014448 addFrameReference(BuildMI(*BB, MI, DL,
14449 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014450
14451 // Load the old value of the high byte of the control word...
14452 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014453 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014454 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014455 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014456
14457 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014458 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014459 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014460
14461 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014462 addFrameReference(BuildMI(*BB, MI, DL,
14463 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014464
14465 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014466 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014467 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014468
14469 // Get the X86 opcode to use.
14470 unsigned Opc;
14471 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014472 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014473 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14474 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14475 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14476 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14477 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14478 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014479 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14480 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14481 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014482 }
14483
14484 X86AddressMode AM;
14485 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014486 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014487 AM.BaseType = X86AddressMode::RegBase;
14488 AM.Base.Reg = Op.getReg();
14489 } else {
14490 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014491 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014492 }
14493 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014494 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014495 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014496 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014497 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014498 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014499 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014500 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014501 AM.GV = Op.getGlobal();
14502 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014503 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014504 }
Dan Gohman14152b42010-07-06 20:24:04 +000014505 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014506 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014507
14508 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014509 addFrameReference(BuildMI(*BB, MI, DL,
14510 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014511
Dan Gohman14152b42010-07-06 20:24:04 +000014512 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014513 return BB;
14514 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014515 // String/text processing lowering.
14516 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014517 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014518 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014519 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014520 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014521 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014522 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014523 case X86::VPCMPESTRM128MEM:
14524 assert(Subtarget->hasSSE42() &&
14525 "Target must have SSE4.2 or AVX features enabled");
14526 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014527
14528 // String/text processing lowering.
14529 case X86::PCMPISTRIREG:
14530 case X86::VPCMPISTRIREG:
14531 case X86::PCMPISTRIMEM:
14532 case X86::VPCMPISTRIMEM:
14533 case X86::PCMPESTRIREG:
14534 case X86::VPCMPESTRIREG:
14535 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014536 case X86::VPCMPESTRIMEM:
14537 assert(Subtarget->hasSSE42() &&
14538 "Target must have SSE4.2 or AVX features enabled");
14539 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014540
Craig Topper8aae8dd2012-11-10 08:57:41 +000014541 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014542 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014543 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014544
Michael Liaobe02a902012-11-08 07:28:54 +000014545 // xbegin
14546 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014547 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014548
Craig Topper8aae8dd2012-11-10 08:57:41 +000014549 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014550 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014551 case X86::ATOMAND16:
14552 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014553 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014554 // Fall through
14555 case X86::ATOMOR8:
14556 case X86::ATOMOR16:
14557 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014558 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014559 // Fall through
14560 case X86::ATOMXOR16:
14561 case X86::ATOMXOR8:
14562 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014563 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014564 // Fall through
14565 case X86::ATOMNAND8:
14566 case X86::ATOMNAND16:
14567 case X86::ATOMNAND32:
14568 case X86::ATOMNAND64:
14569 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014570 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014571 case X86::ATOMMAX16:
14572 case X86::ATOMMAX32:
14573 case X86::ATOMMAX64:
14574 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014575 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014576 case X86::ATOMMIN16:
14577 case X86::ATOMMIN32:
14578 case X86::ATOMMIN64:
14579 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014580 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014581 case X86::ATOMUMAX16:
14582 case X86::ATOMUMAX32:
14583 case X86::ATOMUMAX64:
14584 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014585 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014586 case X86::ATOMUMIN16:
14587 case X86::ATOMUMIN32:
14588 case X86::ATOMUMIN64:
14589 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014590
14591 // This group does 64-bit operations on a 32-bit host.
14592 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014593 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014594 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014595 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014596 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014597 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014598 case X86::ATOMMAX6432:
14599 case X86::ATOMMIN6432:
14600 case X86::ATOMUMAX6432:
14601 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014602 case X86::ATOMSWAP6432:
14603 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014604
Dan Gohmand6708ea2009-08-15 01:38:56 +000014605 case X86::VASTART_SAVE_XMM_REGS:
14606 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014607
14608 case X86::VAARG_64:
14609 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014610
14611 case X86::EH_SjLj_SetJmp32:
14612 case X86::EH_SjLj_SetJmp64:
14613 return emitEHSjLjSetJmp(MI, BB);
14614
14615 case X86::EH_SjLj_LongJmp32:
14616 case X86::EH_SjLj_LongJmp64:
14617 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014618 }
14619}
14620
14621//===----------------------------------------------------------------------===//
14622// X86 Optimization Hooks
14623//===----------------------------------------------------------------------===//
14624
Dan Gohman475871a2008-07-27 21:46:04 +000014625void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014626 APInt &KnownZero,
14627 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014628 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014629 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014630 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014631 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014632 assert((Opc >= ISD::BUILTIN_OP_END ||
14633 Opc == ISD::INTRINSIC_WO_CHAIN ||
14634 Opc == ISD::INTRINSIC_W_CHAIN ||
14635 Opc == ISD::INTRINSIC_VOID) &&
14636 "Should use MaskedValueIsZero if you don't know whether Op"
14637 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014638
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014639 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014640 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014641 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014642 case X86ISD::ADD:
14643 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014644 case X86ISD::ADC:
14645 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014646 case X86ISD::SMUL:
14647 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014648 case X86ISD::INC:
14649 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014650 case X86ISD::OR:
14651 case X86ISD::XOR:
14652 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014653 // These nodes' second result is a boolean.
14654 if (Op.getResNo() == 0)
14655 break;
14656 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014657 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014658 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014659 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014660 case ISD::INTRINSIC_WO_CHAIN: {
14661 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14662 unsigned NumLoBits = 0;
14663 switch (IntId) {
14664 default: break;
14665 case Intrinsic::x86_sse_movmsk_ps:
14666 case Intrinsic::x86_avx_movmsk_ps_256:
14667 case Intrinsic::x86_sse2_movmsk_pd:
14668 case Intrinsic::x86_avx_movmsk_pd_256:
14669 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014670 case Intrinsic::x86_sse2_pmovmskb_128:
14671 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014672 // High bits of movmskp{s|d}, pmovmskb are known zero.
14673 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014674 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014675 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14676 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14677 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14678 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14679 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14680 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014681 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014682 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014683 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014684 break;
14685 }
14686 }
14687 break;
14688 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014689 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014690}
Chris Lattner259e97c2006-01-31 19:43:35 +000014691
Owen Andersonbc146b02010-09-21 20:42:50 +000014692unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14693 unsigned Depth) const {
14694 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14695 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14696 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014697
Owen Andersonbc146b02010-09-21 20:42:50 +000014698 // Fallback case.
14699 return 1;
14700}
14701
Evan Cheng206ee9d2006-07-07 08:33:52 +000014702/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014703/// node is a GlobalAddress + offset.
14704bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014705 const GlobalValue* &GA,
14706 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014707 if (N->getOpcode() == X86ISD::Wrapper) {
14708 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014709 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014710 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014711 return true;
14712 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014713 }
Evan Chengad4196b2008-05-12 19:56:52 +000014714 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014715}
14716
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014717/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14718/// same as extracting the high 128-bit part of 256-bit vector and then
14719/// inserting the result into the low part of a new 256-bit vector
14720static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14721 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014722 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014723
14724 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014725 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014726 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14727 SVOp->getMaskElt(j) >= 0)
14728 return false;
14729
14730 return true;
14731}
14732
14733/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14734/// same as extracting the low 128-bit part of 256-bit vector and then
14735/// inserting the result into the high part of a new 256-bit vector
14736static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14737 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014738 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014739
14740 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014741 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014742 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14743 SVOp->getMaskElt(j) >= 0)
14744 return false;
14745
14746 return true;
14747}
14748
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014749/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14750static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014751 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014752 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014753 DebugLoc dl = N->getDebugLoc();
14754 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14755 SDValue V1 = SVOp->getOperand(0);
14756 SDValue V2 = SVOp->getOperand(1);
14757 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014758 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014759
14760 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14761 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14762 //
14763 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014764 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014765 // V UNDEF BUILD_VECTOR UNDEF
14766 // \ / \ /
14767 // CONCAT_VECTOR CONCAT_VECTOR
14768 // \ /
14769 // \ /
14770 // RESULT: V + zero extended
14771 //
14772 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14773 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14774 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14775 return SDValue();
14776
14777 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14778 return SDValue();
14779
14780 // To match the shuffle mask, the first half of the mask should
14781 // be exactly the first vector, and all the rest a splat with the
14782 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014783 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014784 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14785 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14786 return SDValue();
14787
Chad Rosier3d1161e2012-01-03 21:05:52 +000014788 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14789 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014790 if (Ld->hasNUsesOfValue(1, 0)) {
14791 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14792 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14793 SDValue ResNode =
14794 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14795 Ld->getMemoryVT(),
14796 Ld->getPointerInfo(),
14797 Ld->getAlignment(),
14798 false/*isVolatile*/, true/*ReadMem*/,
14799 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014800
14801 // Make sure the newly-created LOAD is in the same position as Ld in
14802 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14803 // and update uses of Ld's output chain to use the TokenFactor.
14804 if (Ld->hasAnyUseOfValue(1)) {
14805 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14806 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14807 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14808 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14809 SDValue(ResNode.getNode(), 1));
14810 }
14811
Chad Rosier42726832012-05-07 18:47:44 +000014812 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14813 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014814 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014815
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014816 // Emit a zeroed vector and insert the desired subvector on its
14817 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014818 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014819 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014820 return DCI.CombineTo(N, InsV);
14821 }
14822
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014823 //===--------------------------------------------------------------------===//
14824 // Combine some shuffles into subvector extracts and inserts:
14825 //
14826
14827 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14828 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014829 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14830 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014831 return DCI.CombineTo(N, InsV);
14832 }
14833
14834 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14835 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014836 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14837 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014838 return DCI.CombineTo(N, InsV);
14839 }
14840
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014841 return SDValue();
14842}
14843
14844/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014845static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014846 TargetLowering::DAGCombinerInfo &DCI,
14847 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014848 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014849 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014850
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014851 // Don't create instructions with illegal types after legalize types has run.
14852 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14853 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14854 return SDValue();
14855
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014856 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014857 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014858 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014859 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014860
14861 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014862 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014863 return SDValue();
14864
14865 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14866 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14867 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014868 SmallVector<SDValue, 16> Elts;
14869 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014870 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014871
Nate Begemanfdea31a2010-03-24 20:49:50 +000014872 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014873}
Evan Chengd880b972008-05-09 21:53:03 +000014874
Nadav Roteme12bf182013-01-04 17:35:21 +000014875/// PerformTruncateCombine - Converts truncate operation to
14876/// a sequence of vector shuffle operations.
14877/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014878static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14879 TargetLowering::DAGCombinerInfo &DCI,
14880 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014881 return SDValue();
14882}
14883
Craig Topper89f4e662012-03-20 07:17:59 +000014884/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14885/// specific shuffle of a load can be folded into a single element load.
14886/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14887/// shuffles have been customed lowered so we need to handle those here.
14888static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14889 TargetLowering::DAGCombinerInfo &DCI) {
14890 if (DCI.isBeforeLegalizeOps())
14891 return SDValue();
14892
14893 SDValue InVec = N->getOperand(0);
14894 SDValue EltNo = N->getOperand(1);
14895
14896 if (!isa<ConstantSDNode>(EltNo))
14897 return SDValue();
14898
14899 EVT VT = InVec.getValueType();
14900
14901 bool HasShuffleIntoBitcast = false;
14902 if (InVec.getOpcode() == ISD::BITCAST) {
14903 // Don't duplicate a load with other uses.
14904 if (!InVec.hasOneUse())
14905 return SDValue();
14906 EVT BCVT = InVec.getOperand(0).getValueType();
14907 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14908 return SDValue();
14909 InVec = InVec.getOperand(0);
14910 HasShuffleIntoBitcast = true;
14911 }
14912
14913 if (!isTargetShuffle(InVec.getOpcode()))
14914 return SDValue();
14915
14916 // Don't duplicate a load with other uses.
14917 if (!InVec.hasOneUse())
14918 return SDValue();
14919
14920 SmallVector<int, 16> ShuffleMask;
14921 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014922 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14923 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014924 return SDValue();
14925
14926 // Select the input vector, guarding against out of range extract vector.
14927 unsigned NumElems = VT.getVectorNumElements();
14928 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14929 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14930 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14931 : InVec.getOperand(1);
14932
14933 // If inputs to shuffle are the same for both ops, then allow 2 uses
14934 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14935
14936 if (LdNode.getOpcode() == ISD::BITCAST) {
14937 // Don't duplicate a load with other uses.
14938 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14939 return SDValue();
14940
14941 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14942 LdNode = LdNode.getOperand(0);
14943 }
14944
14945 if (!ISD::isNormalLoad(LdNode.getNode()))
14946 return SDValue();
14947
14948 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14949
14950 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14951 return SDValue();
14952
14953 if (HasShuffleIntoBitcast) {
14954 // If there's a bitcast before the shuffle, check if the load type and
14955 // alignment is valid.
14956 unsigned Align = LN0->getAlignment();
14957 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014958 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014959 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14960
14961 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14962 return SDValue();
14963 }
14964
14965 // All checks match so transform back to vector_shuffle so that DAG combiner
14966 // can finish the job
14967 DebugLoc dl = N->getDebugLoc();
14968
14969 // Create shuffle node taking into account the case that its a unary shuffle
14970 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14971 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14972 InVec.getOperand(0), Shuffle,
14973 &ShuffleMask[0]);
14974 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14975 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14976 EltNo);
14977}
14978
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014979/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14980/// generation and convert it from being a bunch of shuffles and extracts
14981/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014982static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014983 TargetLowering::DAGCombinerInfo &DCI) {
14984 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14985 if (NewOp.getNode())
14986 return NewOp;
14987
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014988 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014989 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14990 // from mmx to v2i32 has a single usage.
14991 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14992 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14993 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14994 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14995 N->getValueType(0),
14996 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014997
14998 // Only operate on vectors of 4 elements, where the alternative shuffling
14999 // gets to be more expensive.
15000 if (InputVector.getValueType() != MVT::v4i32)
15001 return SDValue();
15002
15003 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15004 // single use which is a sign-extend or zero-extend, and all elements are
15005 // used.
15006 SmallVector<SDNode *, 4> Uses;
15007 unsigned ExtractedElements = 0;
15008 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15009 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15010 if (UI.getUse().getResNo() != InputVector.getResNo())
15011 return SDValue();
15012
15013 SDNode *Extract = *UI;
15014 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15015 return SDValue();
15016
15017 if (Extract->getValueType(0) != MVT::i32)
15018 return SDValue();
15019 if (!Extract->hasOneUse())
15020 return SDValue();
15021 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15022 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15023 return SDValue();
15024 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15025 return SDValue();
15026
15027 // Record which element was extracted.
15028 ExtractedElements |=
15029 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15030
15031 Uses.push_back(Extract);
15032 }
15033
15034 // If not all the elements were used, this may not be worthwhile.
15035 if (ExtractedElements != 15)
15036 return SDValue();
15037
15038 // Ok, we've now decided to do the transformation.
15039 DebugLoc dl = InputVector.getDebugLoc();
15040
15041 // Store the value to a temporary stack slot.
15042 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015043 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15044 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015045
15046 // Replace each use (extract) with a load of the appropriate element.
15047 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15048 UE = Uses.end(); UI != UE; ++UI) {
15049 SDNode *Extract = *UI;
15050
Nadav Rotem86694292011-05-17 08:31:57 +000015051 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015052 SDValue Idx = Extract->getOperand(1);
15053 unsigned EltSize =
15054 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15055 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015056 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015057 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15058
Nadav Rotem86694292011-05-17 08:31:57 +000015059 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015060 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015061
15062 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015063 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015064 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015065 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015066
15067 // Replace the exact with the load.
15068 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15069 }
15070
15071 // The replacement was made in place; don't return anything.
15072 return SDValue();
15073}
15074
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015075/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15076static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15077 SDValue RHS, SelectionDAG &DAG,
15078 const X86Subtarget *Subtarget) {
15079 if (!VT.isVector())
15080 return 0;
15081
15082 switch (VT.getSimpleVT().SimpleTy) {
15083 default: return 0;
15084 case MVT::v32i8:
15085 case MVT::v16i16:
15086 case MVT::v8i32:
15087 if (!Subtarget->hasAVX2())
15088 return 0;
15089 case MVT::v16i8:
15090 case MVT::v8i16:
15091 case MVT::v4i32:
15092 if (!Subtarget->hasSSE2())
15093 return 0;
15094 }
15095
15096 // SSE2 has only a small subset of the operations.
15097 bool hasUnsigned = Subtarget->hasSSE41() ||
15098 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15099 bool hasSigned = Subtarget->hasSSE41() ||
15100 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15101
15102 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15103
15104 // Check for x CC y ? x : y.
15105 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15106 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15107 switch (CC) {
15108 default: break;
15109 case ISD::SETULT:
15110 case ISD::SETULE:
15111 return hasUnsigned ? X86ISD::UMIN : 0;
15112 case ISD::SETUGT:
15113 case ISD::SETUGE:
15114 return hasUnsigned ? X86ISD::UMAX : 0;
15115 case ISD::SETLT:
15116 case ISD::SETLE:
15117 return hasSigned ? X86ISD::SMIN : 0;
15118 case ISD::SETGT:
15119 case ISD::SETGE:
15120 return hasSigned ? X86ISD::SMAX : 0;
15121 }
15122 // Check for x CC y ? y : x -- a min/max with reversed arms.
15123 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15124 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15125 switch (CC) {
15126 default: break;
15127 case ISD::SETULT:
15128 case ISD::SETULE:
15129 return hasUnsigned ? X86ISD::UMAX : 0;
15130 case ISD::SETUGT:
15131 case ISD::SETUGE:
15132 return hasUnsigned ? X86ISD::UMIN : 0;
15133 case ISD::SETLT:
15134 case ISD::SETLE:
15135 return hasSigned ? X86ISD::SMAX : 0;
15136 case ISD::SETGT:
15137 case ISD::SETGE:
15138 return hasSigned ? X86ISD::SMIN : 0;
15139 }
15140 }
15141
15142 return 0;
15143}
15144
Duncan Sands6bcd2192011-09-17 16:49:39 +000015145/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15146/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015147static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015148 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015149 const X86Subtarget *Subtarget) {
15150 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015151 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015152 // Get the LHS/RHS of the select.
15153 SDValue LHS = N->getOperand(1);
15154 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015155 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015156
Dan Gohman670e5392009-09-21 18:03:22 +000015157 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015158 // instructions match the semantics of the common C idiom x<y?x:y but not
15159 // x<=y?x:y, because of how they handle negative zero (which can be
15160 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015161 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15162 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015163 (Subtarget->hasSSE2() ||
15164 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015165 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015166
Chris Lattner47b4ce82009-03-11 05:48:52 +000015167 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015168 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015169 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15170 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015171 switch (CC) {
15172 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015173 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015174 // Converting this to a min would handle NaNs incorrectly, and swapping
15175 // the operands would cause it to handle comparisons between positive
15176 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015177 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015178 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015179 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15180 break;
15181 std::swap(LHS, RHS);
15182 }
Dan Gohman670e5392009-09-21 18:03:22 +000015183 Opcode = X86ISD::FMIN;
15184 break;
15185 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015186 // Converting this to a min would handle comparisons between positive
15187 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015188 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015189 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15190 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015191 Opcode = X86ISD::FMIN;
15192 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015193 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015194 // Converting this to a min would handle both negative zeros and NaNs
15195 // incorrectly, but we can swap the operands to fix both.
15196 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015197 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015198 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015199 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015200 Opcode = X86ISD::FMIN;
15201 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015202
Dan Gohman670e5392009-09-21 18:03:22 +000015203 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015204 // Converting this to a max would handle comparisons between positive
15205 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015206 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015207 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015208 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015209 Opcode = X86ISD::FMAX;
15210 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015211 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015212 // Converting this to a max would handle NaNs incorrectly, and swapping
15213 // the operands would cause it to handle comparisons between positive
15214 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015215 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015216 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015217 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15218 break;
15219 std::swap(LHS, RHS);
15220 }
Dan Gohman670e5392009-09-21 18:03:22 +000015221 Opcode = X86ISD::FMAX;
15222 break;
15223 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015224 // Converting this to a max would handle both negative zeros and NaNs
15225 // incorrectly, but we can swap the operands to fix both.
15226 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015227 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015228 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015229 case ISD::SETGE:
15230 Opcode = X86ISD::FMAX;
15231 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015232 }
Dan Gohman670e5392009-09-21 18:03:22 +000015233 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015234 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15235 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015236 switch (CC) {
15237 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015238 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015239 // Converting this to a min would handle comparisons between positive
15240 // and negative zero incorrectly, and swapping the operands would
15241 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015242 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015243 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015244 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015245 break;
15246 std::swap(LHS, RHS);
15247 }
Dan Gohman670e5392009-09-21 18:03:22 +000015248 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015249 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015250 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015251 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015252 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015253 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15254 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015255 Opcode = X86ISD::FMIN;
15256 break;
15257 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015258 // Converting this to a min would handle both negative zeros and NaNs
15259 // incorrectly, but we can swap the operands to fix both.
15260 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015261 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015262 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015263 case ISD::SETGE:
15264 Opcode = X86ISD::FMIN;
15265 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015266
Dan Gohman670e5392009-09-21 18:03:22 +000015267 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015268 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015269 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015270 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015271 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015272 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015273 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015274 // Converting this to a max would handle comparisons between positive
15275 // and negative zero incorrectly, and swapping the operands would
15276 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015277 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015278 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015279 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015280 break;
15281 std::swap(LHS, RHS);
15282 }
Dan Gohman670e5392009-09-21 18:03:22 +000015283 Opcode = X86ISD::FMAX;
15284 break;
15285 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015286 // Converting this to a max would handle both negative zeros and NaNs
15287 // incorrectly, but we can swap the operands to fix both.
15288 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015289 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015290 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015291 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015292 Opcode = X86ISD::FMAX;
15293 break;
15294 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015295 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015296
Chris Lattner47b4ce82009-03-11 05:48:52 +000015297 if (Opcode)
15298 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015299 }
Eric Christopherfd179292009-08-27 18:07:15 +000015300
Chris Lattnerd1980a52009-03-12 06:52:53 +000015301 // If this is a select between two integer constants, try to do some
15302 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015303 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15304 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015305 // Don't do this for crazy integer types.
15306 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15307 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015308 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015309 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015310
Chris Lattnercee56e72009-03-13 05:53:31 +000015311 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015312 // Efficiently invertible.
15313 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15314 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15315 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15316 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015317 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015318 }
Eric Christopherfd179292009-08-27 18:07:15 +000015319
Chris Lattnerd1980a52009-03-12 06:52:53 +000015320 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015321 if (FalseC->getAPIntValue() == 0 &&
15322 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015323 if (NeedsCondInvert) // Invert the condition if needed.
15324 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15325 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015326
Chris Lattnerd1980a52009-03-12 06:52:53 +000015327 // Zero extend the condition if needed.
15328 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015329
Chris Lattnercee56e72009-03-13 05:53:31 +000015330 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015331 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015332 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015333 }
Eric Christopherfd179292009-08-27 18:07:15 +000015334
Chris Lattner97a29a52009-03-13 05:22:11 +000015335 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015336 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015337 if (NeedsCondInvert) // Invert the condition if needed.
15338 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15339 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015340
Chris Lattner97a29a52009-03-13 05:22:11 +000015341 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015342 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15343 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015344 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015345 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015346 }
Eric Christopherfd179292009-08-27 18:07:15 +000015347
Chris Lattnercee56e72009-03-13 05:53:31 +000015348 // Optimize cases that will turn into an LEA instruction. This requires
15349 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015350 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015351 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015352 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015353
Chris Lattnercee56e72009-03-13 05:53:31 +000015354 bool isFastMultiplier = false;
15355 if (Diff < 10) {
15356 switch ((unsigned char)Diff) {
15357 default: break;
15358 case 1: // result = add base, cond
15359 case 2: // result = lea base( , cond*2)
15360 case 3: // result = lea base(cond, cond*2)
15361 case 4: // result = lea base( , cond*4)
15362 case 5: // result = lea base(cond, cond*4)
15363 case 8: // result = lea base( , cond*8)
15364 case 9: // result = lea base(cond, cond*8)
15365 isFastMultiplier = true;
15366 break;
15367 }
15368 }
Eric Christopherfd179292009-08-27 18:07:15 +000015369
Chris Lattnercee56e72009-03-13 05:53:31 +000015370 if (isFastMultiplier) {
15371 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15372 if (NeedsCondInvert) // Invert the condition if needed.
15373 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15374 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015375
Chris Lattnercee56e72009-03-13 05:53:31 +000015376 // Zero extend the condition if needed.
15377 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15378 Cond);
15379 // Scale the condition by the difference.
15380 if (Diff != 1)
15381 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15382 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015383
Chris Lattnercee56e72009-03-13 05:53:31 +000015384 // Add the base if non-zero.
15385 if (FalseC->getAPIntValue() != 0)
15386 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15387 SDValue(FalseC, 0));
15388 return Cond;
15389 }
Eric Christopherfd179292009-08-27 18:07:15 +000015390 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015391 }
15392 }
Eric Christopherfd179292009-08-27 18:07:15 +000015393
Evan Cheng56f582d2012-01-04 01:41:39 +000015394 // Canonicalize max and min:
15395 // (x > y) ? x : y -> (x >= y) ? x : y
15396 // (x < y) ? x : y -> (x <= y) ? x : y
15397 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15398 // the need for an extra compare
15399 // against zero. e.g.
15400 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15401 // subl %esi, %edi
15402 // testl %edi, %edi
15403 // movl $0, %eax
15404 // cmovgl %edi, %eax
15405 // =>
15406 // xorl %eax, %eax
15407 // subl %esi, $edi
15408 // cmovsl %eax, %edi
15409 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15410 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15411 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15412 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15413 switch (CC) {
15414 default: break;
15415 case ISD::SETLT:
15416 case ISD::SETGT: {
15417 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15418 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15419 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15420 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15421 }
15422 }
15423 }
15424
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015425 // Match VSELECTs into subs with unsigned saturation.
15426 if (!DCI.isBeforeLegalize() &&
15427 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15428 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15429 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15430 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15431 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15432
15433 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15434 // left side invert the predicate to simplify logic below.
15435 SDValue Other;
15436 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15437 Other = RHS;
15438 CC = ISD::getSetCCInverse(CC, true);
15439 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15440 Other = LHS;
15441 }
15442
15443 if (Other.getNode() && Other->getNumOperands() == 2 &&
15444 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15445 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15446 SDValue CondRHS = Cond->getOperand(1);
15447
15448 // Look for a general sub with unsigned saturation first.
15449 // x >= y ? x-y : 0 --> subus x, y
15450 // x > y ? x-y : 0 --> subus x, y
15451 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15452 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15453 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15454
15455 // If the RHS is a constant we have to reverse the const canonicalization.
15456 // x > C-1 ? x+-C : 0 --> subus x, C
15457 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15458 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15459 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015460 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015461 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015462 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015463 }
15464
15465 // Another special case: If C was a sign bit, the sub has been
15466 // canonicalized into a xor.
15467 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15468 // it's safe to decanonicalize the xor?
15469 // x s< 0 ? x^C : 0 --> subus x, C
15470 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15471 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15472 isSplatVector(OpRHS.getNode())) {
15473 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15474 if (A.isSignBit())
15475 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15476 }
15477 }
15478 }
15479
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015480 // Try to match a min/max vector operation.
15481 if (!DCI.isBeforeLegalize() &&
15482 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15483 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15484 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15485
Nadav Rotemcc616562012-01-15 19:27:55 +000015486 // If we know that this node is legal then we know that it is going to be
15487 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15488 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15489 // to simplify previous instructions.
15490 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15491 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015492 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015493 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015494
15495 // Don't optimize vector selects that map to mask-registers.
15496 if (BitWidth == 1)
15497 return SDValue();
15498
Nadav Rotemcc616562012-01-15 19:27:55 +000015499 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15500 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15501
15502 APInt KnownZero, KnownOne;
15503 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15504 DCI.isBeforeLegalizeOps());
15505 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15506 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15507 DCI.CommitTargetLoweringOpt(TLO);
15508 }
15509
Dan Gohman475871a2008-07-27 21:46:04 +000015510 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015511}
15512
Michael Liao2a33cec2012-08-10 19:58:13 +000015513// Check whether a boolean test is testing a boolean value generated by
15514// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15515// code.
15516//
15517// Simplify the following patterns:
15518// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15519// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15520// to (Op EFLAGS Cond)
15521//
15522// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15523// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15524// to (Op EFLAGS !Cond)
15525//
15526// where Op could be BRCOND or CMOV.
15527//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015528static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015529 // Quit if not CMP and SUB with its value result used.
15530 if (Cmp.getOpcode() != X86ISD::CMP &&
15531 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15532 return SDValue();
15533
15534 // Quit if not used as a boolean value.
15535 if (CC != X86::COND_E && CC != X86::COND_NE)
15536 return SDValue();
15537
15538 // Check CMP operands. One of them should be 0 or 1 and the other should be
15539 // an SetCC or extended from it.
15540 SDValue Op1 = Cmp.getOperand(0);
15541 SDValue Op2 = Cmp.getOperand(1);
15542
15543 SDValue SetCC;
15544 const ConstantSDNode* C = 0;
15545 bool needOppositeCond = (CC == X86::COND_E);
15546
15547 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15548 SetCC = Op2;
15549 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15550 SetCC = Op1;
15551 else // Quit if all operands are not constants.
15552 return SDValue();
15553
15554 if (C->getZExtValue() == 1)
15555 needOppositeCond = !needOppositeCond;
15556 else if (C->getZExtValue() != 0)
15557 // Quit if the constant is neither 0 or 1.
15558 return SDValue();
15559
15560 // Skip 'zext' node.
15561 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15562 SetCC = SetCC.getOperand(0);
15563
Michael Liao7fdc66b2012-09-10 16:36:16 +000015564 switch (SetCC.getOpcode()) {
15565 case X86ISD::SETCC:
15566 // Set the condition code or opposite one if necessary.
15567 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15568 if (needOppositeCond)
15569 CC = X86::GetOppositeBranchCondition(CC);
15570 return SetCC.getOperand(1);
15571 case X86ISD::CMOV: {
15572 // Check whether false/true value has canonical one, i.e. 0 or 1.
15573 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15574 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15575 // Quit if true value is not a constant.
15576 if (!TVal)
15577 return SDValue();
15578 // Quit if false value is not a constant.
15579 if (!FVal) {
15580 // A special case for rdrand, where 0 is set if false cond is found.
15581 SDValue Op = SetCC.getOperand(0);
15582 if (Op.getOpcode() != X86ISD::RDRAND)
15583 return SDValue();
15584 }
15585 // Quit if false value is not the constant 0 or 1.
15586 bool FValIsFalse = true;
15587 if (FVal && FVal->getZExtValue() != 0) {
15588 if (FVal->getZExtValue() != 1)
15589 return SDValue();
15590 // If FVal is 1, opposite cond is needed.
15591 needOppositeCond = !needOppositeCond;
15592 FValIsFalse = false;
15593 }
15594 // Quit if TVal is not the constant opposite of FVal.
15595 if (FValIsFalse && TVal->getZExtValue() != 1)
15596 return SDValue();
15597 if (!FValIsFalse && TVal->getZExtValue() != 0)
15598 return SDValue();
15599 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15600 if (needOppositeCond)
15601 CC = X86::GetOppositeBranchCondition(CC);
15602 return SetCC.getOperand(3);
15603 }
15604 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015605
Michael Liao7fdc66b2012-09-10 16:36:16 +000015606 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015607}
15608
Chris Lattnerd1980a52009-03-12 06:52:53 +000015609/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15610static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015611 TargetLowering::DAGCombinerInfo &DCI,
15612 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015613 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015614
Chris Lattnerd1980a52009-03-12 06:52:53 +000015615 // If the flag operand isn't dead, don't touch this CMOV.
15616 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15617 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015618
Evan Chengb5a55d92011-05-24 01:48:22 +000015619 SDValue FalseOp = N->getOperand(0);
15620 SDValue TrueOp = N->getOperand(1);
15621 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15622 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015623
Evan Chengb5a55d92011-05-24 01:48:22 +000015624 if (CC == X86::COND_E || CC == X86::COND_NE) {
15625 switch (Cond.getOpcode()) {
15626 default: break;
15627 case X86ISD::BSR:
15628 case X86ISD::BSF:
15629 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15630 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15631 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15632 }
15633 }
15634
Michael Liao2a33cec2012-08-10 19:58:13 +000015635 SDValue Flags;
15636
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015637 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015638 if (Flags.getNode() &&
15639 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015640 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015641 SDValue Ops[] = { FalseOp, TrueOp,
15642 DAG.getConstant(CC, MVT::i8), Flags };
15643 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15644 Ops, array_lengthof(Ops));
15645 }
15646
Chris Lattnerd1980a52009-03-12 06:52:53 +000015647 // If this is a select between two integer constants, try to do some
15648 // optimizations. Note that the operands are ordered the opposite of SELECT
15649 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015650 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15651 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015652 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15653 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015654 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15655 CC = X86::GetOppositeBranchCondition(CC);
15656 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015657 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015658 }
Eric Christopherfd179292009-08-27 18:07:15 +000015659
Chris Lattnerd1980a52009-03-12 06:52:53 +000015660 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015661 // This is efficient for any integer data type (including i8/i16) and
15662 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015663 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015664 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15665 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015666
Chris Lattnerd1980a52009-03-12 06:52:53 +000015667 // Zero extend the condition if needed.
15668 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015669
Chris Lattnerd1980a52009-03-12 06:52:53 +000015670 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15671 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015672 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015673 if (N->getNumValues() == 2) // Dead flag value?
15674 return DCI.CombineTo(N, Cond, SDValue());
15675 return Cond;
15676 }
Eric Christopherfd179292009-08-27 18:07:15 +000015677
Chris Lattnercee56e72009-03-13 05:53:31 +000015678 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15679 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015680 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015681 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15682 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015683
Chris Lattner97a29a52009-03-13 05:22:11 +000015684 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015685 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15686 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015687 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15688 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015689
Chris Lattner97a29a52009-03-13 05:22:11 +000015690 if (N->getNumValues() == 2) // Dead flag value?
15691 return DCI.CombineTo(N, Cond, SDValue());
15692 return Cond;
15693 }
Eric Christopherfd179292009-08-27 18:07:15 +000015694
Chris Lattnercee56e72009-03-13 05:53:31 +000015695 // Optimize cases that will turn into an LEA instruction. This requires
15696 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015697 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015698 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015699 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015700
Chris Lattnercee56e72009-03-13 05:53:31 +000015701 bool isFastMultiplier = false;
15702 if (Diff < 10) {
15703 switch ((unsigned char)Diff) {
15704 default: break;
15705 case 1: // result = add base, cond
15706 case 2: // result = lea base( , cond*2)
15707 case 3: // result = lea base(cond, cond*2)
15708 case 4: // result = lea base( , cond*4)
15709 case 5: // result = lea base(cond, cond*4)
15710 case 8: // result = lea base( , cond*8)
15711 case 9: // result = lea base(cond, cond*8)
15712 isFastMultiplier = true;
15713 break;
15714 }
15715 }
Eric Christopherfd179292009-08-27 18:07:15 +000015716
Chris Lattnercee56e72009-03-13 05:53:31 +000015717 if (isFastMultiplier) {
15718 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015719 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15720 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015721 // Zero extend the condition if needed.
15722 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15723 Cond);
15724 // Scale the condition by the difference.
15725 if (Diff != 1)
15726 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15727 DAG.getConstant(Diff, Cond.getValueType()));
15728
15729 // Add the base if non-zero.
15730 if (FalseC->getAPIntValue() != 0)
15731 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15732 SDValue(FalseC, 0));
15733 if (N->getNumValues() == 2) // Dead flag value?
15734 return DCI.CombineTo(N, Cond, SDValue());
15735 return Cond;
15736 }
Eric Christopherfd179292009-08-27 18:07:15 +000015737 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015738 }
15739 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015740
15741 // Handle these cases:
15742 // (select (x != c), e, c) -> select (x != c), e, x),
15743 // (select (x == c), c, e) -> select (x == c), x, e)
15744 // where the c is an integer constant, and the "select" is the combination
15745 // of CMOV and CMP.
15746 //
15747 // The rationale for this change is that the conditional-move from a constant
15748 // needs two instructions, however, conditional-move from a register needs
15749 // only one instruction.
15750 //
15751 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15752 // some instruction-combining opportunities. This opt needs to be
15753 // postponed as late as possible.
15754 //
15755 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15756 // the DCI.xxxx conditions are provided to postpone the optimization as
15757 // late as possible.
15758
15759 ConstantSDNode *CmpAgainst = 0;
15760 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15761 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000015762 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015763
15764 if (CC == X86::COND_NE &&
15765 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15766 CC = X86::GetOppositeBranchCondition(CC);
15767 std::swap(TrueOp, FalseOp);
15768 }
15769
15770 if (CC == X86::COND_E &&
15771 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15772 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15773 DAG.getConstant(CC, MVT::i8), Cond };
15774 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15775 array_lengthof(Ops));
15776 }
15777 }
15778 }
15779
Chris Lattnerd1980a52009-03-12 06:52:53 +000015780 return SDValue();
15781}
15782
Evan Cheng0b0cd912009-03-28 05:57:29 +000015783/// PerformMulCombine - Optimize a single multiply with constant into two
15784/// in order to implement it with two cheaper instructions, e.g.
15785/// LEA + SHL, LEA + LEA.
15786static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15787 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015788 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15789 return SDValue();
15790
Owen Andersone50ed302009-08-10 22:56:29 +000015791 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015792 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015793 return SDValue();
15794
15795 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15796 if (!C)
15797 return SDValue();
15798 uint64_t MulAmt = C->getZExtValue();
15799 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15800 return SDValue();
15801
15802 uint64_t MulAmt1 = 0;
15803 uint64_t MulAmt2 = 0;
15804 if ((MulAmt % 9) == 0) {
15805 MulAmt1 = 9;
15806 MulAmt2 = MulAmt / 9;
15807 } else if ((MulAmt % 5) == 0) {
15808 MulAmt1 = 5;
15809 MulAmt2 = MulAmt / 5;
15810 } else if ((MulAmt % 3) == 0) {
15811 MulAmt1 = 3;
15812 MulAmt2 = MulAmt / 3;
15813 }
15814 if (MulAmt2 &&
15815 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15816 DebugLoc DL = N->getDebugLoc();
15817
15818 if (isPowerOf2_64(MulAmt2) &&
15819 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15820 // If second multiplifer is pow2, issue it first. We want the multiply by
15821 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15822 // is an add.
15823 std::swap(MulAmt1, MulAmt2);
15824
15825 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015826 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015827 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015828 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015829 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015830 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015831 DAG.getConstant(MulAmt1, VT));
15832
Eric Christopherfd179292009-08-27 18:07:15 +000015833 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015834 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015835 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015836 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015837 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015838 DAG.getConstant(MulAmt2, VT));
15839
15840 // Do not add new nodes to DAG combiner worklist.
15841 DCI.CombineTo(N, NewMul, false);
15842 }
15843 return SDValue();
15844}
15845
Evan Chengad9c0a32009-12-15 00:53:42 +000015846static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15847 SDValue N0 = N->getOperand(0);
15848 SDValue N1 = N->getOperand(1);
15849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15850 EVT VT = N0.getValueType();
15851
15852 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15853 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015854 if (VT.isInteger() && !VT.isVector() &&
15855 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015856 N0.getOperand(1).getOpcode() == ISD::Constant) {
15857 SDValue N00 = N0.getOperand(0);
15858 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15859 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15860 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15861 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15862 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15863 APInt ShAmt = N1C->getAPIntValue();
15864 Mask = Mask.shl(ShAmt);
15865 if (Mask != 0)
15866 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15867 N00, DAG.getConstant(Mask, VT));
15868 }
15869 }
15870
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015871 // Hardware support for vector shifts is sparse which makes us scalarize the
15872 // vector operations in many cases. Also, on sandybridge ADD is faster than
15873 // shl.
15874 // (shl V, 1) -> add V,V
15875 if (isSplatVector(N1.getNode())) {
15876 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15877 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15878 // We shift all of the values by one. In many cases we do not have
15879 // hardware support for this operation. This is better expressed as an ADD
15880 // of two values.
15881 if (N1C && (1 == N1C->getZExtValue())) {
15882 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15883 }
15884 }
15885
Evan Chengad9c0a32009-12-15 00:53:42 +000015886 return SDValue();
15887}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015888
Nate Begeman740ab032009-01-26 00:52:55 +000015889/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15890/// when possible.
15891static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015892 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015893 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015894 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015895 if (N->getOpcode() == ISD::SHL) {
15896 SDValue V = PerformSHLCombine(N, DAG);
15897 if (V.getNode()) return V;
15898 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015899
Nate Begeman740ab032009-01-26 00:52:55 +000015900 // On X86 with SSE2 support, we can transform this to a vector shift if
15901 // all elements are shifted by the same amount. We can't do this in legalize
15902 // because the a constant vector is typically transformed to a constant pool
15903 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015904 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015905 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015906
Craig Topper7be5dfd2011-11-12 09:58:49 +000015907 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015908 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015909 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015910 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015911
Mon P Wang3becd092009-01-28 08:12:05 +000015912 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015913 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015914 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015915 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015916 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15917 unsigned NumElts = VT.getVectorNumElements();
15918 unsigned i = 0;
15919 for (; i != NumElts; ++i) {
15920 SDValue Arg = ShAmtOp.getOperand(i);
15921 if (Arg.getOpcode() == ISD::UNDEF) continue;
15922 BaseShAmt = Arg;
15923 break;
15924 }
Craig Topper37c26772012-01-17 04:44:50 +000015925 // Handle the case where the build_vector is all undef
15926 // FIXME: Should DAG allow this?
15927 if (i == NumElts)
15928 return SDValue();
15929
Mon P Wang3becd092009-01-28 08:12:05 +000015930 for (; i != NumElts; ++i) {
15931 SDValue Arg = ShAmtOp.getOperand(i);
15932 if (Arg.getOpcode() == ISD::UNDEF) continue;
15933 if (Arg != BaseShAmt) {
15934 return SDValue();
15935 }
15936 }
15937 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015938 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015939 SDValue InVec = ShAmtOp.getOperand(0);
15940 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15941 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15942 unsigned i = 0;
15943 for (; i != NumElts; ++i) {
15944 SDValue Arg = InVec.getOperand(i);
15945 if (Arg.getOpcode() == ISD::UNDEF) continue;
15946 BaseShAmt = Arg;
15947 break;
15948 }
15949 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15950 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015951 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015952 if (C->getZExtValue() == SplatIdx)
15953 BaseShAmt = InVec.getOperand(1);
15954 }
15955 }
Mon P Wang845b1892012-02-01 22:15:20 +000015956 if (BaseShAmt.getNode() == 0) {
15957 // Don't create instructions with illegal types after legalize
15958 // types has run.
15959 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15960 !DCI.isBeforeLegalize())
15961 return SDValue();
15962
Mon P Wangefa42202009-09-03 19:56:25 +000015963 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15964 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015965 }
Mon P Wang3becd092009-01-28 08:12:05 +000015966 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015967 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015968
Mon P Wangefa42202009-09-03 19:56:25 +000015969 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015970 if (EltVT.bitsGT(MVT::i32))
15971 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15972 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015973 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015974
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015975 // The shift amount is identical so we can do a vector shift.
15976 SDValue ValOp = N->getOperand(0);
15977 switch (N->getOpcode()) {
15978 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015979 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015980 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015981 switch (VT.getSimpleVT().SimpleTy) {
15982 default: return SDValue();
15983 case MVT::v2i64:
15984 case MVT::v4i32:
15985 case MVT::v8i16:
15986 case MVT::v4i64:
15987 case MVT::v8i32:
15988 case MVT::v16i16:
15989 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15990 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015991 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015992 switch (VT.getSimpleVT().SimpleTy) {
15993 default: return SDValue();
15994 case MVT::v4i32:
15995 case MVT::v8i16:
15996 case MVT::v8i32:
15997 case MVT::v16i16:
15998 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15999 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016000 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000016001 switch (VT.getSimpleVT().SimpleTy) {
16002 default: return SDValue();
16003 case MVT::v2i64:
16004 case MVT::v4i32:
16005 case MVT::v8i16:
16006 case MVT::v4i64:
16007 case MVT::v8i32:
16008 case MVT::v16i16:
16009 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
16010 }
Nate Begeman740ab032009-01-26 00:52:55 +000016011 }
Nate Begeman740ab032009-01-26 00:52:55 +000016012}
16013
Stuart Hastings865f0932011-06-03 23:53:54 +000016014// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16015// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16016// and friends. Likewise for OR -> CMPNEQSS.
16017static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16018 TargetLowering::DAGCombinerInfo &DCI,
16019 const X86Subtarget *Subtarget) {
16020 unsigned opcode;
16021
16022 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16023 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016024 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016025 SDValue N0 = N->getOperand(0);
16026 SDValue N1 = N->getOperand(1);
16027 SDValue CMP0 = N0->getOperand(1);
16028 SDValue CMP1 = N1->getOperand(1);
16029 DebugLoc DL = N->getDebugLoc();
16030
16031 // The SETCCs should both refer to the same CMP.
16032 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16033 return SDValue();
16034
16035 SDValue CMP00 = CMP0->getOperand(0);
16036 SDValue CMP01 = CMP0->getOperand(1);
16037 EVT VT = CMP00.getValueType();
16038
16039 if (VT == MVT::f32 || VT == MVT::f64) {
16040 bool ExpectingFlags = false;
16041 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016042 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016043 !ExpectingFlags && UI != UE; ++UI)
16044 switch (UI->getOpcode()) {
16045 default:
16046 case ISD::BR_CC:
16047 case ISD::BRCOND:
16048 case ISD::SELECT:
16049 ExpectingFlags = true;
16050 break;
16051 case ISD::CopyToReg:
16052 case ISD::SIGN_EXTEND:
16053 case ISD::ZERO_EXTEND:
16054 case ISD::ANY_EXTEND:
16055 break;
16056 }
16057
16058 if (!ExpectingFlags) {
16059 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16060 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16061
16062 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16063 X86::CondCode tmp = cc0;
16064 cc0 = cc1;
16065 cc1 = tmp;
16066 }
16067
16068 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16069 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16070 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16071 X86ISD::NodeType NTOperator = is64BitFP ?
16072 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16073 // FIXME: need symbolic constants for these magic numbers.
16074 // See X86ATTInstPrinter.cpp:printSSECC().
16075 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16076 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16077 DAG.getConstant(x86cc, MVT::i8));
16078 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16079 OnesOrZeroesF);
16080 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16081 DAG.getConstant(1, MVT::i32));
16082 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16083 return OneBitOfTruth;
16084 }
16085 }
16086 }
16087 }
16088 return SDValue();
16089}
16090
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016091/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16092/// so it can be folded inside ANDNP.
16093static bool CanFoldXORWithAllOnes(const SDNode *N) {
16094 EVT VT = N->getValueType(0);
16095
16096 // Match direct AllOnes for 128 and 256-bit vectors
16097 if (ISD::isBuildVectorAllOnes(N))
16098 return true;
16099
16100 // Look through a bit convert.
16101 if (N->getOpcode() == ISD::BITCAST)
16102 N = N->getOperand(0).getNode();
16103
16104 // Sometimes the operand may come from a insert_subvector building a 256-bit
16105 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016106 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016107 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16108 SDValue V1 = N->getOperand(0);
16109 SDValue V2 = N->getOperand(1);
16110
16111 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16112 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16113 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16114 ISD::isBuildVectorAllOnes(V2.getNode()))
16115 return true;
16116 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016117
16118 return false;
16119}
16120
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016121// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16122// register. In most cases we actually compare or select YMM-sized registers
16123// and mixing the two types creates horrible code. This method optimizes
16124// some of the transition sequences.
16125static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16126 TargetLowering::DAGCombinerInfo &DCI,
16127 const X86Subtarget *Subtarget) {
16128 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016129 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016130 return SDValue();
16131
16132 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16133 N->getOpcode() == ISD::ZERO_EXTEND ||
16134 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16135
16136 SDValue Narrow = N->getOperand(0);
16137 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016138 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016139 return SDValue();
16140
16141 if (Narrow->getOpcode() != ISD::XOR &&
16142 Narrow->getOpcode() != ISD::AND &&
16143 Narrow->getOpcode() != ISD::OR)
16144 return SDValue();
16145
16146 SDValue N0 = Narrow->getOperand(0);
16147 SDValue N1 = Narrow->getOperand(1);
16148 DebugLoc DL = Narrow->getDebugLoc();
16149
16150 // The Left side has to be a trunc.
16151 if (N0.getOpcode() != ISD::TRUNCATE)
16152 return SDValue();
16153
16154 // The type of the truncated inputs.
16155 EVT WideVT = N0->getOperand(0)->getValueType(0);
16156 if (WideVT != VT)
16157 return SDValue();
16158
16159 // The right side has to be a 'trunc' or a constant vector.
16160 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16161 bool RHSConst = (isSplatVector(N1.getNode()) &&
16162 isa<ConstantSDNode>(N1->getOperand(0)));
16163 if (!RHSTrunc && !RHSConst)
16164 return SDValue();
16165
16166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16167
16168 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16169 return SDValue();
16170
16171 // Set N0 and N1 to hold the inputs to the new wide operation.
16172 N0 = N0->getOperand(0);
16173 if (RHSConst) {
16174 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16175 N1->getOperand(0));
16176 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16177 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16178 } else if (RHSTrunc) {
16179 N1 = N1->getOperand(0);
16180 }
16181
16182 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016183 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016184 unsigned Opcode = N->getOpcode();
16185 switch (Opcode) {
16186 case ISD::ANY_EXTEND:
16187 return Op;
16188 case ISD::ZERO_EXTEND: {
16189 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16190 APInt Mask = APInt::getAllOnesValue(InBits);
16191 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16192 return DAG.getNode(ISD::AND, DL, VT,
16193 Op, DAG.getConstant(Mask, VT));
16194 }
16195 case ISD::SIGN_EXTEND:
16196 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16197 Op, DAG.getValueType(NarrowVT));
16198 default:
16199 llvm_unreachable("Unexpected opcode");
16200 }
16201}
16202
Nate Begemanb65c1752010-12-17 22:55:37 +000016203static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16204 TargetLowering::DAGCombinerInfo &DCI,
16205 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016206 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016207 if (DCI.isBeforeLegalizeOps())
16208 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016209
Stuart Hastings865f0932011-06-03 23:53:54 +000016210 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16211 if (R.getNode())
16212 return R;
16213
Craig Topperb926afc2012-12-17 05:12:30 +000016214 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016215 // BLSI is X & (-X)
16216 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016217 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16218 SDValue N0 = N->getOperand(0);
16219 SDValue N1 = N->getOperand(1);
16220 DebugLoc DL = N->getDebugLoc();
16221
Craig Topperb4c94572011-10-21 06:55:01 +000016222 // Check LHS for neg
16223 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16224 isZero(N0.getOperand(0)))
16225 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16226
16227 // Check RHS for neg
16228 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16229 isZero(N1.getOperand(0)))
16230 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16231
16232 // Check LHS for X-1
16233 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16234 isAllOnes(N0.getOperand(1)))
16235 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16236
16237 // Check RHS for X-1
16238 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16239 isAllOnes(N1.getOperand(1)))
16240 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16241
Craig Topper54a11172011-10-14 07:06:56 +000016242 return SDValue();
16243 }
16244
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016245 // Want to form ANDNP nodes:
16246 // 1) In the hopes of then easily combining them with OR and AND nodes
16247 // to form PBLEND/PSIGN.
16248 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016249 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016250 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016251
Nate Begemanb65c1752010-12-17 22:55:37 +000016252 SDValue N0 = N->getOperand(0);
16253 SDValue N1 = N->getOperand(1);
16254 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016255
Nate Begemanb65c1752010-12-17 22:55:37 +000016256 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016257 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016258 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16259 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016260 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016261
16262 // Check RHS for vnot
16263 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016264 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16265 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016266 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016267
Nate Begemanb65c1752010-12-17 22:55:37 +000016268 return SDValue();
16269}
16270
Evan Cheng760d1942010-01-04 21:22:48 +000016271static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016272 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016273 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016274 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016275 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016276 return SDValue();
16277
Stuart Hastings865f0932011-06-03 23:53:54 +000016278 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16279 if (R.getNode())
16280 return R;
16281
Evan Cheng760d1942010-01-04 21:22:48 +000016282 SDValue N0 = N->getOperand(0);
16283 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016284
Nate Begemanb65c1752010-12-17 22:55:37 +000016285 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016286 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016287 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016288 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016289 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016290
Craig Topper1666cb62011-11-19 07:07:26 +000016291 // Canonicalize pandn to RHS
16292 if (N0.getOpcode() == X86ISD::ANDNP)
16293 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016294 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016295 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16296 SDValue Mask = N1.getOperand(0);
16297 SDValue X = N1.getOperand(1);
16298 SDValue Y;
16299 if (N0.getOperand(0) == Mask)
16300 Y = N0.getOperand(1);
16301 if (N0.getOperand(1) == Mask)
16302 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016303
Craig Topper1666cb62011-11-19 07:07:26 +000016304 // Check to see if the mask appeared in both the AND and ANDNP and
16305 if (!Y.getNode())
16306 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016307
Craig Topper1666cb62011-11-19 07:07:26 +000016308 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016309 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016310 if (Mask.getOpcode() == ISD::BITCAST)
16311 Mask = Mask.getOperand(0);
16312 if (X.getOpcode() == ISD::BITCAST)
16313 X = X.getOperand(0);
16314 if (Y.getOpcode() == ISD::BITCAST)
16315 Y = Y.getOperand(0);
16316
Craig Topper1666cb62011-11-19 07:07:26 +000016317 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016318
Craig Toppered2e13d2012-01-22 19:15:14 +000016319 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016320 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16321 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016322 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016323 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016324
16325 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016326 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016327 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16328 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16329 if ((SraAmt + 1) != EltBits)
16330 return SDValue();
16331
16332 DebugLoc DL = N->getDebugLoc();
16333
16334 // Now we know we at least have a plendvb with the mask val. See if
16335 // we can form a psignb/w/d.
16336 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016337 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16338 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016339 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16340 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16341 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016342 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016343 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016344 }
16345 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016346 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016347 return SDValue();
16348
16349 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16350
16351 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16352 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16353 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016354 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016355 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016356 }
16357 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016358
Craig Topper1666cb62011-11-19 07:07:26 +000016359 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16360 return SDValue();
16361
Nate Begemanb65c1752010-12-17 22:55:37 +000016362 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016363 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16364 std::swap(N0, N1);
16365 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16366 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016367 if (!N0.hasOneUse() || !N1.hasOneUse())
16368 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016369
16370 SDValue ShAmt0 = N0.getOperand(1);
16371 if (ShAmt0.getValueType() != MVT::i8)
16372 return SDValue();
16373 SDValue ShAmt1 = N1.getOperand(1);
16374 if (ShAmt1.getValueType() != MVT::i8)
16375 return SDValue();
16376 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16377 ShAmt0 = ShAmt0.getOperand(0);
16378 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16379 ShAmt1 = ShAmt1.getOperand(0);
16380
16381 DebugLoc DL = N->getDebugLoc();
16382 unsigned Opc = X86ISD::SHLD;
16383 SDValue Op0 = N0.getOperand(0);
16384 SDValue Op1 = N1.getOperand(0);
16385 if (ShAmt0.getOpcode() == ISD::SUB) {
16386 Opc = X86ISD::SHRD;
16387 std::swap(Op0, Op1);
16388 std::swap(ShAmt0, ShAmt1);
16389 }
16390
Evan Cheng8b1190a2010-04-28 01:18:01 +000016391 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016392 if (ShAmt1.getOpcode() == ISD::SUB) {
16393 SDValue Sum = ShAmt1.getOperand(0);
16394 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016395 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16396 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16397 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16398 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016399 return DAG.getNode(Opc, DL, VT,
16400 Op0, Op1,
16401 DAG.getNode(ISD::TRUNCATE, DL,
16402 MVT::i8, ShAmt0));
16403 }
16404 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16405 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16406 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016407 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016408 return DAG.getNode(Opc, DL, VT,
16409 N0.getOperand(0), N1.getOperand(0),
16410 DAG.getNode(ISD::TRUNCATE, DL,
16411 MVT::i8, ShAmt0));
16412 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016413
Evan Cheng760d1942010-01-04 21:22:48 +000016414 return SDValue();
16415}
16416
Manman Ren92363622012-06-07 22:39:10 +000016417// Generate NEG and CMOV for integer abs.
16418static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16419 EVT VT = N->getValueType(0);
16420
16421 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16422 // 8-bit integer abs to NEG and CMOV.
16423 if (VT.isInteger() && VT.getSizeInBits() == 8)
16424 return SDValue();
16425
16426 SDValue N0 = N->getOperand(0);
16427 SDValue N1 = N->getOperand(1);
16428 DebugLoc DL = N->getDebugLoc();
16429
16430 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16431 // and change it to SUB and CMOV.
16432 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16433 N0.getOpcode() == ISD::ADD &&
16434 N0.getOperand(1) == N1 &&
16435 N1.getOpcode() == ISD::SRA &&
16436 N1.getOperand(0) == N0.getOperand(0))
16437 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16438 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16439 // Generate SUB & CMOV.
16440 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16441 DAG.getConstant(0, VT), N0.getOperand(0));
16442
16443 SDValue Ops[] = { N0.getOperand(0), Neg,
16444 DAG.getConstant(X86::COND_GE, MVT::i8),
16445 SDValue(Neg.getNode(), 1) };
16446 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16447 Ops, array_lengthof(Ops));
16448 }
16449 return SDValue();
16450}
16451
Craig Topper3738ccd2011-12-27 06:27:23 +000016452// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016453static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16454 TargetLowering::DAGCombinerInfo &DCI,
16455 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016456 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016457 if (DCI.isBeforeLegalizeOps())
16458 return SDValue();
16459
Manman Ren45d53b82012-06-08 18:58:26 +000016460 if (Subtarget->hasCMov()) {
16461 SDValue RV = performIntegerAbsCombine(N, DAG);
16462 if (RV.getNode())
16463 return RV;
16464 }
Manman Ren92363622012-06-07 22:39:10 +000016465
16466 // Try forming BMI if it is available.
16467 if (!Subtarget->hasBMI())
16468 return SDValue();
16469
Craig Topperb4c94572011-10-21 06:55:01 +000016470 if (VT != MVT::i32 && VT != MVT::i64)
16471 return SDValue();
16472
Craig Topper3738ccd2011-12-27 06:27:23 +000016473 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16474
Craig Topperb4c94572011-10-21 06:55:01 +000016475 // Create BLSMSK instructions by finding X ^ (X-1)
16476 SDValue N0 = N->getOperand(0);
16477 SDValue N1 = N->getOperand(1);
16478 DebugLoc DL = N->getDebugLoc();
16479
16480 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16481 isAllOnes(N0.getOperand(1)))
16482 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16483
16484 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16485 isAllOnes(N1.getOperand(1)))
16486 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16487
16488 return SDValue();
16489}
16490
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016491/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16492static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016493 TargetLowering::DAGCombinerInfo &DCI,
16494 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016495 LoadSDNode *Ld = cast<LoadSDNode>(N);
16496 EVT RegVT = Ld->getValueType(0);
16497 EVT MemVT = Ld->getMemoryVT();
16498 DebugLoc dl = Ld->getDebugLoc();
16499 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016500 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016501
16502 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016503 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016504 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016505
16506 // On Sandybridge unaligned 256bit loads are inefficient.
16507 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016508 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016509 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016510 if (NumElems < 2)
16511 return SDValue();
16512
Nadav Rotem48177ac2013-01-18 23:10:30 +000016513 SDValue Ptr = Ld->getBasePtr();
16514 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16515
16516 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16517 NumElems/2);
16518 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16519 Ld->getPointerInfo(), Ld->isVolatile(),
16520 Ld->isNonTemporal(), Ld->isInvariant(),
16521 Alignment);
16522 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16523 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16524 Ld->getPointerInfo(), Ld->isVolatile(),
16525 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016526 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016527 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16528 Load1.getValue(1),
16529 Load2.getValue(1));
16530
16531 SDValue NewVec = DAG.getUNDEF(RegVT);
16532 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16533 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16534 return DCI.CombineTo(N, NewVec, TF, true);
16535 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016536
Nadav Rotemca6f2962011-09-18 19:00:23 +000016537 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016538 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16539 // expansion is still better than scalar code.
16540 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16541 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016542 // TODO: It is possible to support ZExt by zeroing the undef values
16543 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016544 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16545 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016546 assert(MemVT != RegVT && "Cannot extend to the same type");
16547 assert(MemVT.isVector() && "Must load a vector from memory");
16548
16549 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016550 unsigned MemSz = MemVT.getSizeInBits();
16551 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016552
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016553 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16554 return SDValue();
16555
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016556 // All sizes must be a power of two.
16557 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16558 return SDValue();
16559
16560 // Attempt to load the original value using scalar loads.
16561 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016562 MVT SclrLoadTy = MVT::i8;
16563 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16564 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16565 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016566 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016567 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016568 }
16569 }
16570
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016571 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16572 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16573 (64 <= MemSz))
16574 SclrLoadTy = MVT::f64;
16575
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016576 // Calculate the number of scalar loads that we need to perform
16577 // in order to load our vector from memory.
16578 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016579 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16580 return SDValue();
16581
16582 unsigned loadRegZize = RegSz;
16583 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16584 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016585
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016586 // Represent our vector as a sequence of elements which are the
16587 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016588 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016589 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016590
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016591 // Represent the data using the same element type that is stored in
16592 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016593 EVT WideVecVT =
16594 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016595 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016596
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016597 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16598 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016599
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016600 // We can't shuffle using an illegal type.
16601 if (!TLI.isTypeLegal(WideVecVT))
16602 return SDValue();
16603
16604 SmallVector<SDValue, 8> Chains;
16605 SDValue Ptr = Ld->getBasePtr();
16606 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16607 TLI.getPointerTy());
16608 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16609
16610 for (unsigned i = 0; i < NumLoads; ++i) {
16611 // Perform a single load.
16612 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16613 Ptr, Ld->getPointerInfo(),
16614 Ld->isVolatile(), Ld->isNonTemporal(),
16615 Ld->isInvariant(), Ld->getAlignment());
16616 Chains.push_back(ScalarLoad.getValue(1));
16617 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16618 // another round of DAGCombining.
16619 if (i == 0)
16620 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16621 else
16622 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16623 ScalarLoad, DAG.getIntPtrConstant(i));
16624
16625 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16626 }
16627
16628 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16629 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016630
16631 // Bitcast the loaded value to a vector of the original element type, in
16632 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016633 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016634 unsigned SizeRatio = RegSz/MemSz;
16635
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016636 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016637 // If we have SSE4.1 we can directly emit a VSEXT node.
16638 if (Subtarget->hasSSE41()) {
16639 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16640 return DCI.CombineTo(N, Sext, TF, true);
16641 }
16642
16643 // Otherwise we'll shuffle the small elements in the high bits of the
16644 // larger type and perform an arithmetic shift. If the shift is not legal
16645 // it's better to scalarize.
16646 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16647 return SDValue();
16648
16649 // Redistribute the loaded elements into the different locations.
16650 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16651 for (unsigned i = 0; i != NumElems; ++i)
16652 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16653
16654 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16655 DAG.getUNDEF(WideVecVT),
16656 &ShuffleVec[0]);
16657
16658 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16659
16660 // Build the arithmetic shift.
16661 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16662 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016663 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16664 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016665
16666 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016667 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016668
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016669 // Redistribute the loaded elements into the different locations.
16670 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016671 for (unsigned i = 0; i != NumElems; ++i)
16672 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016673
16674 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016675 DAG.getUNDEF(WideVecVT),
16676 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016677
16678 // Bitcast to the requested type.
16679 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16680 // Replace the original load with the new sequence
16681 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016682 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016683 }
16684
16685 return SDValue();
16686}
16687
Chris Lattner149a4e52008-02-22 02:09:43 +000016688/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016689static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016690 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016691 StoreSDNode *St = cast<StoreSDNode>(N);
16692 EVT VT = St->getValue().getValueType();
16693 EVT StVT = St->getMemoryVT();
16694 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016695 SDValue StoredVal = St->getOperand(1);
16696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016697 unsigned Alignment = St->getAlignment();
16698 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016699
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016700 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016701 // On Sandy Bridge, 256-bit memory operations are executed by two
16702 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16703 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016704 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016705 StVT == VT && !IsAligned) {
16706 unsigned NumElems = VT.getVectorNumElements();
16707 if (NumElems < 2)
16708 return SDValue();
16709
16710 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16711 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016712
16713 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16714 SDValue Ptr0 = St->getBasePtr();
16715 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16716
16717 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16718 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016719 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016720 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16721 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016722 St->isNonTemporal(),
16723 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016724 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16725 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016726
16727 // Optimize trunc store (of multiple scalars) to shuffle and store.
16728 // First, pack all of the elements in one place. Next, store to memory
16729 // in fewer chunks.
16730 if (St->isTruncatingStore() && VT.isVector()) {
16731 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16732 unsigned NumElems = VT.getVectorNumElements();
16733 assert(StVT != VT && "Cannot truncate to the same type");
16734 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16735 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16736
16737 // From, To sizes and ElemCount must be pow of two
16738 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016739 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016740 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016741 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016742
Nadav Rotem614061b2011-08-10 19:30:14 +000016743 unsigned SizeRatio = FromSz / ToSz;
16744
16745 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16746
16747 // Create a type on which we perform the shuffle
16748 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16749 StVT.getScalarType(), NumElems*SizeRatio);
16750
16751 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16752
16753 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16754 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016755 for (unsigned i = 0; i != NumElems; ++i)
16756 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016757
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016758 // Can't shuffle using an illegal type.
16759 if (!TLI.isTypeLegal(WideVecVT))
16760 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016761
16762 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016763 DAG.getUNDEF(WideVecVT),
16764 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016765 // At this point all of the data is stored at the bottom of the
16766 // register. We now need to save it to mem.
16767
16768 // Find the largest store unit
16769 MVT StoreType = MVT::i8;
16770 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16771 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16772 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016773 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016774 StoreType = Tp;
16775 }
16776
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016777 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16778 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16779 (64 <= NumElems * ToSz))
16780 StoreType = MVT::f64;
16781
Nadav Rotem614061b2011-08-10 19:30:14 +000016782 // Bitcast the original vector into a vector of store-size units
16783 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016784 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016785 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16786 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16787 SmallVector<SDValue, 8> Chains;
16788 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16789 TLI.getPointerTy());
16790 SDValue Ptr = St->getBasePtr();
16791
16792 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016793 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016794 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16795 StoreType, ShuffWide,
16796 DAG.getIntPtrConstant(i));
16797 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16798 St->getPointerInfo(), St->isVolatile(),
16799 St->isNonTemporal(), St->getAlignment());
16800 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16801 Chains.push_back(Ch);
16802 }
16803
16804 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16805 Chains.size());
16806 }
16807
Chris Lattner149a4e52008-02-22 02:09:43 +000016808 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16809 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016810 // A preferable solution to the general problem is to figure out the right
16811 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016812
16813 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016814 if (VT.getSizeInBits() != 64)
16815 return SDValue();
16816
Devang Patel578efa92009-06-05 21:57:13 +000016817 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016818 bool NoImplicitFloatOps = F->getAttributes().
16819 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016820 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016821 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016822 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016823 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016824 isa<LoadSDNode>(St->getValue()) &&
16825 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16826 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016827 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016828 LoadSDNode *Ld = 0;
16829 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016830 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016831 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016832 // Must be a store of a load. We currently handle two cases: the load
16833 // is a direct child, and it's under an intervening TokenFactor. It is
16834 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016835 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016836 Ld = cast<LoadSDNode>(St->getChain());
16837 else if (St->getValue().hasOneUse() &&
16838 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016839 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016840 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016841 TokenFactorIndex = i;
16842 Ld = cast<LoadSDNode>(St->getValue());
16843 } else
16844 Ops.push_back(ChainVal->getOperand(i));
16845 }
16846 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016847
Evan Cheng536e6672009-03-12 05:59:15 +000016848 if (!Ld || !ISD::isNormalLoad(Ld))
16849 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016850
Evan Cheng536e6672009-03-12 05:59:15 +000016851 // If this is not the MMX case, i.e. we are just turning i64 load/store
16852 // into f64 load/store, avoid the transformation if there are multiple
16853 // uses of the loaded value.
16854 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16855 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016856
Evan Cheng536e6672009-03-12 05:59:15 +000016857 DebugLoc LdDL = Ld->getDebugLoc();
16858 DebugLoc StDL = N->getDebugLoc();
16859 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16860 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16861 // pair instead.
16862 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016863 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016864 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16865 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016866 Ld->isNonTemporal(), Ld->isInvariant(),
16867 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016868 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016869 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016870 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016871 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016872 Ops.size());
16873 }
Evan Cheng536e6672009-03-12 05:59:15 +000016874 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016875 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016876 St->isVolatile(), St->isNonTemporal(),
16877 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016878 }
Evan Cheng536e6672009-03-12 05:59:15 +000016879
16880 // Otherwise, lower to two pairs of 32-bit loads / stores.
16881 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016882 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16883 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016884
Owen Anderson825b72b2009-08-11 20:47:22 +000016885 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016886 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016887 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016888 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016889 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016890 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016891 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016892 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016893 MinAlign(Ld->getAlignment(), 4));
16894
16895 SDValue NewChain = LoLd.getValue(1);
16896 if (TokenFactorIndex != -1) {
16897 Ops.push_back(LoLd);
16898 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016899 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016900 Ops.size());
16901 }
16902
16903 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016904 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16905 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016906
16907 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016908 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016909 St->isVolatile(), St->isNonTemporal(),
16910 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016911 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016912 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016913 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016914 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016915 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016916 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016917 }
Dan Gohman475871a2008-07-27 21:46:04 +000016918 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016919}
16920
Duncan Sands17470be2011-09-22 20:15:48 +000016921/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16922/// and return the operands for the horizontal operation in LHS and RHS. A
16923/// horizontal operation performs the binary operation on successive elements
16924/// of its first operand, then on successive elements of its second operand,
16925/// returning the resulting values in a vector. For example, if
16926/// A = < float a0, float a1, float a2, float a3 >
16927/// and
16928/// B = < float b0, float b1, float b2, float b3 >
16929/// then the result of doing a horizontal operation on A and B is
16930/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16931/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16932/// A horizontal-op B, for some already available A and B, and if so then LHS is
16933/// set to A, RHS to B, and the routine returns 'true'.
16934/// Note that the binary operation should have the property that if one of the
16935/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016936static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016937 // Look for the following pattern: if
16938 // A = < float a0, float a1, float a2, float a3 >
16939 // B = < float b0, float b1, float b2, float b3 >
16940 // and
16941 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16942 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16943 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16944 // which is A horizontal-op B.
16945
16946 // At least one of the operands should be a vector shuffle.
16947 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16948 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16949 return false;
16950
16951 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016952
16953 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16954 "Unsupported vector type for horizontal add/sub");
16955
16956 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16957 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016958 unsigned NumElts = VT.getVectorNumElements();
16959 unsigned NumLanes = VT.getSizeInBits()/128;
16960 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016961 assert((NumLaneElts % 2 == 0) &&
16962 "Vector type should have an even number of elements in each lane");
16963 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016964
16965 // View LHS in the form
16966 // LHS = VECTOR_SHUFFLE A, B, LMask
16967 // If LHS is not a shuffle then pretend it is the shuffle
16968 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16969 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16970 // type VT.
16971 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016972 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016973 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16974 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16975 A = LHS.getOperand(0);
16976 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16977 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016978 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16979 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016980 } else {
16981 if (LHS.getOpcode() != ISD::UNDEF)
16982 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016983 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016984 LMask[i] = i;
16985 }
16986
16987 // Likewise, view RHS in the form
16988 // RHS = VECTOR_SHUFFLE C, D, RMask
16989 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016990 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016991 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16992 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16993 C = RHS.getOperand(0);
16994 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16995 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016996 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16997 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016998 } else {
16999 if (RHS.getOpcode() != ISD::UNDEF)
17000 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017001 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017002 RMask[i] = i;
17003 }
17004
17005 // Check that the shuffles are both shuffling the same vectors.
17006 if (!(A == C && B == D) && !(A == D && B == C))
17007 return false;
17008
17009 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17010 if (!A.getNode() && !B.getNode())
17011 return false;
17012
17013 // If A and B occur in reverse order in RHS, then "swap" them (which means
17014 // rewriting the mask).
17015 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017016 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017017
17018 // At this point LHS and RHS are equivalent to
17019 // LHS = VECTOR_SHUFFLE A, B, LMask
17020 // RHS = VECTOR_SHUFFLE A, B, RMask
17021 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017022 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017023 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017024
Craig Topperf8363302011-12-02 08:18:41 +000017025 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017026 if (LIdx < 0 || RIdx < 0 ||
17027 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17028 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017029 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017030
Craig Topperf8363302011-12-02 08:18:41 +000017031 // Check that successive elements are being operated on. If not, this is
17032 // not a horizontal operation.
17033 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17034 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017035 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017036 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017037 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017038 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017039 }
17040
17041 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17042 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17043 return true;
17044}
17045
17046/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17047static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17048 const X86Subtarget *Subtarget) {
17049 EVT VT = N->getValueType(0);
17050 SDValue LHS = N->getOperand(0);
17051 SDValue RHS = N->getOperand(1);
17052
17053 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017054 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017055 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017056 isHorizontalBinOp(LHS, RHS, true))
17057 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17058 return SDValue();
17059}
17060
17061/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17062static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17063 const X86Subtarget *Subtarget) {
17064 EVT VT = N->getValueType(0);
17065 SDValue LHS = N->getOperand(0);
17066 SDValue RHS = N->getOperand(1);
17067
17068 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017069 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017070 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017071 isHorizontalBinOp(LHS, RHS, false))
17072 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17073 return SDValue();
17074}
17075
Chris Lattner6cf73262008-01-25 06:14:17 +000017076/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17077/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017078static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017079 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17080 // F[X]OR(0.0, x) -> x
17081 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017082 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17083 if (C->getValueAPF().isPosZero())
17084 return N->getOperand(1);
17085 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17086 if (C->getValueAPF().isPosZero())
17087 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017088 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017089}
17090
Nadav Rotemd60cb112012-08-19 13:06:16 +000017091/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17092/// X86ISD::FMAX nodes.
17093static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17094 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17095
17096 // Only perform optimizations if UnsafeMath is used.
17097 if (!DAG.getTarget().Options.UnsafeFPMath)
17098 return SDValue();
17099
17100 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017101 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017102 unsigned NewOp = 0;
17103 switch (N->getOpcode()) {
17104 default: llvm_unreachable("unknown opcode");
17105 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17106 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17107 }
17108
17109 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17110 N->getOperand(0), N->getOperand(1));
17111}
17112
Chris Lattneraf723b92008-01-25 05:46:26 +000017113/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017114static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017115 // FAND(0.0, x) -> 0.0
17116 // FAND(x, 0.0) -> 0.0
17117 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17118 if (C->getValueAPF().isPosZero())
17119 return N->getOperand(0);
17120 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17121 if (C->getValueAPF().isPosZero())
17122 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017123 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017124}
17125
Dan Gohmane5af2d32009-01-29 01:59:02 +000017126static SDValue PerformBTCombine(SDNode *N,
17127 SelectionDAG &DAG,
17128 TargetLowering::DAGCombinerInfo &DCI) {
17129 // BT ignores high bits in the bit index operand.
17130 SDValue Op1 = N->getOperand(1);
17131 if (Op1.hasOneUse()) {
17132 unsigned BitWidth = Op1.getValueSizeInBits();
17133 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17134 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017135 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17136 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017137 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017138 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17139 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17140 DCI.CommitTargetLoweringOpt(TLO);
17141 }
17142 return SDValue();
17143}
Chris Lattner83e6c992006-10-04 06:57:07 +000017144
Eli Friedman7a5e5552009-06-07 06:52:44 +000017145static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17146 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017147 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017148 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017149 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017150 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017151 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017152 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017153 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017154 }
17155 return SDValue();
17156}
17157
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017158static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17159 const X86Subtarget *Subtarget) {
17160 EVT VT = N->getValueType(0);
17161 if (!VT.isVector())
17162 return SDValue();
17163
17164 SDValue N0 = N->getOperand(0);
17165 SDValue N1 = N->getOperand(1);
17166 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17167 DebugLoc dl = N->getDebugLoc();
17168
17169 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17170 // both SSE and AVX2 since there is no sign-extended shift right
17171 // operation on a vector with 64-bit elements.
17172 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17173 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17174 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17175 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17176 SDValue N00 = N0.getOperand(0);
17177
17178 // EXTLOAD has a better solution on AVX2,
17179 // it may be replaced with X86ISD::VSEXT node.
17180 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17181 if (!ISD::isNormalLoad(N00.getNode()))
17182 return SDValue();
17183
17184 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17185 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17186 N00, N1);
17187 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17188 }
17189 }
17190 return SDValue();
17191}
17192
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017193static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17194 TargetLowering::DAGCombinerInfo &DCI,
17195 const X86Subtarget *Subtarget) {
17196 if (!DCI.isBeforeLegalizeOps())
17197 return SDValue();
17198
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017199 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017200 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017201
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017202 EVT VT = N->getValueType(0);
17203 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017204 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17205 if (R.getNode())
17206 return R;
17207 }
17208
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017209 return SDValue();
17210}
17211
Michael Liaof6c24ee2012-08-10 14:39:24 +000017212static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017213 const X86Subtarget* Subtarget) {
17214 DebugLoc dl = N->getDebugLoc();
17215 EVT VT = N->getValueType(0);
17216
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017217 // Let legalize expand this if it isn't a legal type yet.
17218 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17219 return SDValue();
17220
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017221 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017222 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17223 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017224 return SDValue();
17225
17226 SDValue A = N->getOperand(0);
17227 SDValue B = N->getOperand(1);
17228 SDValue C = N->getOperand(2);
17229
17230 bool NegA = (A.getOpcode() == ISD::FNEG);
17231 bool NegB = (B.getOpcode() == ISD::FNEG);
17232 bool NegC = (C.getOpcode() == ISD::FNEG);
17233
Michael Liaof6c24ee2012-08-10 14:39:24 +000017234 // Negative multiplication when NegA xor NegB
17235 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017236 if (NegA)
17237 A = A.getOperand(0);
17238 if (NegB)
17239 B = B.getOperand(0);
17240 if (NegC)
17241 C = C.getOperand(0);
17242
17243 unsigned Opcode;
17244 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017245 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017246 else
Craig Topperbf404372012-08-31 15:40:30 +000017247 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17248
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017249 return DAG.getNode(Opcode, dl, VT, A, B, C);
17250}
17251
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017252static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017253 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017254 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017255 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17256 // (and (i32 x86isd::setcc_carry), 1)
17257 // This eliminates the zext. This transformation is necessary because
17258 // ISD::SETCC is always legalized to i8.
17259 DebugLoc dl = N->getDebugLoc();
17260 SDValue N0 = N->getOperand(0);
17261 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017262
Evan Cheng2e489c42009-12-16 00:53:11 +000017263 if (N0.getOpcode() == ISD::AND &&
17264 N0.hasOneUse() &&
17265 N0.getOperand(0).hasOneUse()) {
17266 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017267 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17268 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17269 if (!C || C->getZExtValue() != 1)
17270 return SDValue();
17271 return DAG.getNode(ISD::AND, dl, VT,
17272 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17273 N00.getOperand(0), N00.getOperand(1)),
17274 DAG.getConstant(1, VT));
17275 }
17276 }
17277
Craig Topper5a529e42013-01-18 06:44:29 +000017278 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017279 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17280 if (R.getNode())
17281 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017282 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017283
Evan Cheng2e489c42009-12-16 00:53:11 +000017284 return SDValue();
17285}
17286
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017287// Optimize x == -y --> x+y == 0
17288// x != -y --> x+y != 0
17289static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17290 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17291 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017292 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017293
17294 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17296 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17297 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17298 LHS.getValueType(), RHS, LHS.getOperand(1));
17299 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17300 addV, DAG.getConstant(0, addV.getValueType()), CC);
17301 }
17302 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17303 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17304 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17305 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17306 RHS.getValueType(), LHS, RHS.getOperand(1));
17307 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17308 addV, DAG.getConstant(0, addV.getValueType()), CC);
17309 }
17310 return SDValue();
17311}
17312
Eric Christophere187e252013-01-31 00:50:48 +000017313// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17314// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017315// an all-ones bit which is more useful than 0/1 in some cases.
17316static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17317 return DAG.getNode(ISD::AND, DL, MVT::i8,
17318 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17319 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17320 DAG.getConstant(1, MVT::i8));
17321}
17322
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017323// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017324static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17325 TargetLowering::DAGCombinerInfo &DCI,
17326 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017327 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017328 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17329 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017330
Shuxin Yanga5526a92012-10-31 23:11:48 +000017331 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017332 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017333 // materializing "setb reg".
17334 //
17335 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17336 // cannot take an immediate as its first operand.
17337 //
Eric Christophere187e252013-01-31 00:50:48 +000017338 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017339 EFLAGS.getValueType().isInteger() &&
17340 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17341 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17342 EFLAGS.getNode()->getVTList(),
17343 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17344 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17345 return MaterializeSETB(DL, NewEFLAGS, DAG);
17346 }
17347 }
17348
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017349 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17350 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17351 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017352 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017353 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017354
Michael Liao2a33cec2012-08-10 19:58:13 +000017355 SDValue Flags;
17356
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017357 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17358 if (Flags.getNode()) {
17359 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17360 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17361 }
17362
Michael Liao2a33cec2012-08-10 19:58:13 +000017363 return SDValue();
17364}
17365
17366// Optimize branch condition evaluation.
17367//
17368static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17369 TargetLowering::DAGCombinerInfo &DCI,
17370 const X86Subtarget *Subtarget) {
17371 DebugLoc DL = N->getDebugLoc();
17372 SDValue Chain = N->getOperand(0);
17373 SDValue Dest = N->getOperand(1);
17374 SDValue EFLAGS = N->getOperand(3);
17375 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17376
17377 SDValue Flags;
17378
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017379 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17380 if (Flags.getNode()) {
17381 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17382 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17383 Flags);
17384 }
17385
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017386 return SDValue();
17387}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017388
Benjamin Kramer1396c402011-06-18 11:09:41 +000017389static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17390 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017391 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017392 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017393
17394 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017395 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017396 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017397 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017398 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17399 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17400 }
17401
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017402 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17403 // a 32-bit target where SSE doesn't support i64->FP operations.
17404 if (Op0.getOpcode() == ISD::LOAD) {
17405 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17406 EVT VT = Ld->getValueType(0);
17407 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17408 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17409 !XTLI->getSubtarget()->is64Bit() &&
17410 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017411 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17412 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017413 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17414 return FILDChain;
17415 }
17416 }
17417 return SDValue();
17418}
17419
Chris Lattner23a01992010-12-20 01:37:09 +000017420// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17421static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17422 X86TargetLowering::DAGCombinerInfo &DCI) {
17423 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17424 // the result is either zero or one (depending on the input carry bit).
17425 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17426 if (X86::isZeroNode(N->getOperand(0)) &&
17427 X86::isZeroNode(N->getOperand(1)) &&
17428 // We don't have a good way to replace an EFLAGS use, so only do this when
17429 // dead right now.
17430 SDValue(N, 1).use_empty()) {
17431 DebugLoc DL = N->getDebugLoc();
17432 EVT VT = N->getValueType(0);
17433 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17434 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17435 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17436 DAG.getConstant(X86::COND_B,MVT::i8),
17437 N->getOperand(2)),
17438 DAG.getConstant(1, VT));
17439 return DCI.CombineTo(N, Res1, CarryOut);
17440 }
17441
17442 return SDValue();
17443}
17444
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017445// fold (add Y, (sete X, 0)) -> adc 0, Y
17446// (add Y, (setne X, 0)) -> sbb -1, Y
17447// (sub (sete X, 0), Y) -> sbb 0, Y
17448// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017449static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017450 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017451
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017452 // Look through ZExts.
17453 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17454 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17455 return SDValue();
17456
17457 SDValue SetCC = Ext.getOperand(0);
17458 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17459 return SDValue();
17460
17461 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17462 if (CC != X86::COND_E && CC != X86::COND_NE)
17463 return SDValue();
17464
17465 SDValue Cmp = SetCC.getOperand(1);
17466 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017467 !X86::isZeroNode(Cmp.getOperand(1)) ||
17468 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017469 return SDValue();
17470
17471 SDValue CmpOp0 = Cmp.getOperand(0);
17472 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17473 DAG.getConstant(1, CmpOp0.getValueType()));
17474
17475 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17476 if (CC == X86::COND_NE)
17477 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17478 DL, OtherVal.getValueType(), OtherVal,
17479 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17480 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17481 DL, OtherVal.getValueType(), OtherVal,
17482 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17483}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017484
Craig Topper54f952a2011-11-19 09:02:40 +000017485/// PerformADDCombine - Do target-specific dag combines on integer adds.
17486static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17487 const X86Subtarget *Subtarget) {
17488 EVT VT = N->getValueType(0);
17489 SDValue Op0 = N->getOperand(0);
17490 SDValue Op1 = N->getOperand(1);
17491
17492 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017493 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017494 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017495 isHorizontalBinOp(Op0, Op1, true))
17496 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17497
17498 return OptimizeConditionalInDecrement(N, DAG);
17499}
17500
17501static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17502 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017503 SDValue Op0 = N->getOperand(0);
17504 SDValue Op1 = N->getOperand(1);
17505
17506 // X86 can't encode an immediate LHS of a sub. See if we can push the
17507 // negation into a preceding instruction.
17508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017509 // If the RHS of the sub is a XOR with one use and a constant, invert the
17510 // immediate. Then add one to the LHS of the sub so we can turn
17511 // X-Y -> X+~Y+1, saving one register.
17512 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17513 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017514 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017515 EVT VT = Op0.getValueType();
17516 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17517 Op1.getOperand(0),
17518 DAG.getConstant(~XorC, VT));
17519 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017520 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017521 }
17522 }
17523
Craig Topper54f952a2011-11-19 09:02:40 +000017524 // Try to synthesize horizontal adds from adds of shuffles.
17525 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017526 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017527 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017528 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017529 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17530
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017531 return OptimizeConditionalInDecrement(N, DAG);
17532}
17533
Michael Liaod9d09602012-10-23 17:34:00 +000017534/// performVZEXTCombine - Performs build vector combines
17535static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17536 TargetLowering::DAGCombinerInfo &DCI,
17537 const X86Subtarget *Subtarget) {
17538 // (vzext (bitcast (vzext (x)) -> (vzext x)
17539 SDValue In = N->getOperand(0);
17540 while (In.getOpcode() == ISD::BITCAST)
17541 In = In.getOperand(0);
17542
17543 if (In.getOpcode() != X86ISD::VZEXT)
17544 return SDValue();
17545
Nadav Rotemb39a5522013-02-14 18:20:48 +000017546 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17547 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017548}
17549
Dan Gohman475871a2008-07-27 21:46:04 +000017550SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017551 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017552 SelectionDAG &DAG = DCI.DAG;
17553 switch (N->getOpcode()) {
17554 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017555 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017556 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017557 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017558 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017559 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017560 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17561 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017562 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017563 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017564 case ISD::SHL:
17565 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017566 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017567 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017568 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017569 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017570 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017571 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017572 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017573 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17574 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017575 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017576 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017577 case X86ISD::FMIN:
17578 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017579 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017580 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017581 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017582 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017583 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017584 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017585 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017586 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017587 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017588 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017589 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017590 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017591 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017592 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017593 case X86ISD::UNPCKH:
17594 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017595 case X86ISD::MOVHLPS:
17596 case X86ISD::MOVLHPS:
17597 case X86ISD::PSHUFD:
17598 case X86ISD::PSHUFHW:
17599 case X86ISD::PSHUFLW:
17600 case X86ISD::MOVSS:
17601 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017602 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017603 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017604 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017605 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017606 }
17607
Dan Gohman475871a2008-07-27 21:46:04 +000017608 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017609}
17610
Evan Chenge5b51ac2010-04-17 06:13:15 +000017611/// isTypeDesirableForOp - Return true if the target has native support for
17612/// the specified value type and it is 'desirable' to use the type for the
17613/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17614/// instruction encodings are longer and some i16 instructions are slow.
17615bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17616 if (!isTypeLegal(VT))
17617 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017618 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017619 return true;
17620
17621 switch (Opc) {
17622 default:
17623 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017624 case ISD::LOAD:
17625 case ISD::SIGN_EXTEND:
17626 case ISD::ZERO_EXTEND:
17627 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017628 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017629 case ISD::SRL:
17630 case ISD::SUB:
17631 case ISD::ADD:
17632 case ISD::MUL:
17633 case ISD::AND:
17634 case ISD::OR:
17635 case ISD::XOR:
17636 return false;
17637 }
17638}
17639
17640/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017641/// beneficial for dag combiner to promote the specified node. If true, it
17642/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017643bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017644 EVT VT = Op.getValueType();
17645 if (VT != MVT::i16)
17646 return false;
17647
Evan Cheng4c26e932010-04-19 19:29:22 +000017648 bool Promote = false;
17649 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017650 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017651 default: break;
17652 case ISD::LOAD: {
17653 LoadSDNode *LD = cast<LoadSDNode>(Op);
17654 // If the non-extending load has a single use and it's not live out, then it
17655 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017656 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17657 Op.hasOneUse()*/) {
17658 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17659 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17660 // The only case where we'd want to promote LOAD (rather then it being
17661 // promoted as an operand is when it's only use is liveout.
17662 if (UI->getOpcode() != ISD::CopyToReg)
17663 return false;
17664 }
17665 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017666 Promote = true;
17667 break;
17668 }
17669 case ISD::SIGN_EXTEND:
17670 case ISD::ZERO_EXTEND:
17671 case ISD::ANY_EXTEND:
17672 Promote = true;
17673 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017674 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017675 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017676 SDValue N0 = Op.getOperand(0);
17677 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017678 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017679 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017680 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017681 break;
17682 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017683 case ISD::ADD:
17684 case ISD::MUL:
17685 case ISD::AND:
17686 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017687 case ISD::XOR:
17688 Commute = true;
17689 // fallthrough
17690 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017691 SDValue N0 = Op.getOperand(0);
17692 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017693 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017694 return false;
17695 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017696 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017697 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017698 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017699 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017700 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017701 }
17702 }
17703
17704 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017705 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017706}
17707
Evan Cheng60c07e12006-07-05 22:17:51 +000017708//===----------------------------------------------------------------------===//
17709// X86 Inline Assembly Support
17710//===----------------------------------------------------------------------===//
17711
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017712namespace {
17713 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017714 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017715 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017716
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017717 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017718 StringRef piece(*args[i]);
17719 if (!s.startswith(piece)) // Check if the piece matches.
17720 return false;
17721
17722 s = s.substr(piece.size());
17723 StringRef::size_type pos = s.find_first_not_of(" \t");
17724 if (pos == 0) // We matched a prefix.
17725 return false;
17726
17727 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017728 }
17729
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017730 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017731 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017732 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017733}
17734
Chris Lattnerb8105652009-07-20 17:51:36 +000017735bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17736 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017737
17738 std::string AsmStr = IA->getAsmString();
17739
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017740 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17741 if (!Ty || Ty->getBitWidth() % 16 != 0)
17742 return false;
17743
Chris Lattnerb8105652009-07-20 17:51:36 +000017744 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017745 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017746 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017747
17748 switch (AsmPieces.size()) {
17749 default: return false;
17750 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017751 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017752 // we will turn this bswap into something that will be lowered to logical
17753 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17754 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017755 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017756 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17757 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17758 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17759 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17760 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17761 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017762 // No need to check constraints, nothing other than the equivalent of
17763 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017764 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017765 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017766
Chris Lattnerb8105652009-07-20 17:51:36 +000017767 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017768 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017769 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017770 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17771 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017772 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017773 const std::string &ConstraintsStr = IA->getConstraintString();
17774 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017775 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000017776 if (AsmPieces.size() == 4 &&
17777 AsmPieces[0] == "~{cc}" &&
17778 AsmPieces[1] == "~{dirflag}" &&
17779 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017780 AsmPieces[3] == "~{fpsr}")
17781 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017782 }
17783 break;
17784 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017785 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017786 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017787 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17788 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17789 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017790 AsmPieces.clear();
17791 const std::string &ConstraintsStr = IA->getConstraintString();
17792 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017793 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017794 if (AsmPieces.size() == 4 &&
17795 AsmPieces[0] == "~{cc}" &&
17796 AsmPieces[1] == "~{dirflag}" &&
17797 AsmPieces[2] == "~{flags}" &&
17798 AsmPieces[3] == "~{fpsr}")
17799 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017800 }
Evan Cheng55d42002011-01-08 01:24:27 +000017801
17802 if (CI->getType()->isIntegerTy(64)) {
17803 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17804 if (Constraints.size() >= 2 &&
17805 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17806 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17807 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017808 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17809 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17810 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017811 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017812 }
17813 }
17814 break;
17815 }
17816 return false;
17817}
17818
Chris Lattnerf4dff842006-07-11 02:54:03 +000017819/// getConstraintType - Given a constraint letter, return the type of
17820/// constraint it is for this target.
17821X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017822X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17823 if (Constraint.size() == 1) {
17824 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017825 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017826 case 'q':
17827 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017828 case 'f':
17829 case 't':
17830 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017831 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017832 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017833 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017834 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017835 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017836 case 'a':
17837 case 'b':
17838 case 'c':
17839 case 'd':
17840 case 'S':
17841 case 'D':
17842 case 'A':
17843 return C_Register;
17844 case 'I':
17845 case 'J':
17846 case 'K':
17847 case 'L':
17848 case 'M':
17849 case 'N':
17850 case 'G':
17851 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017852 case 'e':
17853 case 'Z':
17854 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017855 default:
17856 break;
17857 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017858 }
Chris Lattner4234f572007-03-25 02:14:49 +000017859 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017860}
17861
John Thompson44ab89e2010-10-29 17:29:13 +000017862/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017863/// This object must already have been set up with the operand type
17864/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017865TargetLowering::ConstraintWeight
17866 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017867 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017868 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017869 Value *CallOperandVal = info.CallOperandVal;
17870 // If we don't have a value, we can't do a match,
17871 // but allow it at the lowest weight.
17872 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017873 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017874 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017875 // Look at the constraint type.
17876 switch (*constraint) {
17877 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017878 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17879 case 'R':
17880 case 'q':
17881 case 'Q':
17882 case 'a':
17883 case 'b':
17884 case 'c':
17885 case 'd':
17886 case 'S':
17887 case 'D':
17888 case 'A':
17889 if (CallOperandVal->getType()->isIntegerTy())
17890 weight = CW_SpecificReg;
17891 break;
17892 case 'f':
17893 case 't':
17894 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017895 if (type->isFloatingPointTy())
17896 weight = CW_SpecificReg;
17897 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017898 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017899 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17900 weight = CW_SpecificReg;
17901 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017902 case 'x':
17903 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017904 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017905 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017906 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017907 break;
17908 case 'I':
17909 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17910 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017911 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017912 }
17913 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017914 case 'J':
17915 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17916 if (C->getZExtValue() <= 63)
17917 weight = CW_Constant;
17918 }
17919 break;
17920 case 'K':
17921 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17922 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17923 weight = CW_Constant;
17924 }
17925 break;
17926 case 'L':
17927 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17928 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17929 weight = CW_Constant;
17930 }
17931 break;
17932 case 'M':
17933 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17934 if (C->getZExtValue() <= 3)
17935 weight = CW_Constant;
17936 }
17937 break;
17938 case 'N':
17939 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17940 if (C->getZExtValue() <= 0xff)
17941 weight = CW_Constant;
17942 }
17943 break;
17944 case 'G':
17945 case 'C':
17946 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17947 weight = CW_Constant;
17948 }
17949 break;
17950 case 'e':
17951 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17952 if ((C->getSExtValue() >= -0x80000000LL) &&
17953 (C->getSExtValue() <= 0x7fffffffLL))
17954 weight = CW_Constant;
17955 }
17956 break;
17957 case 'Z':
17958 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17959 if (C->getZExtValue() <= 0xffffffff)
17960 weight = CW_Constant;
17961 }
17962 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017963 }
17964 return weight;
17965}
17966
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017967/// LowerXConstraint - try to replace an X constraint, which matches anything,
17968/// with another that has more specific requirements based on the type of the
17969/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017970const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017971LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017972 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17973 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017974 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017975 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017976 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017977 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017978 return "x";
17979 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017980
Chris Lattner5e764232008-04-26 23:02:14 +000017981 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017982}
17983
Chris Lattner48884cd2007-08-25 00:47:38 +000017984/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17985/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017986void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017987 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017988 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017989 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017990 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017991
Eric Christopher100c8332011-06-02 23:16:42 +000017992 // Only support length 1 constraints for now.
17993 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017994
Eric Christopher100c8332011-06-02 23:16:42 +000017995 char ConstraintLetter = Constraint[0];
17996 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017997 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017998 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017999 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018000 if (C->getZExtValue() <= 31) {
18001 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018002 break;
18003 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018004 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018005 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018006 case 'J':
18007 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018008 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018009 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18010 break;
18011 }
18012 }
18013 return;
18014 case 'K':
18015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018016 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018017 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18018 break;
18019 }
18020 }
18021 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018022 case 'N':
18023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018024 if (C->getZExtValue() <= 255) {
18025 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018026 break;
18027 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018028 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018029 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018030 case 'e': {
18031 // 32-bit signed value
18032 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018033 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18034 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018035 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018036 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018037 break;
18038 }
18039 // FIXME gcc accepts some relocatable values here too, but only in certain
18040 // memory models; it's complicated.
18041 }
18042 return;
18043 }
18044 case 'Z': {
18045 // 32-bit unsigned value
18046 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018047 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18048 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018049 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18050 break;
18051 }
18052 }
18053 // FIXME gcc accepts some relocatable values here too, but only in certain
18054 // memory models; it's complicated.
18055 return;
18056 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018057 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018058 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018059 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018060 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018061 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018062 break;
18063 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018064
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018065 // In any sort of PIC mode addresses need to be computed at runtime by
18066 // adding in a register or some sort of table lookup. These can't
18067 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018068 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018069 return;
18070
Chris Lattnerdc43a882007-05-03 16:52:29 +000018071 // If we are in non-pic codegen mode, we allow the address of a global (with
18072 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018073 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018074 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018075
Chris Lattner49921962009-05-08 18:23:14 +000018076 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18077 while (1) {
18078 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18079 Offset += GA->getOffset();
18080 break;
18081 } else if (Op.getOpcode() == ISD::ADD) {
18082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18083 Offset += C->getZExtValue();
18084 Op = Op.getOperand(0);
18085 continue;
18086 }
18087 } else if (Op.getOpcode() == ISD::SUB) {
18088 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18089 Offset += -C->getZExtValue();
18090 Op = Op.getOperand(0);
18091 continue;
18092 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018093 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018094
Chris Lattner49921962009-05-08 18:23:14 +000018095 // Otherwise, this isn't something we can handle, reject it.
18096 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018097 }
Eric Christopherfd179292009-08-27 18:07:15 +000018098
Dan Gohman46510a72010-04-15 01:51:59 +000018099 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018100 // If we require an extra load to get this address, as in PIC mode, we
18101 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018102 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18103 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018104 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018105
Devang Patel0d881da2010-07-06 22:08:15 +000018106 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18107 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018108 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018109 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018110 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018111
Gabor Greifba36cb52008-08-28 21:40:38 +000018112 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018113 Ops.push_back(Result);
18114 return;
18115 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018116 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018117}
18118
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018119std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018120X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018121 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018122 // First, see if this is a constraint that directly corresponds to an LLVM
18123 // register class.
18124 if (Constraint.size() == 1) {
18125 // GCC Constraint Letters
18126 switch (Constraint[0]) {
18127 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018128 // TODO: Slight differences here in allocation order and leaving
18129 // RIP in the class. Do they matter any more here than they do
18130 // in the normal allocation?
18131 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18132 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018133 if (VT == MVT::i32 || VT == MVT::f32)
18134 return std::make_pair(0U, &X86::GR32RegClass);
18135 if (VT == MVT::i16)
18136 return std::make_pair(0U, &X86::GR16RegClass);
18137 if (VT == MVT::i8 || VT == MVT::i1)
18138 return std::make_pair(0U, &X86::GR8RegClass);
18139 if (VT == MVT::i64 || VT == MVT::f64)
18140 return std::make_pair(0U, &X86::GR64RegClass);
18141 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018142 }
18143 // 32-bit fallthrough
18144 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018145 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018146 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18147 if (VT == MVT::i16)
18148 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18149 if (VT == MVT::i8 || VT == MVT::i1)
18150 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18151 if (VT == MVT::i64)
18152 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018153 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018154 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018155 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018156 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018157 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018158 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018159 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018160 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018161 return std::make_pair(0U, &X86::GR32RegClass);
18162 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018163 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018164 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018165 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018166 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018167 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018168 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018169 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18170 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018171 case 'f': // FP Stack registers.
18172 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18173 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018174 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018175 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018176 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018177 return std::make_pair(0U, &X86::RFP64RegClass);
18178 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018179 case 'y': // MMX_REGS if MMX allowed.
18180 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018181 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018182 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018183 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018184 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018185 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018186 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018187
Owen Anderson825b72b2009-08-11 20:47:22 +000018188 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018189 default: break;
18190 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018191 case MVT::f32:
18192 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018193 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018194 case MVT::f64:
18195 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018196 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018197 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018198 case MVT::v16i8:
18199 case MVT::v8i16:
18200 case MVT::v4i32:
18201 case MVT::v2i64:
18202 case MVT::v4f32:
18203 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018204 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018205 // AVX types.
18206 case MVT::v32i8:
18207 case MVT::v16i16:
18208 case MVT::v8i32:
18209 case MVT::v4i64:
18210 case MVT::v8f32:
18211 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018212 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018213 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018214 break;
18215 }
18216 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018217
Chris Lattnerf76d1802006-07-31 23:26:50 +000018218 // Use the default implementation in TargetLowering to convert the register
18219 // constraint into a member of a register class.
18220 std::pair<unsigned, const TargetRegisterClass*> Res;
18221 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018222
18223 // Not found as a standard register?
18224 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018225 // Map st(0) -> st(7) -> ST0
18226 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18227 tolower(Constraint[1]) == 's' &&
18228 tolower(Constraint[2]) == 't' &&
18229 Constraint[3] == '(' &&
18230 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18231 Constraint[5] == ')' &&
18232 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018233
Chris Lattner56d77c72009-09-13 22:41:48 +000018234 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018235 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018236 return Res;
18237 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018238
Chris Lattner56d77c72009-09-13 22:41:48 +000018239 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018240 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018241 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018242 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018243 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018244 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018245
18246 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018247 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018248 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018249 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018250 return Res;
18251 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018252
Dale Johannesen330169f2008-11-13 21:52:36 +000018253 // 'A' means EAX + EDX.
18254 if (Constraint == "A") {
18255 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018256 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018257 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018258 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018259 return Res;
18260 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018261
Chris Lattnerf76d1802006-07-31 23:26:50 +000018262 // Otherwise, check to see if this is a register class of the wrong value
18263 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18264 // turn into {ax},{dx}.
18265 if (Res.second->hasType(VT))
18266 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018267
Chris Lattnerf76d1802006-07-31 23:26:50 +000018268 // All of the single-register GCC register classes map their values onto
18269 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18270 // really want an 8-bit or 32-bit register, map to the appropriate register
18271 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018272 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018273 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018274 unsigned DestReg = 0;
18275 switch (Res.first) {
18276 default: break;
18277 case X86::AX: DestReg = X86::AL; break;
18278 case X86::DX: DestReg = X86::DL; break;
18279 case X86::CX: DestReg = X86::CL; break;
18280 case X86::BX: DestReg = X86::BL; break;
18281 }
18282 if (DestReg) {
18283 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018284 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018285 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018286 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018287 unsigned DestReg = 0;
18288 switch (Res.first) {
18289 default: break;
18290 case X86::AX: DestReg = X86::EAX; break;
18291 case X86::DX: DestReg = X86::EDX; break;
18292 case X86::CX: DestReg = X86::ECX; break;
18293 case X86::BX: DestReg = X86::EBX; break;
18294 case X86::SI: DestReg = X86::ESI; break;
18295 case X86::DI: DestReg = X86::EDI; break;
18296 case X86::BP: DestReg = X86::EBP; break;
18297 case X86::SP: DestReg = X86::ESP; break;
18298 }
18299 if (DestReg) {
18300 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018301 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018302 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018303 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018304 unsigned DestReg = 0;
18305 switch (Res.first) {
18306 default: break;
18307 case X86::AX: DestReg = X86::RAX; break;
18308 case X86::DX: DestReg = X86::RDX; break;
18309 case X86::CX: DestReg = X86::RCX; break;
18310 case X86::BX: DestReg = X86::RBX; break;
18311 case X86::SI: DestReg = X86::RSI; break;
18312 case X86::DI: DestReg = X86::RDI; break;
18313 case X86::BP: DestReg = X86::RBP; break;
18314 case X86::SP: DestReg = X86::RSP; break;
18315 }
18316 if (DestReg) {
18317 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018318 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018319 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018320 }
Craig Topperc9099502012-04-20 06:31:50 +000018321 } else if (Res.second == &X86::FR32RegClass ||
18322 Res.second == &X86::FR64RegClass ||
18323 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018324 // Handle references to XMM physical registers that got mapped into the
18325 // wrong class. This can happen with constraints like {xmm0} where the
18326 // target independent register mapper will just pick the first match it can
18327 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018328
18329 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018330 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018331 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018332 Res.second = &X86::FR64RegClass;
18333 else if (X86::VR128RegClass.hasType(VT))
18334 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018335 else if (X86::VR256RegClass.hasType(VT))
18336 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018337 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018338
Chris Lattnerf76d1802006-07-31 23:26:50 +000018339 return Res;
18340}