blob: 2939bcfb4a8f28025fd7537276c10d397f24a3f7 [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
Benjamin Kramer02c2ecf2013-03-07 18:48:40 +000088 // If the input is a buildvector just emit a smaller one.
89 if (Vec.getOpcode() == ISD::BUILD_VECTOR)
90 return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT,
91 Vec->op_begin()+NormalizedIdxVal, ElemsPerChunk);
92
Craig Topperb8d9da12012-09-06 06:09:01 +000093 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000094 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
95 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000096
Craig Topperb14940a2012-04-22 20:55:18 +000097 return Result;
David Greenea5f26012011-02-07 19:36:54 +000098}
99
100/// Generate a DAG to put 128-bits into a vector > 128 bits. This
101/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000102/// simple superregister reference. Idx is an index in the 128 bits
103/// we want. It need not be aligned to a 128-bit bounday. That makes
104/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000105static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
106 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000107 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000108 // Inserting UNDEF is Result
109 if (Vec.getOpcode() == ISD::UNDEF)
110 return Result;
111
Craig Topperb14940a2012-04-22 20:55:18 +0000112 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000113 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000114
Craig Topperb14940a2012-04-22 20:55:18 +0000115 EVT ElVT = VT.getVectorElementType();
116 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000117
Craig Topperb14940a2012-04-22 20:55:18 +0000118 // Insert the relevant 128 bits.
119 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb14940a2012-04-22 20:55:18 +0000121 // This is the index of the first element of the 128-bit chunk
122 // we want.
123 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
124 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000125
Craig Topperb8d9da12012-09-06 06:09:01 +0000126 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000127 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
128 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000129}
130
Craig Topper4c7972d2012-04-22 18:15:59 +0000131/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
132/// instructions. This is used because creating CONCAT_VECTOR nodes of
133/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
134/// large BUILD_VECTORS.
135static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
136 unsigned NumElems, SelectionDAG &DAG,
137 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000138 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
139 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000143 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
144 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000145
Evan Cheng2bffee22011-02-01 01:14:13 +0000146 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000147 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000148 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000149 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000150 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000151
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000152 if (Subtarget->isTargetLinux())
153 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000154 if (Subtarget->isTargetELF())
155 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000158 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000161X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000163 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000164 X86ScalarSSEf64 = Subtarget->hasSSE2();
165 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000166
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000167 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000168 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000169
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000170 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000171 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000172
173 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000174 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000175 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
176 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000177
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 // For 64-bit since we have so many registers use the ILP scheduler, for
179 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000180 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000181 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000182 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000183 else if (Subtarget->is64Bit())
184 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000185 else
186 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000187 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000188
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000189 // Bypass expensive divides on Atom when compiling with O2
190 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) {
Preston Gurd8d662b52012-10-04 21:33:40 +0000191 addBypassSlowDiv(32, 8);
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000192 if (Subtarget->is64Bit())
193 addBypassSlowDiv(64, 16);
194 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000195
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000196 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000197 // Setup Windows compiler runtime calls.
198 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000199 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000200 setLibcallName(RTLIB::SREM_I64, "_allrem");
201 setLibcallName(RTLIB::UREM_I64, "_aullrem");
202 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000208
209 // The _ftol2 runtime function has an unusual calling conv, which
210 // is modeled by a special pseudo-instruction.
211 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
212 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
213 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
214 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000215 }
216
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000218 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000219 setUseUnderscoreSetJmp(false);
220 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000221 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000222 // MS runtime is weird: it exports _setjmp, but longjmp!
223 setUseUnderscoreSetJmp(true);
224 setUseUnderscoreLongJmp(false);
225 } else {
226 setUseUnderscoreSetJmp(true);
227 setUseUnderscoreLongJmp(true);
228 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000229
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000231 addRegisterClass(MVT::i8, &X86::GR8RegClass);
232 addRegisterClass(MVT::i16, &X86::GR16RegClass);
233 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000235 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000238
Scott Michelfdc40a02009-02-17 22:15:04 +0000239 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000241 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000243 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
245 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000246
247 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
249 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
250 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
251 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
252 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
253 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000254
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000255 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
256 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000260
Evan Cheng25ab6902006-09-08 06:48:29 +0000261 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000264 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 // We have an algorithm for SSE2->double, and we turn this into a
266 // 64-bit FILD followed by conditional FADD for other targets.
267 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000268 // We have an algorithm for SSE2, and we turn this into a 64-bit
269 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000270 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272
273 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
274 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
276 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000278 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 // SSE has no i16 to fp conversion, only i32
280 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000287 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000288 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
290 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000291 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000292
Dale Johannesen73328d12007-09-19 23:55:34 +0000293 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
294 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
296 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000297
Evan Cheng02568ff2006-01-30 22:13:22 +0000298 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
299 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000303 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000305 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000307 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
309 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310 }
311
312 // Handle FP_TO_UINT by promoting the destination to a larger signed
313 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000317
Evan Cheng25ab6902006-09-08 06:48:29 +0000318 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000321 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000322 // Since AVX is a superset of SSE3, only check for SSE here.
323 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 // Expand FP_TO_UINT into a select.
325 // FIXME: We would like to use a Custom expander here eventually to do
326 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000329 // With SSE3 we can use fisttpll to convert to a signed i64; without
330 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000334 if (isTargetFTOL()) {
335 // Use the _ftol2 runtime function, which has a pseudo-instruction
336 // to handle its weird calling convention.
337 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
338 }
339
Chris Lattner399610a2006-12-05 18:22:22 +0000340 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000341 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000342 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
343 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000344 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000345 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000346 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000347 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000348 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000349 }
Chris Lattner21f66852005-12-23 05:15:23 +0000350
Dan Gohmanb00ee212008-02-18 19:34:53 +0000351 // Scalar integer divide and remainder are lowered to use operations that
352 // produce two results, to match the available instructions. This exposes
353 // the two-result form to trivial CSE, which is able to combine x/y and x%y
354 // into a single instruction.
355 //
356 // Scalar integer multiply-high is also lowered to use two-result
357 // operations, to match the available instructions. However, plain multiply
358 // (low) operations are left as Legal, as there are single-result
359 // instructions for this in x86. Using the two-result multiply instructions
360 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000361 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000362 MVT VT = IntVTs[i];
363 setOperationAction(ISD::MULHS, VT, Expand);
364 setOperationAction(ISD::MULHU, VT, Expand);
365 setOperationAction(ISD::SDIV, VT, Expand);
366 setOperationAction(ISD::UDIV, VT, Expand);
367 setOperationAction(ISD::SREM, VT, Expand);
368 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000369
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000370 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000371 setOperationAction(ISD::ADDC, VT, Custom);
372 setOperationAction(ISD::ADDE, VT, Custom);
373 setOperationAction(ISD::SUBC, VT, Custom);
374 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000375 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000376
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
378 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Tom Stellard3ef53832013-03-08 15:36:57 +0000379 setOperationAction(ISD::BR_CC , MVT::f32, Expand);
380 setOperationAction(ISD::BR_CC , MVT::f64, Expand);
381 setOperationAction(ISD::BR_CC , MVT::f80, Expand);
382 setOperationAction(ISD::BR_CC , MVT::i8, Expand);
383 setOperationAction(ISD::BR_CC , MVT::i16, Expand);
384 setOperationAction(ISD::BR_CC , MVT::i32, Expand);
385 setOperationAction(ISD::BR_CC , MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
389 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
390 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
391 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
392 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
393 setOperationAction(ISD::FREM , MVT::f32 , Expand);
394 setOperationAction(ISD::FREM , MVT::f64 , Expand);
395 setOperationAction(ISD::FREM , MVT::f80 , Expand);
396 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000397
Chandler Carruth77821022011-12-24 12:12:34 +0000398 // Promote the i8 variants and force them on up to i32 which has a shorter
399 // encoding.
400 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
401 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
402 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
403 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000404 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000405 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
406 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000409 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000410 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
411 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
414 }
Craig Topper37f21672011-10-11 06:44:02 +0000415
416 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000417 // When promoting the i8 variants, force them to i32 for a shorter
418 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000419 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000420 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
421 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
422 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
424 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
425 if (Subtarget->is64Bit())
426 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000427 } else {
428 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
429 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
430 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000431 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
432 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
433 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
434 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000435 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000436 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
437 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000438 }
439
Benjamin Kramer1292c222010-12-04 20:32:23 +0000440 if (Subtarget->hasPOPCNT()) {
441 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
442 } else {
443 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
444 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
445 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
446 if (Subtarget->is64Bit())
447 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
448 }
449
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
451 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000452
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000453 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000454 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000455 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000456 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000457 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
459 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
460 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
461 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
462 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000463 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
465 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
466 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
467 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000470 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000471 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000473 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
474 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000475 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000476 // other SjLj exception interfaces are implemented and please don't build
477 // your own exception handling based on them.
478 // LLVM/Clang supports zero-cost DWARF exception handling.
479 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
480 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000481
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000482 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
484 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
485 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
486 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000487 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
489 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000490 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000491 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
493 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
494 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
495 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000496 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000497 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000498 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
500 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
501 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000502 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
504 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
505 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000506 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507
Craig Topper1accb7e2012-01-10 06:54:16 +0000508 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000510
Eric Christopher9a9d2752010-07-22 02:48:34 +0000511 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000512 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000513
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000514 // On X86 and X86-64, atomic operations are lowered to locked instructions.
515 // Locked instructions, in turn, have implicit fence semantics (all memory
516 // operations are flushed before issuing the locked instruction, and they
517 // are not buffered), so we can fold away the common pattern of
518 // fence-atomic-fence.
519 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000520
Mon P Wang63307c32008-05-05 19:05:59 +0000521 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000522 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000523 MVT VT = IntVTs[i];
524 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000526 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000527 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000528
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000529 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000530 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
532 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
533 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
534 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
535 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
536 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
537 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000538 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
539 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
540 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
541 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000542 }
543
Eli Friedman43f51ae2011-08-26 21:21:21 +0000544 if (Subtarget->hasCmpxchg16b()) {
545 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
546 }
547
Evan Cheng3c992d22006-03-07 02:02:57 +0000548 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000549 if (!Subtarget->isTargetDarwin() &&
550 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000551 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000553 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000554
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
556 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
557 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
558 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000559 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000560 setExceptionPointerRegister(X86::RAX);
561 setExceptionSelectorRegister(X86::RDX);
562 } else {
563 setExceptionPointerRegister(X86::EAX);
564 setExceptionSelectorRegister(X86::EDX);
565 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
567 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000568
Duncan Sands4a544a72011-09-06 13:37:06 +0000569 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
570 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000573 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000574
Nate Begemanacc398c2006-01-25 18:21:52 +0000575 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::VASTART , MVT::Other, Custom);
577 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000578 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::VAARG , MVT::Other, Custom);
580 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000581 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::VAARG , MVT::Other, Expand);
583 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000584 }
Evan Chengae642192007-03-02 23:16:35 +0000585
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
587 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000588
589 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
590 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
591 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000592 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000593 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
594 MVT::i64 : MVT::i32, Custom);
595 else
596 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
597 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000598
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000599 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000601 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000602 addRegisterClass(MVT::f32, &X86::FR32RegClass);
603 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604
Evan Cheng223547a2006-01-31 22:28:30 +0000605 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FABS , MVT::f64, Custom);
607 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000608
609 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FNEG , MVT::f64, Custom);
611 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000612
Evan Cheng68c47cb2007-01-05 07:55:56 +0000613 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
615 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000616
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000617 // Lower this to FGETSIGNx86 plus an AND.
618 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
619 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
620
Evan Chengd25e9e82006-02-02 00:28:23 +0000621 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000622 setOperationAction(ISD::FSIN , MVT::f64, Expand);
623 setOperationAction(ISD::FCOS , MVT::f64, Expand);
624 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
625 setOperationAction(ISD::FSIN , MVT::f32, Expand);
626 setOperationAction(ISD::FCOS , MVT::f32, Expand);
627 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000628
Chris Lattnera54aa942006-01-29 06:26:08 +0000629 // Expand FP immediates into loads from the stack, except for the special
630 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0)); // xorpd
632 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000633 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634 // Use SSE for f32, x87 for f64.
635 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000636 addRegisterClass(MVT::f32, &X86::FR32RegClass);
637 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
639 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641
642 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000644
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000646
647 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000650
651 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000652 setOperationAction(ISD::FSIN , MVT::f32, Expand);
653 setOperationAction(ISD::FCOS , MVT::f32, Expand);
654 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000655
Nate Begemane1795842008-02-14 08:57:00 +0000656 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // xorps
658 addLegalFPImmediate(APFloat(+0.0)); // FLD0
659 addLegalFPImmediate(APFloat(+1.0)); // FLD1
660 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
661 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
662
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000663 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000664 setOperationAction(ISD::FSIN , MVT::f64, Expand);
665 setOperationAction(ISD::FCOS , MVT::f64, Expand);
666 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000667 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000669 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000670 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000671 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
672 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
675 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
676 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
677 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000678
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000679 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000680 setOperationAction(ISD::FSIN , MVT::f64, Expand);
681 setOperationAction(ISD::FSIN , MVT::f32, Expand);
682 setOperationAction(ISD::FCOS , MVT::f64, Expand);
683 setOperationAction(ISD::FCOS , MVT::f32, Expand);
684 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
685 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000686 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000687 addLegalFPImmediate(APFloat(+0.0)); // FLD0
688 addLegalFPImmediate(APFloat(+1.0)); // FLD1
689 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
690 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000691 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
692 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
693 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
694 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000695 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000696
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 // We don't support FMA.
698 setOperationAction(ISD::FMA, MVT::f64, Expand);
699 setOperationAction(ISD::FMA, MVT::f32, Expand);
700
Dale Johannesen59a58732007-08-05 18:49:15 +0000701 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000702 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000703 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
705 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000706 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000707 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000708 addLegalFPImmediate(TmpFlt); // FLD0
709 TmpFlt.changeSign();
710 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000711
712 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000713 APFloat TmpFlt2(+1.0);
714 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
715 &ignored);
716 addLegalFPImmediate(TmpFlt2); // FLD1
717 TmpFlt2.changeSign();
718 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
719 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000720
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000721 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000722 setOperationAction(ISD::FSIN , MVT::f80, Expand);
723 setOperationAction(ISD::FCOS , MVT::f80, Expand);
724 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000725 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000726
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000727 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
728 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
729 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
730 setOperationAction(ISD::FRINT, MVT::f80, Expand);
731 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000732 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000733 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000734
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000735 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
737 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
738 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000739
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::FLOG, MVT::f80, Expand);
741 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
742 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
743 setOperationAction(ISD::FEXP, MVT::f80, Expand);
744 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000745
Mon P Wangf007a8b2008-11-06 05:31:54 +0000746 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000747 // (for widening) or expand (for scalarization). Then we will selectively
748 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000749 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
750 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000751 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000752 setOperationAction(ISD::ADD , VT, Expand);
753 setOperationAction(ISD::SUB , VT, Expand);
754 setOperationAction(ISD::FADD, VT, Expand);
755 setOperationAction(ISD::FNEG, VT, Expand);
756 setOperationAction(ISD::FSUB, VT, Expand);
757 setOperationAction(ISD::MUL , VT, Expand);
758 setOperationAction(ISD::FMUL, VT, Expand);
759 setOperationAction(ISD::SDIV, VT, Expand);
760 setOperationAction(ISD::UDIV, VT, Expand);
761 setOperationAction(ISD::FDIV, VT, Expand);
762 setOperationAction(ISD::SREM, VT, Expand);
763 setOperationAction(ISD::UREM, VT, Expand);
764 setOperationAction(ISD::LOAD, VT, Expand);
765 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
767 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
768 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
769 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
770 setOperationAction(ISD::FABS, VT, Expand);
771 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000772 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000773 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000774 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000775 setOperationAction(ISD::FREM, VT, Expand);
776 setOperationAction(ISD::FMA, VT, Expand);
777 setOperationAction(ISD::FPOWI, VT, Expand);
778 setOperationAction(ISD::FSQRT, VT, Expand);
779 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
780 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000781 setOperationAction(ISD::FCEIL, VT, Expand);
782 setOperationAction(ISD::FTRUNC, VT, Expand);
783 setOperationAction(ISD::FRINT, VT, Expand);
784 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000785 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
786 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
787 setOperationAction(ISD::SDIVREM, VT, Expand);
788 setOperationAction(ISD::UDIVREM, VT, Expand);
789 setOperationAction(ISD::FPOW, VT, Expand);
790 setOperationAction(ISD::CTPOP, VT, Expand);
791 setOperationAction(ISD::CTTZ, VT, Expand);
792 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
793 setOperationAction(ISD::CTLZ, VT, Expand);
794 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
795 setOperationAction(ISD::SHL, VT, Expand);
796 setOperationAction(ISD::SRA, VT, Expand);
797 setOperationAction(ISD::SRL, VT, Expand);
798 setOperationAction(ISD::ROTL, VT, Expand);
799 setOperationAction(ISD::ROTR, VT, Expand);
800 setOperationAction(ISD::BSWAP, VT, Expand);
801 setOperationAction(ISD::SETCC, VT, Expand);
802 setOperationAction(ISD::FLOG, VT, Expand);
803 setOperationAction(ISD::FLOG2, VT, Expand);
804 setOperationAction(ISD::FLOG10, VT, Expand);
805 setOperationAction(ISD::FEXP, VT, Expand);
806 setOperationAction(ISD::FEXP2, VT, Expand);
807 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
808 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
809 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
810 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
811 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
812 setOperationAction(ISD::TRUNCATE, VT, Expand);
813 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
814 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
815 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
816 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000817 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
818 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000819 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000820 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000821 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
822 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
823 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000824 }
825
Evan Chengc7ce29b2009-02-13 22:36:38 +0000826 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
827 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000828 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000829 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000830 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831 }
832
Dale Johannesen0488fb62010-09-30 23:57:10 +0000833 // MMX-sized vectors (other than x86mmx) are expected to be expanded
834 // into smaller operations.
835 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
836 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
837 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
838 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
839 setOperationAction(ISD::AND, MVT::v8i8, Expand);
840 setOperationAction(ISD::AND, MVT::v4i16, Expand);
841 setOperationAction(ISD::AND, MVT::v2i32, Expand);
842 setOperationAction(ISD::AND, MVT::v1i64, Expand);
843 setOperationAction(ISD::OR, MVT::v8i8, Expand);
844 setOperationAction(ISD::OR, MVT::v4i16, Expand);
845 setOperationAction(ISD::OR, MVT::v2i32, Expand);
846 setOperationAction(ISD::OR, MVT::v1i64, Expand);
847 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
848 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
849 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
850 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
851 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
852 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
853 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
854 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
856 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
857 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
858 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
859 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000860 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
861 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
862 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
863 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000864
Craig Topper1accb7e2012-01-10 06:54:16 +0000865 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000866 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000867
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
869 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
870 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
871 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
872 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
873 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000874 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
876 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
877 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
878 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
879 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000880 }
881
Craig Topper1accb7e2012-01-10 06:54:16 +0000882 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000883 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000884
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000885 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
886 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000887 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
888 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
889 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
890 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
893 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
894 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
895 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000896 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
898 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
899 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
900 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
901 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
902 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
903 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
904 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
905 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
906 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
907 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
908 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000909 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910
Nadav Rotem354efd82011-09-18 14:57:03 +0000911 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000912 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
913 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
914 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000915
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
917 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
918 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
919 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
920 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000921
Evan Cheng2c3ae372006-04-12 21:21:57 +0000922 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000923 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000924 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000925 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000926 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000927 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000928 // Do not attempt to custom lower non-128-bit vectors
929 if (!VT.is128BitVector())
930 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000931 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
932 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
933 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000934 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
937 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
938 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
939 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000942
Nate Begemancdd1eec2008-02-12 22:51:28 +0000943 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000946 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000948 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000949 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000950 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000951
952 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000953 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000954 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000955
Craig Topper0d1f1762012-08-12 00:34:56 +0000956 setOperationAction(ISD::AND, VT, Promote);
957 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
958 setOperationAction(ISD::OR, VT, Promote);
959 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
960 setOperationAction(ISD::XOR, VT, Promote);
961 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
962 setOperationAction(ISD::LOAD, VT, Promote);
963 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
964 setOperationAction(ISD::SELECT, VT, Promote);
965 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000966 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000967
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000969
Evan Cheng2c3ae372006-04-12 21:21:57 +0000970 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
972 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
973 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
974 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000975
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
977 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000978
Michael Liaoa7554632012-10-23 17:36:08 +0000979 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
980 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000981 // As there is no 64-bit GPR available, we need build a special custom
982 // sequence to convert from v2i32 to v2f32.
983 if (!Subtarget->is64Bit())
984 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000985
Michael Liao9d796db2012-10-10 16:32:15 +0000986 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000987 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000988
Michael Liaob8150d82012-09-10 18:33:51 +0000989 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000990 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000991
Craig Topperd0a31172012-01-10 06:37:29 +0000992 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000993 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
994 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
995 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
996 setOperationAction(ISD::FRINT, MVT::f32, Legal);
997 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
998 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
999 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1000 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1001 setOperationAction(ISD::FRINT, MVT::f64, Legal);
1002 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1003
Craig Topper12fb5c62012-09-08 17:42:27 +00001004 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001005 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
1006 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
1007 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
1008 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001009 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001010 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1011 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1012 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1013 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001014
Nate Begeman14d12ca2008-02-11 04:19:36 +00001015 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001018 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1019 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1020 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1021 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1022 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001023
Nate Begeman14d12ca2008-02-11 04:19:36 +00001024 // i8 and i16 vectors are custom , because the source register and source
1025 // source memory operand types are not the same width. f32 vectors are
1026 // custom since the immediate controlling the insert encodes additional
1027 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1029 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1030 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1031 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001032
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1034 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1035 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1036 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001037
Pete Coopera77214a2011-11-14 19:38:42 +00001038 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001039 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001040 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001041 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1042 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001043 }
1044 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001045
Craig Topper1accb7e2012-01-10 06:54:16 +00001046 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001047 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001048 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001049
Nadav Rotem43012222011-05-11 08:12:09 +00001050 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001051 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001052
Nadav Rotem43012222011-05-11 08:12:09 +00001053 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001054 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001055
Michael Liao5c5f1902013-03-20 02:28:20 +00001056 // In the customized shift lowering, the legal cases in AVX2 will be
1057 // recognized.
1058 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1059 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001060
Michael Liao5c5f1902013-03-20 02:28:20 +00001061 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1062 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
Michael Liao5c5f1902013-03-20 02:28:20 +00001064 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001065
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001066 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1067 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001068 }
1069
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001070 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001071 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1072 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1073 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1074 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1075 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1076 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001077
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1080 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001081
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1083 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1084 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1085 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1086 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001087 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001088 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1089 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1090 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1091 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001093 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001094
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1096 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1097 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1098 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1099 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001100 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001101 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1102 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1103 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1104 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001106 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001107
Michael Liaobedcbd42012-10-16 18:14:11 +00001108 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001109 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001110
1111 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1112
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001113 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1114 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001115 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001116
Michael Liaoa7554632012-10-23 17:36:08 +00001117 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1118 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1119 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1120
Michael Liaob8150d82012-09-10 18:33:51 +00001121 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1122
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001123 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1124 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1125
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001126 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1127 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1128
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001129 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001130 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001131
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001132 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1133
Duncan Sands28b77e92011-09-06 19:07:46 +00001134 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1135 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1136 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1137 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001138
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001139 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1140 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1141 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1142
Craig Topperaaa643c2011-11-09 07:28:55 +00001143 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1144 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1145 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1146 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001147
Nadav Rotem0509db22012-12-28 05:45:24 +00001148 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1149 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1150 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1151 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1152 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1153 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001154
Craig Topperbf404372012-08-31 15:40:30 +00001155 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001156 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1157 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1158 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1159 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1160 setOperationAction(ISD::FMA, MVT::f32, Legal);
1161 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001162 }
Craig Topper880ef452012-08-11 22:34:26 +00001163
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001164 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001165 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1166 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1167 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1168 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001169
Craig Topperaaa643c2011-11-09 07:28:55 +00001170 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1171 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1172 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1173 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001174
Craig Topperaaa643c2011-11-09 07:28:55 +00001175 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1176 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1177 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001178 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001179
1180 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001181
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001182 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001183 } else {
1184 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1185 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1186 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1187 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1188
1189 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1190 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1191 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1192 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1193
1194 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1195 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1196 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1197 // Don't lower v32i8 because there is no 128-bit byte mul
1198 }
Craig Topper13894fa2011-08-24 06:14:18 +00001199
Michael Liao5c5f1902013-03-20 02:28:20 +00001200 // In the customized shift lowering, the legal cases in AVX2 will be
1201 // recognized.
1202 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1203 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1204
1205 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1206 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1207
1208 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1209
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001210 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001211 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1212 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001213 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001214
1215 // Extract subvector is special because the value type
1216 // (result) is 128-bit but the source is 256-bit wide.
1217 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001218 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001219
1220 // Do not attempt to custom lower other non-256-bit vectors
1221 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001222 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001223
Craig Topper0d1f1762012-08-12 00:34:56 +00001224 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1225 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1226 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1227 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1228 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1229 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1230 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001231 }
1232
David Greene54d8eba2011-01-27 22:38:56 +00001233 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001234 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001235 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001236
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001237 // Do not attempt to promote non-256-bit vectors
1238 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001239 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001240
Craig Topper0d1f1762012-08-12 00:34:56 +00001241 setOperationAction(ISD::AND, VT, Promote);
1242 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1243 setOperationAction(ISD::OR, VT, Promote);
1244 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1245 setOperationAction(ISD::XOR, VT, Promote);
1246 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1247 setOperationAction(ISD::LOAD, VT, Promote);
1248 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1249 setOperationAction(ISD::SELECT, VT, Promote);
1250 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001251 }
David Greene9b9838d2009-06-29 16:47:10 +00001252 }
1253
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001254 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1255 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001256 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1257 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001258 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1259 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001260 }
1261
Evan Cheng6be2c582006-04-05 23:38:46 +00001262 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001264 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001265
Eli Friedman962f5492010-06-02 19:35:46 +00001266 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1267 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001268 //
Eli Friedman962f5492010-06-02 19:35:46 +00001269 // FIXME: We really should do custom legalization for addition and
1270 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1271 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001272 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1273 // Add/Sub/Mul with overflow operations are custom lowered.
1274 MVT VT = IntVTs[i];
1275 setOperationAction(ISD::SADDO, VT, Custom);
1276 setOperationAction(ISD::UADDO, VT, Custom);
1277 setOperationAction(ISD::SSUBO, VT, Custom);
1278 setOperationAction(ISD::USUBO, VT, Custom);
1279 setOperationAction(ISD::SMULO, VT, Custom);
1280 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001281 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001282
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001283 // There are no 8-bit 3-address imul/mul instructions
1284 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1285 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001286
Evan Chengd54f2d52009-03-31 19:38:51 +00001287 if (!Subtarget->is64Bit()) {
1288 // These libcalls are not available in 32-bit.
1289 setLibcallName(RTLIB::SHL_I128, 0);
1290 setLibcallName(RTLIB::SRL_I128, 0);
1291 setLibcallName(RTLIB::SRA_I128, 0);
1292 }
1293
Evan Cheng8688a582013-01-29 02:32:37 +00001294 // Combine sin / cos into one node or libcall if possible.
1295 if (Subtarget->hasSinCos()) {
1296 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1297 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001298 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001299 // For MacOSX, we don't want to the normal expansion of a libcall to
1300 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1301 // traffic.
1302 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1303 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1304 }
1305 }
1306
Evan Cheng206ee9d2006-07-07 08:33:52 +00001307 // We have target-specific dag combine patterns for the following nodes:
1308 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001309 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001310 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001311 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001312 setTargetDAGCombine(ISD::SHL);
1313 setTargetDAGCombine(ISD::SRA);
1314 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001315 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001316 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001317 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001318 setTargetDAGCombine(ISD::FADD);
1319 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001320 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001321 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001322 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001323 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001324 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001325 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001326 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001327 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001328 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001329 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001330 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001331 if (Subtarget->is64Bit())
1332 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001333 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001334
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001335 computeRegisterProperties();
1336
Evan Cheng05219282011-01-06 06:52:41 +00001337 // On Darwin, -Os means optimize for size without hurting performance,
1338 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001339 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1340 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1341 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1342 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1343 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1344 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001345 setPrefLoopAlignment(4); // 2^4 bytes.
Jim Grosbach3450f802013-02-20 21:13:59 +00001346 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001347
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001348 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001349 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001350
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001351 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001352}
1353
Duncan Sands28b77e92011-09-06 19:07:46 +00001354EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1355 if (!VT.isVector()) return MVT::i8;
1356 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001357}
1358
Evan Cheng29286502008-01-23 23:17:41 +00001359/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1360/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001361static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001362 if (MaxAlign == 16)
1363 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001364 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001365 if (VTy->getBitWidth() == 128)
1366 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001367 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001368 unsigned EltAlign = 0;
1369 getMaxByValAlign(ATy->getElementType(), EltAlign);
1370 if (EltAlign > MaxAlign)
1371 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001372 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001373 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1374 unsigned EltAlign = 0;
1375 getMaxByValAlign(STy->getElementType(i), EltAlign);
1376 if (EltAlign > MaxAlign)
1377 MaxAlign = EltAlign;
1378 if (MaxAlign == 16)
1379 break;
1380 }
1381 }
Evan Cheng29286502008-01-23 23:17:41 +00001382}
1383
1384/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1385/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001386/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1387/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001388unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001389 if (Subtarget->is64Bit()) {
1390 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001391 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001392 if (TyAlign > 8)
1393 return TyAlign;
1394 return 8;
1395 }
1396
Evan Cheng29286502008-01-23 23:17:41 +00001397 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001398 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001399 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001400 return Align;
1401}
Chris Lattner2b02a442007-02-25 08:29:00 +00001402
Evan Chengf0df0312008-05-15 08:39:06 +00001403/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001404/// and store operations as a result of memset, memcpy, and memmove
1405/// lowering. If DstAlign is zero that means it's safe to destination
1406/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1407/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001408/// probably because the source does not need to be loaded. If 'IsMemset' is
1409/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1410/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1411/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001412/// It returns EVT::Other if the type should be determined using generic
1413/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001414EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001415X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1416 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001417 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001418 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001419 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001420 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001421 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001422 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1423 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001424 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001425 (Subtarget->isUnalignedMemAccessFast() ||
1426 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001427 (SrcAlign == 0 || SrcAlign >= 16)))) {
1428 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001429 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001430 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001431 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001432 return MVT::v8f32;
1433 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001434 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001435 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001436 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001437 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001438 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001439 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001440 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001441 // Do not use f64 to lower memcpy if source is string constant. It's
1442 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001443 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001444 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001445 }
Evan Chengf0df0312008-05-15 08:39:06 +00001446 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 return MVT::i64;
1448 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001449}
1450
Evan Cheng7d342672012-12-12 01:32:07 +00001451bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001452 if (VT == MVT::f32)
1453 return X86ScalarSSEf32;
1454 else if (VT == MVT::f64)
1455 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001456 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001457}
1458
Evan Cheng376642e2012-12-10 23:21:26 +00001459bool
1460X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1461 if (Fast)
1462 *Fast = Subtarget->isUnalignedMemAccessFast();
1463 return true;
1464}
1465
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001466/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1467/// current function. The returned value is a member of the
1468/// MachineJumpTableInfo::JTEntryKind enum.
1469unsigned X86TargetLowering::getJumpTableEncoding() const {
1470 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1471 // symbol.
1472 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1473 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001474 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001475
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001476 // Otherwise, use the normal jump table encoding heuristics.
1477 return TargetLowering::getJumpTableEncoding();
1478}
1479
Chris Lattnerc64daab2010-01-26 05:02:42 +00001480const MCExpr *
1481X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1482 const MachineBasicBlock *MBB,
1483 unsigned uid,MCContext &Ctx) const{
1484 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1485 Subtarget->isPICStyleGOT());
1486 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1487 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001488 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1489 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001490}
1491
Evan Chengcc415862007-11-09 01:32:10 +00001492/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1493/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001495 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001496 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001497 // This doesn't have DebugLoc associated with it, but is not really the
1498 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001499 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001500 return Table;
1501}
1502
Chris Lattner589c6f62010-01-26 06:28:43 +00001503/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1504/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1505/// MCExpr.
1506const MCExpr *X86TargetLowering::
1507getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1508 MCContext &Ctx) const {
1509 // X86-64 uses RIP relative addressing based on the jump table label.
1510 if (Subtarget->isPICStyleRIPRel())
1511 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1512
1513 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001514 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001515}
1516
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001517// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001518std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001519X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001520 const TargetRegisterClass *RRC = 0;
1521 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001522 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001523 default:
1524 return TargetLowering::findRepresentativeClass(VT);
1525 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001526 RRC = Subtarget->is64Bit() ?
1527 (const TargetRegisterClass*)&X86::GR64RegClass :
1528 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001529 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001530 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001531 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001532 break;
1533 case MVT::f32: case MVT::f64:
1534 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1535 case MVT::v4f32: case MVT::v2f64:
1536 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1537 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001538 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001539 break;
1540 }
1541 return std::make_pair(RRC, Cost);
1542}
1543
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001544bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1545 unsigned &Offset) const {
1546 if (!Subtarget->isTargetLinux())
1547 return false;
1548
1549 if (Subtarget->is64Bit()) {
1550 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1551 Offset = 0x28;
1552 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1553 AddressSpace = 256;
1554 else
1555 AddressSpace = 257;
1556 } else {
1557 // %gs:0x14 on i386
1558 Offset = 0x14;
1559 AddressSpace = 256;
1560 }
1561 return true;
1562}
1563
Chris Lattner2b02a442007-02-25 08:29:00 +00001564//===----------------------------------------------------------------------===//
1565// Return Value Calling Convention Implementation
1566//===----------------------------------------------------------------------===//
1567
Chris Lattner59ed56b2007-02-28 04:55:35 +00001568#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001569
Michael J. Spencerec38de22010-10-10 22:04:20 +00001570bool
Eric Christopher471e4222011-06-08 23:55:35 +00001571X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001572 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001573 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001574 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001575 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001576 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001577 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001578 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001579}
1580
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581SDValue
1582X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001583 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001584 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001585 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001586 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001587 MachineFunction &MF = DAG.getMachineFunction();
1588 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
Chris Lattner9774c912007-02-27 05:28:59 +00001590 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001591 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 RVLocs, *DAG.getContext());
1593 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001594
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001597 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1598 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001599 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1600 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001602 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1604 CCValAssign &VA = RVLocs[i];
1605 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001606 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001607 EVT ValVT = ValToCopy.getValueType();
1608
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001609 // Promote values to the appropriate types
1610 if (VA.getLocInfo() == CCValAssign::SExt)
1611 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1612 else if (VA.getLocInfo() == CCValAssign::ZExt)
1613 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1614 else if (VA.getLocInfo() == CCValAssign::AExt)
1615 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1616 else if (VA.getLocInfo() == CCValAssign::BCvt)
1617 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1618
Dale Johannesenc4510512010-09-24 19:05:48 +00001619 // If this is x86-64, and we disabled SSE, we can't return FP values,
1620 // or SSE or MMX vectors.
1621 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1622 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001623 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001624 report_fatal_error("SSE register return with SSE disabled");
1625 }
1626 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1627 // llvm-gcc has never done it right and no one has noticed, so this
1628 // should be OK for now.
1629 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001630 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001631 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001632
Chris Lattner447ff682008-03-11 03:23:40 +00001633 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1634 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001635 if (VA.getLocReg() == X86::ST0 ||
1636 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001637 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1638 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001639 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001641 RetOps.push_back(ValToCopy);
1642 // Don't emit a copytoreg.
1643 continue;
1644 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001645
Evan Cheng242b38b2009-02-23 09:03:22 +00001646 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1647 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001648 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001649 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001650 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001652 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1653 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001654 // If we don't have SSE2 available, convert to v4f32 so the generated
1655 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001656 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001657 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001658 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001659 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001660 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001661
Dale Johannesendd64c412009-02-04 00:33:20 +00001662 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001663 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001664 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001665 }
Dan Gohman61a92132008-04-21 23:59:07 +00001666
Eli Benderskya5597f02013-01-25 22:07:43 +00001667 // The x86-64 ABIs require that for returning structs by value we copy
1668 // the sret argument into %rax/%eax (depending on ABI) for the return.
1669 // We saved the argument into a virtual register in the entry block,
1670 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001671 if (Subtarget->is64Bit() &&
1672 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1673 MachineFunction &MF = DAG.getMachineFunction();
1674 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1675 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001676 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001677 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001678 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001679
Eli Benderskya5597f02013-01-25 22:07:43 +00001680 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1681 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001682 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001683
Eli Benderskya5597f02013-01-25 22:07:43 +00001684 // RAX/EAX now acts like a return value.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001685 RetOps.push_back(DAG.getRegister(RetValReg, MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001686 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001687
Chris Lattner447ff682008-03-11 03:23:40 +00001688 RetOps[0] = Chain; // Update chain.
1689
1690 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001691 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001692 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
1694 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001696}
1697
Evan Chengbf010eb2012-04-10 01:51:00 +00001698bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001699 if (N->getNumValues() != 1)
1700 return false;
1701 if (!N->hasNUsesOfValue(1, 0))
1702 return false;
1703
Evan Chengbf010eb2012-04-10 01:51:00 +00001704 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001705 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001706 if (Copy->getOpcode() == ISD::CopyToReg) {
1707 // If the copy has a glue operand, we conservatively assume it isn't safe to
1708 // perform a tail call.
1709 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1710 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001711 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001712 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001713 return false;
1714
Evan Cheng1bf891a2010-12-01 22:59:46 +00001715 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001716 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001717 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718 if (UI->getOpcode() != X86ISD::RET_FLAG)
1719 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001720 HasRet = true;
1721 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001722
Evan Chengbf010eb2012-04-10 01:51:00 +00001723 if (!HasRet)
1724 return false;
1725
1726 Chain = TCChain;
1727 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001728}
1729
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001730MVT
1731X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001732 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001733 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001734 // TODO: Is this also valid on 32-bit?
1735 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001736 ReturnMVT = MVT::i8;
1737 else
1738 ReturnMVT = MVT::i32;
1739
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001740 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001741 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001742}
1743
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744/// LowerCallResult - Lower the result values of a call into the
1745/// appropriate copies out of appropriate physical registers.
1746///
1747SDValue
1748X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001749 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 const SmallVectorImpl<ISD::InputArg> &Ins,
1751 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001752 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001753
Chris Lattnere32bbf62007-02-28 07:09:55 +00001754 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001755 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001756 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001757 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001758 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001760
Chris Lattner3085e152007-02-25 08:59:22 +00001761 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001762 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001763 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001764 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001765
Torok Edwin3f142c32009-02-01 18:15:56 +00001766 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001768 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001769 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001770 }
1771
Evan Cheng79fb3b42009-02-20 20:43:02 +00001772 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001773
1774 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001775 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001776 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001777 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001778 // instead.
1779 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1780 // If we prefer to use the value in xmm registers, copy it out as f80 and
1781 // use a truncate to move it from fp stack reg to xmm reg.
1782 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001783 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001784 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1785 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001786 Val = Chain.getValue(0);
1787
1788 // Round the f80 to the right size, which also moves it to the appropriate
1789 // xmm register.
1790 if (CopyVT != VA.getValVT())
1791 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1792 // This truncation won't change the value.
1793 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001794 } else {
1795 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1796 CopyVT, InFlag).getValue(1);
1797 Val = Chain.getValue(0);
1798 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001799 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001801 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001802
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001804}
1805
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001806//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001807// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001808//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001809// StdCall calling convention seems to be standard for many Windows' API
1810// routines and around. It differs from C calling convention just a little:
1811// callee should clean up the stack, not caller. Symbols should be also
1812// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001813// For info on fast calling convention see Fast Calling Convention (tail call)
1814// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001815
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001817/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001818enum StructReturnType {
1819 NotStructReturn,
1820 RegStructReturn,
1821 StackStructReturn
1822};
1823static StructReturnType
1824callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001826 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001827
Rafael Espindola1cee7102012-07-25 13:41:10 +00001828 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1829 if (!Flags.isSRet())
1830 return NotStructReturn;
1831 if (Flags.isInReg())
1832 return RegStructReturn;
1833 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001834}
1835
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001836/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001837/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001838static StructReturnType
1839argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001840 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001841 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001842
Rafael Espindola1cee7102012-07-25 13:41:10 +00001843 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1844 if (!Flags.isSRet())
1845 return NotStructReturn;
1846 if (Flags.isInReg())
1847 return RegStructReturn;
1848 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001849}
1850
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001851/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1852/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001853/// the specific parameter attribute. The copy will be passed as a byval
1854/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001855static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001856CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001857 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1858 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001859 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001860
Dale Johannesendd64c412009-02-04 00:33:20 +00001861 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001862 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001863 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001864}
1865
Chris Lattner29689432010-03-11 00:22:57 +00001866/// IsTailCallConvention - Return true if the calling convention is one that
1867/// supports tail call optimization.
1868static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001869 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1870 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001871}
1872
Evan Cheng485fafc2011-03-21 01:19:09 +00001873bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001874 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001875 return false;
1876
1877 CallSite CS(CI);
1878 CallingConv::ID CalleeCC = CS.getCallingConv();
1879 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1880 return false;
1881
1882 return true;
1883}
1884
Evan Cheng0c439eb2010-01-27 00:07:07 +00001885/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1886/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001887static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1888 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001889 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001890}
1891
Dan Gohman98ca4f22009-08-05 01:29:28 +00001892SDValue
1893X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001894 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001895 const SmallVectorImpl<ISD::InputArg> &Ins,
1896 DebugLoc dl, SelectionDAG &DAG,
1897 const CCValAssign &VA,
1898 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001899 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001900 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001901 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001902 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1903 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001904 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001905 EVT ValVT;
1906
1907 // If value is passed by pointer we have address passed instead of the value
1908 // itself.
1909 if (VA.getLocInfo() == CCValAssign::Indirect)
1910 ValVT = VA.getLocVT();
1911 else
1912 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001913
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001914 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001915 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001916 // In case of tail call optimization mark all arguments mutable. Since they
1917 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001918 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001919 unsigned Bytes = Flags.getByValSize();
1920 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1921 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001922 return DAG.getFrameIndex(FI, getPointerTy());
1923 } else {
1924 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001925 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001926 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1927 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001928 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001929 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001930 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001931}
1932
Dan Gohman475871a2008-07-27 21:46:04 +00001933SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001935 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936 bool isVarArg,
1937 const SmallVectorImpl<ISD::InputArg> &Ins,
1938 DebugLoc dl,
1939 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001940 SmallVectorImpl<SDValue> &InVals)
1941 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001942 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001943 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 const Function* Fn = MF.getFunction();
1946 if (Fn->hasExternalLinkage() &&
1947 Subtarget->isTargetCygMing() &&
1948 Fn->getName() == "main")
1949 FuncInfo->setForceFramePointer(true);
1950
Evan Cheng1bc78042006-04-26 01:20:17 +00001951 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001952 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001953 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001954 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955
Chris Lattner29689432010-03-11 00:22:57 +00001956 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001957 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001958
Chris Lattner638402b2007-02-28 07:00:42 +00001959 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001960 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001961 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001963
1964 // Allocate shadow area for Win64
1965 if (IsWin64) {
1966 CCInfo.AllocateStack(32, 8);
1967 }
1968
Duncan Sands45907662010-10-31 13:21:44 +00001969 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Chris Lattnerf39f7712007-02-28 05:46:49 +00001971 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001972 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001973 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1974 CCValAssign &VA = ArgLocs[i];
1975 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1976 // places.
1977 assert(VA.getValNo() != LastVal &&
1978 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001979 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001981
Chris Lattnerf39f7712007-02-28 05:46:49 +00001982 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001983 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001984 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001986 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001988 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001990 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001993 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001994 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001995 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001997 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001999 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002000 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002001
Devang Patel68e6bee2011-02-21 23:21:26 +00002002 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002003 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Chris Lattnerf39f7712007-02-28 05:46:49 +00002005 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2006 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2007 // right size.
2008 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002009 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002010 DAG.getValueType(VA.getValVT()));
2011 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002012 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002013 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002014 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002015 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002016
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002017 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002018 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002019 if (RegVT.isVector())
2020 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2021 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002022 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002023 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002024 } else {
2025 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002026 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002027 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002028
2029 // If value is passed via pointer - do a load.
2030 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002031 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002032 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002033
Dan Gohman98ca4f22009-08-05 01:29:28 +00002034 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002035 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002036
Eli Benderskya5597f02013-01-25 22:07:43 +00002037 // The x86-64 ABIs require that for returning structs by value we copy
2038 // the sret argument into %rax/%eax (depending on ABI) for the return.
2039 // Save the argument into a virtual register so that we can access it
2040 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002041 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002042 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2043 unsigned Reg = FuncInfo->getSRetReturnReg();
2044 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002045 MVT PtrTy = getPointerTy();
2046 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002047 FuncInfo->setSRetReturnReg(Reg);
2048 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002049 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002051 }
2052
Chris Lattnerf39f7712007-02-28 05:46:49 +00002053 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002054 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002055 if (FuncIsMadeTailCallSafe(CallConv,
2056 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002057 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002058
Evan Cheng1bc78042006-04-26 01:20:17 +00002059 // If the function takes variable number of arguments, make a frame index for
2060 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002061 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002062 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2063 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002064 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002065 }
2066 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002067 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2068
2069 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002070 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002072 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002073 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002074 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2075 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002076 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2078 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2079 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002080 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002081 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002082
2083 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002084 // The XMM registers which might contain var arg parameters are shadowed
2085 // in their paired GPR. So we only need to save the GPR to their home
2086 // slots.
2087 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002088 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002089 } else {
2090 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2091 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002092
Chad Rosier30450e82011-12-22 22:35:21 +00002093 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2094 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002095 }
2096 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2097 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002098
Bill Wendling831737d2012-12-30 10:32:01 +00002099 bool NoImplicitFloatOps = Fn->getAttributes().
2100 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002101 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002102 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002103 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2104 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002105 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002106 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002107 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002108 // Kernel mode asks for SSE to be disabled, so don't push them
2109 // on the stack.
2110 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002111
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002112 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002113 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002114 // Get to the caller-allocated home save location. Add 8 to account
2115 // for the return address.
2116 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002117 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002118 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002119 // Fixup to set vararg frame on shadow area (4 x i64).
2120 if (NumIntRegs < 4)
2121 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002122 } else {
2123 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002124 // registers, then we must store them to their spots on the stack so
2125 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002126 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2127 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2128 FuncInfo->setRegSaveFrameIndex(
2129 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002130 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002131 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002132
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002134 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002135 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2136 getPointerTy());
2137 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002138 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002139 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2140 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002141 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002142 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002144 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002145 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002146 MachinePointerInfo::getFixedStack(
2147 FuncInfo->getRegSaveFrameIndex(), Offset),
2148 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002150 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002151 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002152
Dan Gohmanface41a2009-08-16 21:24:25 +00002153 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2154 // Now store the XMM (fp + vector) parameter registers.
2155 SmallVector<SDValue, 11> SaveXMMOps;
2156 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002157
Craig Topperc9099502012-04-20 06:31:50 +00002158 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002159 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2160 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002161
Dan Gohman1e93df62010-04-17 14:41:14 +00002162 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2163 FuncInfo->getRegSaveFrameIndex()));
2164 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2165 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002166
Dan Gohmanface41a2009-08-16 21:24:25 +00002167 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002168 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002169 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002170 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2171 SaveXMMOps.push_back(Val);
2172 }
2173 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2174 MVT::Other,
2175 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002177
2178 if (!MemOps.empty())
2179 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2180 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002182 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Gordon Henriksen86737662008-01-05 16:56:59 +00002184 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002185 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2186 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002187 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002188 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002189 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002190 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002191 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002192 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002193 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002194 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002195
Gordon Henriksen86737662008-01-05 16:56:59 +00002196 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002197 // RegSaveFrameIndex is X86-64 only.
2198 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002199 if (CallConv == CallingConv::X86_FastCall ||
2200 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002201 // fastcc functions can't have varargs.
2202 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 }
Evan Cheng25caf632006-05-23 21:06:34 +00002204
Rafael Espindola76927d752011-08-30 19:39:58 +00002205 FuncInfo->setArgumentStackSize(StackSize);
2206
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002208}
2209
Dan Gohman475871a2008-07-27 21:46:04 +00002210SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2212 SDValue StackPtr, SDValue Arg,
2213 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002214 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002215 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002216 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002217 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002218 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002219 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002220 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002221
2222 return DAG.getStore(Chain, dl, Arg, PtrOff,
2223 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002224 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002225}
2226
Bill Wendling64e87322009-01-16 19:25:27 +00002227/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002228/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002229SDValue
2230X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002231 SDValue &OutRetAddr, SDValue Chain,
2232 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002233 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002234 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002235 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002236 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002237
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002239 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002240 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002241 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242}
2243
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002244/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002245/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002246static SDValue
2247EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002248 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2249 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250 // Store the return address to the appropriate stack slot.
2251 if (!FPDiff) return Chain;
2252 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002253 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002254 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002255 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002256 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002257 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002258 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002259 return Chain;
2260}
2261
Dan Gohman98ca4f22009-08-05 01:29:28 +00002262SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002263X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002264 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002265 SelectionDAG &DAG = CLI.DAG;
2266 DebugLoc &dl = CLI.DL;
2267 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2268 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2269 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2270 SDValue Chain = CLI.Chain;
2271 SDValue Callee = CLI.Callee;
2272 CallingConv::ID CallConv = CLI.CallConv;
2273 bool &isTailCall = CLI.IsTailCall;
2274 bool isVarArg = CLI.IsVarArg;
2275
Dan Gohman98ca4f22009-08-05 01:29:28 +00002276 MachineFunction &MF = DAG.getMachineFunction();
2277 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002278 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002279 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002280 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002281 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002282
Nick Lewycky22de16d2012-01-19 00:34:10 +00002283 if (MF.getTarget().Options.DisableTailCalls)
2284 isTailCall = false;
2285
Evan Cheng5f941932010-02-05 02:21:12 +00002286 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002287 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002288 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002289 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002290 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002291 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002292
2293 // Sibcalls are automatically detected tailcalls which do not require
2294 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002295 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002296 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002297
2298 if (isTailCall)
2299 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002300 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002301
Chris Lattner29689432010-03-11 00:22:57 +00002302 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002303 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002304
Chris Lattner638402b2007-02-28 07:00:42 +00002305 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002306 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002307 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002308 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002309
2310 // Allocate shadow area for Win64
2311 if (IsWin64) {
2312 CCInfo.AllocateStack(32, 8);
2313 }
2314
Duncan Sands45907662010-10-31 13:21:44 +00002315 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Chris Lattner423c5f42007-02-28 05:31:48 +00002317 // Get a count of how many bytes are to be pushed on the stack.
2318 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002319 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002320 // This is a sibcall. The memory operands are available in caller's
2321 // own caller's stack.
2322 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002323 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2324 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002325 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002326
Gordon Henriksen86737662008-01-05 16:56:59 +00002327 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002328 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002329 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002330 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2331 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2332
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 FPDiff = NumBytesCallerPushed - NumBytes;
2334
2335 // Set the delta of movement of the returnaddr stackslot.
2336 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002337 if (FPDiff < X86Info->getTCReturnAddrDelta())
2338 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002339 }
2340
Evan Chengf22f9b32010-02-06 03:28:46 +00002341 if (!IsSibcall)
2342 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002343
Dan Gohman475871a2008-07-27 21:46:04 +00002344 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002345 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002346 if (isTailCall && FPDiff)
2347 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2348 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002349
Dan Gohman475871a2008-07-27 21:46:04 +00002350 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2351 SmallVector<SDValue, 8> MemOpChains;
2352 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002353
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002354 // Walk the register/memloc assignments, inserting copies/loads. In the case
2355 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002356 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2357 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002358 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002359 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002361 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002362
Chris Lattner423c5f42007-02-28 05:31:48 +00002363 // Promote the value if needed.
2364 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002365 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002366 case CCValAssign::Full: break;
2367 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002368 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002369 break;
2370 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002371 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002372 break;
2373 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002374 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002375 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002376 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002377 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2378 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002379 } else
2380 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2381 break;
2382 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002383 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002384 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002385 case CCValAssign::Indirect: {
2386 // Store the argument.
2387 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002388 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002389 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002390 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002391 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002392 Arg = SpillSlot;
2393 break;
2394 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Chris Lattner423c5f42007-02-28 05:31:48 +00002397 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002398 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2399 if (isVarArg && IsWin64) {
2400 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2401 // shadow reg if callee is a varargs function.
2402 unsigned ShadowReg = 0;
2403 switch (VA.getLocReg()) {
2404 case X86::XMM0: ShadowReg = X86::RCX; break;
2405 case X86::XMM1: ShadowReg = X86::RDX; break;
2406 case X86::XMM2: ShadowReg = X86::R8; break;
2407 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002408 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002409 if (ShadowReg)
2410 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002411 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002412 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002413 assert(VA.isMemLoc());
2414 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002415 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2416 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002417 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2418 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002419 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002420 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002421
Evan Cheng32fe1032006-05-25 00:59:30 +00002422 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002423 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002424 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002425
Chris Lattner88e1fd52009-07-09 04:24:46 +00002426 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002427 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2428 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002430 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2431 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002432 } else {
2433 // If we are tail calling and generating PIC/GOT style code load the
2434 // address of the callee into ECX. The value in ecx is used as target of
2435 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2436 // for tail calls on PIC/GOT architectures. Normally we would just put the
2437 // address of GOT into ebx and then call target@PLT. But for tail calls
2438 // ebx would be restored (since ebx is callee saved) before jumping to the
2439 // target@PLT.
2440
2441 // Note: The actual moving to ECX is done further down.
2442 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2443 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2444 !G->getGlobal()->hasProtectedVisibility())
2445 Callee = LowerGlobalAddress(Callee, DAG);
2446 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002447 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002448 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002449 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002450
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002451 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002452 // From AMD64 ABI document:
2453 // For calls that may call functions that use varargs or stdargs
2454 // (prototype-less calls or calls to functions containing ellipsis (...) in
2455 // the declaration) %al is used as hidden argument to specify the number
2456 // of SSE registers used. The contents of %al do not need to match exactly
2457 // the number of registers, but must be an ubound on the number of SSE
2458 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002459
Gordon Henriksen86737662008-01-05 16:56:59 +00002460 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002461 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002462 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2463 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2464 };
2465 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002466 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002467 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002468
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002469 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2470 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002471 }
2472
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002473 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002474 if (isTailCall) {
2475 // Force all the incoming stack arguments to be loaded from the stack
2476 // before any new outgoing arguments are stored to the stack, because the
2477 // outgoing stack slots may alias the incoming argument stack slots, and
2478 // the alias isn't otherwise explicit. This is slightly more conservative
2479 // than necessary, because it means that each store effectively depends
2480 // on every argument instead of just those arguments it would clobber.
2481 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2482
Dan Gohman475871a2008-07-27 21:46:04 +00002483 SmallVector<SDValue, 8> MemOpChains2;
2484 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002485 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002486 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002487 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2488 CCValAssign &VA = ArgLocs[i];
2489 if (VA.isRegLoc())
2490 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002491 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002492 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002493 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002494 // Create frame index.
2495 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002496 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002497 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002498 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002499
Duncan Sands276dcbd2008-03-21 09:14:45 +00002500 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002501 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002502 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002503 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002504 StackPtr = DAG.getCopyFromReg(Chain, dl,
2505 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002506 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002507 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002508
Dan Gohman98ca4f22009-08-05 01:29:28 +00002509 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2510 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002511 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002512 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002513 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002514 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002515 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002516 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002517 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002518 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002519 }
2520 }
2521
2522 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002523 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002524 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002525
2526 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002527 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2528 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002529 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002530 }
2531
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002532 // Build a sequence of copy-to-reg nodes chained together with token chain
2533 // and flag operands which copy the outgoing args into registers.
2534 SDValue InFlag;
2535 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2536 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2537 RegsToPass[i].second, InFlag);
2538 InFlag = Chain.getValue(1);
2539 }
2540
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002541 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2542 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2543 // In the 64-bit large code model, we have to make all calls
2544 // through a register, since the call instruction's 32-bit
2545 // pc-relative offset may not be large enough to hold the whole
2546 // address.
2547 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002548 // If the callee is a GlobalAddress node (quite common, every direct call
2549 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2550 // it.
2551
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002552 // We should use extra load for direct calls to dllimported functions in
2553 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002554 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002555 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002556 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002557 bool ExtraLoad = false;
2558 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002559
Chris Lattner48a7d022009-07-09 05:02:21 +00002560 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2561 // external symbols most go through the PLT in PIC mode. If the symbol
2562 // has hidden or protected visibility, or if it is static or local, then
2563 // we don't need to use the PLT - we can directly call it.
2564 if (Subtarget->isTargetELF() &&
2565 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002566 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002567 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002568 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002569 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002570 (!Subtarget->getTargetTriple().isMacOSX() ||
2571 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002572 // PC-relative references to external symbols should go through $stub,
2573 // unless we're building with the leopard linker or later, which
2574 // automatically synthesizes these stubs.
2575 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002576 } else if (Subtarget->isPICStyleRIPRel() &&
2577 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002578 cast<Function>(GV)->getAttributes().
2579 hasAttribute(AttributeSet::FunctionIndex,
2580 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002581 // If the function is marked as non-lazy, generate an indirect call
2582 // which loads from the GOT directly. This avoids runtime overhead
2583 // at the cost of eager binding (and one extra byte of encoding).
2584 OpFlags = X86II::MO_GOTPCREL;
2585 WrapperKind = X86ISD::WrapperRIP;
2586 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002587 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002588
Devang Patel0d881da2010-07-06 22:08:15 +00002589 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002590 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002591
2592 // Add a wrapper if needed.
2593 if (WrapperKind != ISD::DELETED_NODE)
2594 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2595 // Add extra indirection if needed.
2596 if (ExtraLoad)
2597 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2598 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002599 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002600 }
Bill Wendling056292f2008-09-16 21:48:12 +00002601 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002602 unsigned char OpFlags = 0;
2603
Evan Cheng1bf891a2010-12-01 22:59:46 +00002604 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2605 // external symbols should go through the PLT.
2606 if (Subtarget->isTargetELF() &&
2607 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2608 OpFlags = X86II::MO_PLT;
2609 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002610 (!Subtarget->getTargetTriple().isMacOSX() ||
2611 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002612 // PC-relative references to external symbols should go through $stub,
2613 // unless we're building with the leopard linker or later, which
2614 // automatically synthesizes these stubs.
2615 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002616 }
Eric Christopherfd179292009-08-27 18:07:15 +00002617
Chris Lattner48a7d022009-07-09 05:02:21 +00002618 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2619 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002620 }
2621
Chris Lattnerd96d0722007-02-25 06:40:16 +00002622 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002623 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002624 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002625
Evan Chengf22f9b32010-02-06 03:28:46 +00002626 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002627 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2628 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002629 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002631
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002632 Ops.push_back(Chain);
2633 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002634
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002636 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002637
Gordon Henriksen86737662008-01-05 16:56:59 +00002638 // Add argument registers to the end of the list so that they are known live
2639 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002640 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2641 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2642 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002643
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002644 // Add a register mask operand representing the call-preserved registers.
2645 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2646 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2647 assert(Mask && "Missing call preserved mask for calling convention");
2648 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002649
Gabor Greifba36cb52008-08-28 21:40:38 +00002650 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002651 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002652
Dan Gohman98ca4f22009-08-05 01:29:28 +00002653 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002654 // We used to do:
2655 //// If this is the first return lowered for this function, add the regs
2656 //// to the liveout set for the function.
2657 // This isn't right, although it's probably harmless on x86; liveouts
2658 // should be computed from returns not tail calls. Consider a void
2659 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002660 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002661 }
2662
Dale Johannesenace16102009-02-03 19:33:06 +00002663 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002664 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002665
Chris Lattner2d297092006-05-23 18:50:38 +00002666 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002667 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002668 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2669 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002670 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002671 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002672 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002673 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002674 // pops the hidden struct pointer, so we have to push it back.
2675 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002676 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002677 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002678 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002679 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002680
Gordon Henriksenae636f82008-01-03 16:47:34 +00002681 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002682 if (!IsSibcall) {
2683 Chain = DAG.getCALLSEQ_END(Chain,
2684 DAG.getIntPtrConstant(NumBytes, true),
2685 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2686 true),
2687 InFlag);
2688 InFlag = Chain.getValue(1);
2689 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002690
Chris Lattner3085e152007-02-25 08:59:22 +00002691 // Handle result values, copying them out of physregs into vregs that we
2692 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002693 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2694 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002695}
2696
Evan Cheng25ab6902006-09-08 06:48:29 +00002697//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002698// Fast Calling Convention (tail call) implementation
2699//===----------------------------------------------------------------------===//
2700
2701// Like std call, callee cleans arguments, convention except that ECX is
2702// reserved for storing the tail called function address. Only 2 registers are
2703// free for argument passing (inreg). Tail call optimization is performed
2704// provided:
2705// * tailcallopt is enabled
2706// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002707// On X86_64 architecture with GOT-style position independent code only local
2708// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002709// To keep the stack aligned according to platform abi the function
2710// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2711// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002712// If a tail called function callee has more arguments than the caller the
2713// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002714// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002715// original REtADDR, but before the saved framepointer or the spilled registers
2716// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2717// stack layout:
2718// arg1
2719// arg2
2720// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002721// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002722// move area ]
2723// (possible EBP)
2724// ESI
2725// EDI
2726// local1 ..
2727
2728/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2729/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002730unsigned
2731X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2732 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002733 MachineFunction &MF = DAG.getMachineFunction();
2734 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002735 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002736 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002737 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002738 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002739 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002740 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2741 // Number smaller than 12 so just add the difference.
2742 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2743 } else {
2744 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002745 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002746 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002747 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002748 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002749}
2750
Evan Cheng5f941932010-02-05 02:21:12 +00002751/// MatchingStackOffset - Return true if the given stack call argument is
2752/// already available in the same position (relatively) of the caller's
2753/// incoming argument stack.
2754static
2755bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2756 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2757 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002758 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2759 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002760 if (Arg.getOpcode() == ISD::CopyFromReg) {
2761 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002762 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002763 return false;
2764 MachineInstr *Def = MRI->getVRegDef(VR);
2765 if (!Def)
2766 return false;
2767 if (!Flags.isByVal()) {
2768 if (!TII->isLoadFromStackSlot(Def, FI))
2769 return false;
2770 } else {
2771 unsigned Opcode = Def->getOpcode();
2772 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2773 Def->getOperand(1).isFI()) {
2774 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002775 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002776 } else
2777 return false;
2778 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002779 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2780 if (Flags.isByVal())
2781 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002782 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002783 // define @foo(%struct.X* %A) {
2784 // tail call @bar(%struct.X* byval %A)
2785 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002786 return false;
2787 SDValue Ptr = Ld->getBasePtr();
2788 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2789 if (!FINode)
2790 return false;
2791 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002792 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002793 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002794 FI = FINode->getIndex();
2795 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002796 } else
2797 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002798
Evan Cheng4cae1332010-03-05 08:38:04 +00002799 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002800 if (!MFI->isFixedObjectIndex(FI))
2801 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002802 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002803}
2804
Dan Gohman98ca4f22009-08-05 01:29:28 +00002805/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2806/// for tail call optimization. Targets which want to do tail call
2807/// optimization should implement this function.
2808bool
2809X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002810 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002811 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002812 bool isCalleeStructRet,
2813 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002814 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002815 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002816 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002817 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002818 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002819 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002820 CalleeCC != CallingConv::C)
2821 return false;
2822
Evan Cheng7096ae42010-01-29 06:45:59 +00002823 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002824 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002825 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002826
2827 // If the function return type is x86_fp80 and the callee return type is not,
2828 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2829 // perform a tailcall optimization here.
2830 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2831 return false;
2832
Evan Cheng13617962010-04-30 01:12:32 +00002833 CallingConv::ID CallerCC = CallerF->getCallingConv();
2834 bool CCMatch = CallerCC == CalleeCC;
2835
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002836 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002837 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002838 return true;
2839 return false;
2840 }
2841
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002842 // Look for obvious safe cases to perform tail call optimization that do not
2843 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002844
Evan Cheng2c12cb42010-03-26 16:26:03 +00002845 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2846 // emit a special epilogue.
2847 if (RegInfo->needsStackRealignment(MF))
2848 return false;
2849
Evan Chenga375d472010-03-15 18:54:48 +00002850 // Also avoid sibcall optimization if either caller or callee uses struct
2851 // return semantics.
2852 if (isCalleeStructRet || isCallerStructRet)
2853 return false;
2854
Chad Rosier2416da32011-06-24 21:15:36 +00002855 // An stdcall caller is expected to clean up its arguments; the callee
2856 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002857 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002858 return false;
2859
Chad Rosier871f6642011-05-18 19:59:50 +00002860 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002861 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002862 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002863
2864 // Optimizing for varargs on Win64 is unlikely to be safe without
2865 // additional testing.
2866 if (Subtarget->isTargetWin64())
2867 return false;
2868
Chad Rosier871f6642011-05-18 19:59:50 +00002869 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002870 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002871 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002872
Chad Rosier871f6642011-05-18 19:59:50 +00002873 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2874 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2875 if (!ArgLocs[i].isRegLoc())
2876 return false;
2877 }
2878
Chad Rosier30450e82011-12-22 22:35:21 +00002879 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2880 // stack. Therefore, if it's not used by the call it is not safe to optimize
2881 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002882 bool Unused = false;
2883 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2884 if (!Ins[i].Used) {
2885 Unused = true;
2886 break;
2887 }
2888 }
2889 if (Unused) {
2890 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002891 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002892 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002893 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002894 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002895 CCValAssign &VA = RVLocs[i];
2896 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2897 return false;
2898 }
2899 }
2900
Evan Cheng13617962010-04-30 01:12:32 +00002901 // If the calling conventions do not match, then we'd better make sure the
2902 // results are returned in the same way as what the caller expects.
2903 if (!CCMatch) {
2904 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002905 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002906 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002907 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2908
2909 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002910 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002911 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002912 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2913
2914 if (RVLocs1.size() != RVLocs2.size())
2915 return false;
2916 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2917 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2918 return false;
2919 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2920 return false;
2921 if (RVLocs1[i].isRegLoc()) {
2922 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2923 return false;
2924 } else {
2925 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2926 return false;
2927 }
2928 }
2929 }
2930
Evan Chenga6bff982010-01-30 01:22:00 +00002931 // If the callee takes no arguments then go on to check the results of the
2932 // call.
2933 if (!Outs.empty()) {
2934 // Check if stack adjustment is needed. For now, do not do this if any
2935 // argument is passed on the stack.
2936 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002937 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002938 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002939
2940 // Allocate shadow area for Win64
2941 if (Subtarget->isTargetWin64()) {
2942 CCInfo.AllocateStack(32, 8);
2943 }
2944
Duncan Sands45907662010-10-31 13:21:44 +00002945 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002946 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002947 MachineFunction &MF = DAG.getMachineFunction();
2948 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2949 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002950
2951 // Check if the arguments are already laid out in the right way as
2952 // the caller's fixed stack objects.
2953 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002954 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2955 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002956 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002957 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2958 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002959 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002960 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002961 if (VA.getLocInfo() == CCValAssign::Indirect)
2962 return false;
2963 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002964 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2965 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002966 return false;
2967 }
2968 }
2969 }
Evan Cheng9c044672010-05-29 01:35:22 +00002970
2971 // If the tailcall address may be in a register, then make sure it's
2972 // possible to register allocate for it. In 32-bit, the call address can
2973 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002974 // callee-saved registers are restored. These happen to be the same
2975 // registers used to pass 'inreg' arguments so watch out for those.
2976 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002977 ((!isa<GlobalAddressSDNode>(Callee) &&
2978 !isa<ExternalSymbolSDNode>(Callee)) ||
2979 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002980 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002981 // In PIC we need an extra register to formulate the address computation
2982 // for the callee.
2983 unsigned MaxInRegs =
2984 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
2985
Evan Cheng9c044672010-05-29 01:35:22 +00002986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2987 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002988 if (!VA.isRegLoc())
2989 continue;
2990 unsigned Reg = VA.getLocReg();
2991 switch (Reg) {
2992 default: break;
2993 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002994 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00002995 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002996 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002997 }
2998 }
2999 }
Evan Chenga6bff982010-01-30 01:22:00 +00003000 }
Evan Chengb1712452010-01-27 06:25:16 +00003001
Evan Cheng86809cc2010-02-03 03:28:02 +00003002 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003003}
3004
Dan Gohman3df24e62008-09-03 23:12:08 +00003005FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003006X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3007 const TargetLibraryInfo *libInfo) const {
3008 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003009}
3010
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003011//===----------------------------------------------------------------------===//
3012// Other Lowering Hooks
3013//===----------------------------------------------------------------------===//
3014
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003015static bool MayFoldLoad(SDValue Op) {
3016 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3017}
3018
3019static bool MayFoldIntoStore(SDValue Op) {
3020 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3021}
3022
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003023static bool isTargetShuffle(unsigned Opcode) {
3024 switch(Opcode) {
3025 default: return false;
3026 case X86ISD::PSHUFD:
3027 case X86ISD::PSHUFHW:
3028 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003029 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003030 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003031 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003032 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003033 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003034 case X86ISD::MOVLPS:
3035 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003036 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003037 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003038 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003039 case X86ISD::MOVSS:
3040 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003041 case X86ISD::UNPCKL:
3042 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003043 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003044 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003045 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003046 return true;
3047 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003048}
3049
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003050static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003051 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003052 switch(Opc) {
3053 default: llvm_unreachable("Unknown x86 shuffle node");
3054 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003055 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003056 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003057 return DAG.getNode(Opc, dl, VT, V1);
3058 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003059}
3060
3061static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003062 SDValue V1, unsigned TargetMask,
3063 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003064 switch(Opc) {
3065 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003066 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003067 case X86ISD::PSHUFHW:
3068 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003069 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003070 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003071 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3072 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003073}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003074
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003075static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003076 SDValue V1, SDValue V2, unsigned TargetMask,
3077 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003078 switch(Opc) {
3079 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003080 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003081 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003082 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003083 return DAG.getNode(Opc, dl, VT, V1, V2,
3084 DAG.getConstant(TargetMask, MVT::i8));
3085 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003086}
3087
3088static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3089 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3090 switch(Opc) {
3091 default: llvm_unreachable("Unknown x86 shuffle node");
3092 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003093 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003094 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003095 case X86ISD::MOVLPS:
3096 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003097 case X86ISD::MOVSS:
3098 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003099 case X86ISD::UNPCKL:
3100 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003101 return DAG.getNode(Opc, dl, VT, V1, V2);
3102 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003103}
3104
Dan Gohmand858e902010-04-17 15:26:15 +00003105SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003106 MachineFunction &MF = DAG.getMachineFunction();
3107 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3108 int ReturnAddrIndex = FuncInfo->getRAIndex();
3109
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003110 if (ReturnAddrIndex == 0) {
3111 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003112 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003114 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003115 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003116 }
3117
Evan Cheng25ab6902006-09-08 06:48:29 +00003118 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003119}
3120
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003121bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3122 bool hasSymbolicDisplacement) {
3123 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003124 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003125 return false;
3126
3127 // If we don't have a symbolic displacement - we don't have any extra
3128 // restrictions.
3129 if (!hasSymbolicDisplacement)
3130 return true;
3131
3132 // FIXME: Some tweaks might be needed for medium code model.
3133 if (M != CodeModel::Small && M != CodeModel::Kernel)
3134 return false;
3135
3136 // For small code model we assume that latest object is 16MB before end of 31
3137 // bits boundary. We may also accept pretty large negative constants knowing
3138 // that all objects are in the positive half of address space.
3139 if (M == CodeModel::Small && Offset < 16*1024*1024)
3140 return true;
3141
3142 // For kernel code model we know that all object resist in the negative half
3143 // of 32bits address space. We may not accept negative offsets, since they may
3144 // be just off and we may accept pretty large positive ones.
3145 if (M == CodeModel::Kernel && Offset > 0)
3146 return true;
3147
3148 return false;
3149}
3150
Evan Chengef41ff62011-06-23 17:54:54 +00003151/// isCalleePop - Determines whether the callee is required to pop its
3152/// own arguments. Callee pop is necessary to support tail calls.
3153bool X86::isCalleePop(CallingConv::ID CallingConv,
3154 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3155 if (IsVarArg)
3156 return false;
3157
3158 switch (CallingConv) {
3159 default:
3160 return false;
3161 case CallingConv::X86_StdCall:
3162 return !is64Bit;
3163 case CallingConv::X86_FastCall:
3164 return !is64Bit;
3165 case CallingConv::X86_ThisCall:
3166 return !is64Bit;
3167 case CallingConv::Fast:
3168 return TailCallOpt;
3169 case CallingConv::GHC:
3170 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003171 case CallingConv::HiPE:
3172 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003173 }
3174}
3175
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003176/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3177/// specific condition code, returning the condition code and the LHS/RHS of the
3178/// comparison to make.
3179static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3180 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003181 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003182 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3183 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3184 // X > -1 -> X == 0, jump !sign.
3185 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003186 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003187 }
3188 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003189 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003190 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003191 }
3192 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003193 // X < 1 -> X <= 0
3194 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003195 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003196 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003197 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003198
Evan Chengd9558e02006-01-06 00:43:03 +00003199 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003200 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003201 case ISD::SETEQ: return X86::COND_E;
3202 case ISD::SETGT: return X86::COND_G;
3203 case ISD::SETGE: return X86::COND_GE;
3204 case ISD::SETLT: return X86::COND_L;
3205 case ISD::SETLE: return X86::COND_LE;
3206 case ISD::SETNE: return X86::COND_NE;
3207 case ISD::SETULT: return X86::COND_B;
3208 case ISD::SETUGT: return X86::COND_A;
3209 case ISD::SETULE: return X86::COND_BE;
3210 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003211 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003213
Chris Lattner4c78e022008-12-23 23:42:27 +00003214 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003215
Chris Lattner4c78e022008-12-23 23:42:27 +00003216 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003217 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3218 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003219 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3220 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003221 }
3222
Chris Lattner4c78e022008-12-23 23:42:27 +00003223 switch (SetCCOpcode) {
3224 default: break;
3225 case ISD::SETOLT:
3226 case ISD::SETOLE:
3227 case ISD::SETUGT:
3228 case ISD::SETUGE:
3229 std::swap(LHS, RHS);
3230 break;
3231 }
3232
3233 // On a floating point condition, the flags are set as follows:
3234 // ZF PF CF op
3235 // 0 | 0 | 0 | X > Y
3236 // 0 | 0 | 1 | X < Y
3237 // 1 | 0 | 0 | X == Y
3238 // 1 | 1 | 1 | unordered
3239 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003240 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003241 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003242 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003243 case ISD::SETOLT: // flipped
3244 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003245 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003246 case ISD::SETOLE: // flipped
3247 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003248 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003249 case ISD::SETUGT: // flipped
3250 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003251 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003252 case ISD::SETUGE: // flipped
3253 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003254 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003255 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003256 case ISD::SETNE: return X86::COND_NE;
3257 case ISD::SETUO: return X86::COND_P;
3258 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003259 case ISD::SETOEQ:
3260 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003261 }
Evan Chengd9558e02006-01-06 00:43:03 +00003262}
3263
Evan Cheng4a460802006-01-11 00:33:36 +00003264/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3265/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003266/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003267static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003268 switch (X86CC) {
3269 default:
3270 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003271 case X86::COND_B:
3272 case X86::COND_BE:
3273 case X86::COND_E:
3274 case X86::COND_P:
3275 case X86::COND_A:
3276 case X86::COND_AE:
3277 case X86::COND_NE:
3278 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003279 return true;
3280 }
3281}
3282
Evan Chengeb2f9692009-10-27 19:56:55 +00003283/// isFPImmLegal - Returns true if the target can instruction select the
3284/// specified FP immediate natively. If false, the legalizer will
3285/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003286bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003287 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3288 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3289 return true;
3290 }
3291 return false;
3292}
3293
Nate Begeman9008ca62009-04-27 18:41:29 +00003294/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3295/// the specified range (L, H].
3296static bool isUndefOrInRange(int Val, int Low, int Hi) {
3297 return (Val < 0) || (Val >= Low && Val < Hi);
3298}
3299
3300/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3301/// specified value.
3302static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003303 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003304}
3305
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003306/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003307/// from position Pos and ending in Pos+Size, falls within the specified
3308/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003309static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003310 unsigned Pos, unsigned Size, int Low) {
3311 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003312 if (!isUndefOrEqual(Mask[i], Low))
3313 return false;
3314 return true;
3315}
3316
Nate Begeman9008ca62009-04-27 18:41:29 +00003317/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3318/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3319/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003320static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003321 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003322 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 return (Mask[0] < 2 && Mask[1] < 2);
3325 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003326}
3327
Nate Begeman9008ca62009-04-27 18:41:29 +00003328/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3329/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003330static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3331 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003332 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003335 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3336 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003337
Evan Cheng506d3df2006-03-29 23:07:14 +00003338 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003339 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003340 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Craig Toppera9a568a2012-05-02 08:03:44 +00003343 if (VT == MVT::v16i16) {
3344 // Lower quadword copied in order or undef.
3345 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3346 return false;
3347
3348 // Upper quadword shuffled.
3349 for (unsigned i = 12; i != 16; ++i)
3350 if (!isUndefOrInRange(Mask[i], 12, 16))
3351 return false;
3352 }
3353
Evan Cheng506d3df2006-03-29 23:07:14 +00003354 return true;
3355}
3356
Nate Begeman9008ca62009-04-27 18:41:29 +00003357/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3358/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003359static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3360 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003361 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003362
Rafael Espindola15684b22009-04-24 12:40:33 +00003363 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003364 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3365 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003366
Rafael Espindola15684b22009-04-24 12:40:33 +00003367 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003368 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003369 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003370 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003371
Craig Toppera9a568a2012-05-02 08:03:44 +00003372 if (VT == MVT::v16i16) {
3373 // Upper quadword copied in order.
3374 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3375 return false;
3376
3377 // Lower quadword shuffled.
3378 for (unsigned i = 8; i != 12; ++i)
3379 if (!isUndefOrInRange(Mask[i], 8, 12))
3380 return false;
3381 }
3382
Rafael Espindola15684b22009-04-24 12:40:33 +00003383 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003384}
3385
Nate Begemana09008b2009-10-19 02:17:23 +00003386/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3387/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003388static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3389 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003390 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3391 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003392 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003393
Craig Topper0e2037b2012-01-20 05:53:00 +00003394 unsigned NumElts = VT.getVectorNumElements();
3395 unsigned NumLanes = VT.getSizeInBits()/128;
3396 unsigned NumLaneElts = NumElts/NumLanes;
3397
3398 // Do not handle 64-bit element shuffles with palignr.
3399 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003400 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003401
Craig Topper0e2037b2012-01-20 05:53:00 +00003402 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3403 unsigned i;
3404 for (i = 0; i != NumLaneElts; ++i) {
3405 if (Mask[i+l] >= 0)
3406 break;
3407 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003408
Craig Topper0e2037b2012-01-20 05:53:00 +00003409 // Lane is all undef, go to next lane
3410 if (i == NumLaneElts)
3411 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003412
Craig Topper0e2037b2012-01-20 05:53:00 +00003413 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003414
Craig Topper0e2037b2012-01-20 05:53:00 +00003415 // Make sure its in this lane in one of the sources
3416 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3417 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003418 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003419
3420 // If not lane 0, then we must match lane 0
3421 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3422 return false;
3423
3424 // Correct second source to be contiguous with first source
3425 if (Start >= (int)NumElts)
3426 Start -= NumElts - NumLaneElts;
3427
3428 // Make sure we're shifting in the right direction.
3429 if (Start <= (int)(i+l))
3430 return false;
3431
3432 Start -= i;
3433
3434 // Check the rest of the elements to see if they are consecutive.
3435 for (++i; i != NumLaneElts; ++i) {
3436 int Idx = Mask[i+l];
3437
3438 // Make sure its in this lane
3439 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3440 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3441 return false;
3442
3443 // If not lane 0, then we must match lane 0
3444 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3445 return false;
3446
3447 if (Idx >= (int)NumElts)
3448 Idx -= NumElts - NumLaneElts;
3449
3450 if (!isUndefOrEqual(Idx, Start+i))
3451 return false;
3452
3453 }
Nate Begemana09008b2009-10-19 02:17:23 +00003454 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003455
Nate Begemana09008b2009-10-19 02:17:23 +00003456 return true;
3457}
3458
Craig Topper1a7700a2012-01-19 08:19:12 +00003459/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3460/// the two vector operands have swapped position.
3461static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3462 unsigned NumElems) {
3463 for (unsigned i = 0; i != NumElems; ++i) {
3464 int idx = Mask[i];
3465 if (idx < 0)
3466 continue;
3467 else if (idx < (int)NumElems)
3468 Mask[i] = idx + NumElems;
3469 else
3470 Mask[i] = idx - NumElems;
3471 }
3472}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003473
Craig Topper1a7700a2012-01-19 08:19:12 +00003474/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3475/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3476/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3477/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003478static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003479 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003480 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003481 return false;
3482
Craig Topper1a7700a2012-01-19 08:19:12 +00003483 unsigned NumElems = VT.getVectorNumElements();
3484 unsigned NumLanes = VT.getSizeInBits()/128;
3485 unsigned NumLaneElems = NumElems/NumLanes;
3486
3487 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003488 return false;
3489
3490 // VSHUFPSY divides the resulting vector into 4 chunks.
3491 // The sources are also splitted into 4 chunks, and each destination
3492 // chunk must come from a different source chunk.
3493 //
3494 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3495 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3496 //
3497 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3498 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3499 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003500 // VSHUFPDY divides the resulting vector into 4 chunks.
3501 // The sources are also splitted into 4 chunks, and each destination
3502 // chunk must come from a different source chunk.
3503 //
3504 // SRC1 => X3 X2 X1 X0
3505 // SRC2 => Y3 Y2 Y1 Y0
3506 //
3507 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3508 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003509 unsigned HalfLaneElems = NumLaneElems/2;
3510 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3511 for (unsigned i = 0; i != NumLaneElems; ++i) {
3512 int Idx = Mask[i+l];
3513 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3514 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3515 return false;
3516 // For VSHUFPSY, the mask of the second half must be the same as the
3517 // first but with the appropriate offsets. This works in the same way as
3518 // VPERMILPS works with masks.
3519 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3520 continue;
3521 if (!isUndefOrEqual(Idx, Mask[i]+l))
3522 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003523 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003524 }
3525
3526 return true;
3527}
3528
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003529/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3530/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003531static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003532 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003533 return false;
3534
Craig Topper7a9a28b2012-08-12 02:23:29 +00003535 unsigned NumElems = VT.getVectorNumElements();
3536
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003537 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003538 return false;
3539
Evan Cheng2064a2b2006-03-28 06:50:32 +00003540 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003541 return isUndefOrEqual(Mask[0], 6) &&
3542 isUndefOrEqual(Mask[1], 7) &&
3543 isUndefOrEqual(Mask[2], 2) &&
3544 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003545}
3546
Nate Begeman0b10b912009-11-07 23:17:15 +00003547/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3548/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3549/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003550static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003551 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003552 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003553
Craig Topper7a9a28b2012-08-12 02:23:29 +00003554 unsigned NumElems = VT.getVectorNumElements();
3555
Nate Begeman0b10b912009-11-07 23:17:15 +00003556 if (NumElems != 4)
3557 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003558
Craig Topperdd637ae2012-02-19 05:41:45 +00003559 return isUndefOrEqual(Mask[0], 2) &&
3560 isUndefOrEqual(Mask[1], 3) &&
3561 isUndefOrEqual(Mask[2], 2) &&
3562 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003563}
3564
Evan Cheng5ced1d82006-04-06 23:23:56 +00003565/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3566/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003567static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003568 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003569 return false;
3570
Craig Topperdd637ae2012-02-19 05:41:45 +00003571 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003572
Evan Cheng5ced1d82006-04-06 23:23:56 +00003573 if (NumElems != 2 && NumElems != 4)
3574 return false;
3575
Chad Rosier238ae312012-04-30 17:47:15 +00003576 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003577 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003578 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003579
Chad Rosier238ae312012-04-30 17:47:15 +00003580 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003581 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003582 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003583
3584 return true;
3585}
3586
Nate Begeman0b10b912009-11-07 23:17:15 +00003587/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3588/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003589static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003590 if (!VT.is128BitVector())
3591 return false;
3592
Craig Topperdd637ae2012-02-19 05:41:45 +00003593 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003594
Craig Topper7a9a28b2012-08-12 02:23:29 +00003595 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003596 return false;
3597
Chad Rosier238ae312012-04-30 17:47:15 +00003598 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003599 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003600 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003601
Chad Rosier238ae312012-04-30 17:47:15 +00003602 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3603 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003604 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003605
3606 return true;
3607}
3608
Elena Demikhovsky15963732012-06-26 08:04:10 +00003609//
3610// Some special combinations that can be optimized.
3611//
3612static
3613SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3614 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003615 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003616 DebugLoc dl = SVOp->getDebugLoc();
3617
3618 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3619 return SDValue();
3620
3621 ArrayRef<int> Mask = SVOp->getMask();
3622
3623 // These are the special masks that may be optimized.
3624 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3625 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3626 bool MatchEvenMask = true;
3627 bool MatchOddMask = true;
3628 for (int i=0; i<8; ++i) {
3629 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3630 MatchEvenMask = false;
3631 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3632 MatchOddMask = false;
3633 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003634
Elena Demikhovsky32510202012-09-04 12:49:02 +00003635 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003636 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003637
Elena Demikhovsky15963732012-06-26 08:04:10 +00003638 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3639
Elena Demikhovsky32510202012-09-04 12:49:02 +00003640 SDValue Op0 = SVOp->getOperand(0);
3641 SDValue Op1 = SVOp->getOperand(1);
3642
3643 if (MatchEvenMask) {
3644 // Shift the second operand right to 32 bits.
3645 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3646 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3647 } else {
3648 // Shift the first operand left to 32 bits.
3649 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3650 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3651 }
3652 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3653 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003654}
3655
Evan Cheng0038e592006-03-28 00:39:58 +00003656/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3657/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003658static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003659 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003660 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003661
3662 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3663 "Unsupported vector type for unpckh");
3664
Craig Topper5a529e42013-01-18 06:44:29 +00003665 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003666 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003667 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003668
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003669 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3670 // independently on 128-bit lanes.
3671 unsigned NumLanes = VT.getSizeInBits()/128;
3672 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003673
Craig Topper94438ba2011-12-16 08:06:31 +00003674 for (unsigned l = 0; l != NumLanes; ++l) {
3675 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3676 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003677 i += 2, ++j) {
3678 int BitI = Mask[i];
3679 int BitI1 = Mask[i+1];
3680 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003681 return false;
David Greenea20244d2011-03-02 17:23:43 +00003682 if (V2IsSplat) {
3683 if (!isUndefOrEqual(BitI1, NumElts))
3684 return false;
3685 } else {
3686 if (!isUndefOrEqual(BitI1, j + NumElts))
3687 return false;
3688 }
Evan Cheng39623da2006-04-20 08:58:49 +00003689 }
Evan Cheng0038e592006-03-28 00:39:58 +00003690 }
David Greenea20244d2011-03-02 17:23:43 +00003691
Evan Cheng0038e592006-03-28 00:39:58 +00003692 return true;
3693}
3694
Evan Cheng4fcb9222006-03-28 02:43:26 +00003695/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3696/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003697static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003698 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003699 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003700
3701 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3702 "Unsupported vector type for unpckh");
3703
Craig Topper5a529e42013-01-18 06:44:29 +00003704 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003705 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003706 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003707
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003708 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3709 // independently on 128-bit lanes.
3710 unsigned NumLanes = VT.getSizeInBits()/128;
3711 unsigned NumLaneElts = NumElts/NumLanes;
3712
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003713 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003714 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3715 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003716 int BitI = Mask[i];
3717 int BitI1 = Mask[i+1];
3718 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003719 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003720 if (V2IsSplat) {
3721 if (isUndefOrEqual(BitI1, NumElts))
3722 return false;
3723 } else {
3724 if (!isUndefOrEqual(BitI1, j+NumElts))
3725 return false;
3726 }
Evan Cheng39623da2006-04-20 08:58:49 +00003727 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003728 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003729 return true;
3730}
3731
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003732/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3733/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3734/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003735static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003736 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003737 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003738
3739 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3740 "Unsupported vector type for unpckh");
3741
Craig Topper5a529e42013-01-18 06:44:29 +00003742 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003743 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003744 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003745
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003746 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3747 // FIXME: Need a better way to get rid of this, there's no latency difference
3748 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3749 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003750 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003751 return false;
3752
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003753 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3754 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003755 unsigned NumLanes = VT.getSizeInBits()/128;
3756 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003757
Craig Topper94438ba2011-12-16 08:06:31 +00003758 for (unsigned l = 0; l != NumLanes; ++l) {
3759 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3760 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003761 i += 2, ++j) {
3762 int BitI = Mask[i];
3763 int BitI1 = Mask[i+1];
3764
3765 if (!isUndefOrEqual(BitI, j))
3766 return false;
3767 if (!isUndefOrEqual(BitI1, j))
3768 return false;
3769 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003770 }
David Greenea20244d2011-03-02 17:23:43 +00003771
Rafael Espindola15684b22009-04-24 12:40:33 +00003772 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003773}
3774
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003775/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3776/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3777/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003778static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003779 unsigned NumElts = VT.getVectorNumElements();
3780
3781 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3782 "Unsupported vector type for unpckh");
3783
Craig Topper5a529e42013-01-18 06:44:29 +00003784 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003785 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003786 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003787
Craig Topper94438ba2011-12-16 08:06:31 +00003788 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3789 // independently on 128-bit lanes.
3790 unsigned NumLanes = VT.getSizeInBits()/128;
3791 unsigned NumLaneElts = NumElts/NumLanes;
3792
3793 for (unsigned l = 0; l != NumLanes; ++l) {
3794 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3795 i != (l+1)*NumLaneElts; i += 2, ++j) {
3796 int BitI = Mask[i];
3797 int BitI1 = Mask[i+1];
3798 if (!isUndefOrEqual(BitI, j))
3799 return false;
3800 if (!isUndefOrEqual(BitI1, j))
3801 return false;
3802 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003803 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003804 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003805}
3806
Evan Cheng017dcc62006-04-21 01:05:10 +00003807/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3808/// specifies a shuffle of elements that is suitable for input to MOVSS,
3809/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003810static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003811 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003812 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003813 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003814 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003815
Craig Topperc612d792012-01-02 09:17:37 +00003816 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003817
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003819 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003820
Craig Topperc612d792012-01-02 09:17:37 +00003821 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003823 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003824
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003825 return true;
3826}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003827
Craig Topper70b883b2011-11-28 10:14:51 +00003828/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003829/// as permutations between 128-bit chunks or halves. As an example: this
3830/// shuffle bellow:
3831/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3832/// The first half comes from the second half of V1 and the second half from the
3833/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003834static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3835 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003836 return false;
3837
3838 // The shuffle result is divided into half A and half B. In total the two
3839 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3840 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003841 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003842 bool MatchA = false, MatchB = false;
3843
3844 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003845 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003846 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3847 MatchA = true;
3848 break;
3849 }
3850 }
3851
3852 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003853 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003854 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3855 MatchB = true;
3856 break;
3857 }
3858 }
3859
3860 return MatchA && MatchB;
3861}
3862
Craig Topper70b883b2011-11-28 10:14:51 +00003863/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3864/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003865static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003866 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003867
Craig Topperc612d792012-01-02 09:17:37 +00003868 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003869
Craig Topperc612d792012-01-02 09:17:37 +00003870 unsigned FstHalf = 0, SndHalf = 0;
3871 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003872 if (SVOp->getMaskElt(i) > 0) {
3873 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3874 break;
3875 }
3876 }
Craig Topperc612d792012-01-02 09:17:37 +00003877 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003878 if (SVOp->getMaskElt(i) > 0) {
3879 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3880 break;
3881 }
3882 }
3883
3884 return (FstHalf | (SndHalf << 4));
3885}
3886
Craig Topper70b883b2011-11-28 10:14:51 +00003887/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003888/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3889/// Note that VPERMIL mask matching is different depending whether theunderlying
3890/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3891/// to the same elements of the low, but to the higher half of the source.
3892/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003893/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003894static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3895 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003896 return false;
3897
Craig Topperc612d792012-01-02 09:17:37 +00003898 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003899 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003900 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003901 return false;
3902
Craig Topperc612d792012-01-02 09:17:37 +00003903 unsigned NumLanes = VT.getSizeInBits()/128;
3904 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003905 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003906 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003907 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003908 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003909 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003910 continue;
3911 // VPERMILPS handling
3912 if (Mask[i] < 0)
3913 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003914 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003915 return false;
3916 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003917 }
3918
3919 return true;
3920}
3921
Craig Topper5aaffa82012-02-19 02:53:47 +00003922/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003923/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003924/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003925static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003926 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003927 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003928 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003929
3930 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003931 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003932 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003933
Nate Begeman9008ca62009-04-27 18:41:29 +00003934 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003935 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003936
Craig Topperc612d792012-01-02 09:17:37 +00003937 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3939 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3940 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003941 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003942
Evan Cheng39623da2006-04-20 08:58:49 +00003943 return true;
3944}
3945
Evan Chengd9539472006-04-14 21:59:03 +00003946/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3947/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003948/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003949static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003950 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003951 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003952 return false;
3953
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003954 unsigned NumElems = VT.getVectorNumElements();
3955
Craig Topper5a529e42013-01-18 06:44:29 +00003956 if ((VT.is128BitVector() && NumElems != 4) ||
3957 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003958 return false;
3959
3960 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003961 for (unsigned i = 0; i != NumElems; i += 2)
3962 if (!isUndefOrEqual(Mask[i], i+1) ||
3963 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003965
3966 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003967}
3968
3969/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3970/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003971/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003972static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003973 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003974 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003975 return false;
3976
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003977 unsigned NumElems = VT.getVectorNumElements();
3978
Craig Topper5a529e42013-01-18 06:44:29 +00003979 if ((VT.is128BitVector() && NumElems != 4) ||
3980 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003981 return false;
3982
3983 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003984 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003985 if (!isUndefOrEqual(Mask[i], i) ||
3986 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003987 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003988
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003989 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003990}
3991
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003992/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3993/// specifies a shuffle of elements that is suitable for input to 256-bit
3994/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003995static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3996 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003997 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003998
Craig Topper7a9a28b2012-08-12 02:23:29 +00003999 unsigned NumElts = VT.getVectorNumElements();
4000 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004001 return false;
4002
Craig Topperc612d792012-01-02 09:17:37 +00004003 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004004 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004005 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004006 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004007 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004008 return false;
4009 return true;
4010}
4011
Evan Cheng0b457f02008-09-25 20:50:48 +00004012/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004013/// specifies a shuffle of elements that is suitable for input to 128-bit
4014/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004015static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004016 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004017 return false;
4018
Craig Topperc612d792012-01-02 09:17:37 +00004019 unsigned e = VT.getVectorNumElements() / 2;
4020 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004021 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004022 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004023 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004024 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004025 return false;
4026 return true;
4027}
4028
David Greenec38a03e2011-02-03 15:50:00 +00004029/// isVEXTRACTF128Index - Return true if the specified
4030/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4031/// suitable for input to VEXTRACTF128.
4032bool X86::isVEXTRACTF128Index(SDNode *N) {
4033 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4034 return false;
4035
4036 // The index should be aligned on a 128-bit boundary.
4037 uint64_t Index =
4038 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4039
Craig Topper5141d972013-01-18 08:41:28 +00004040 MVT VT = N->getValueType(0).getSimpleVT();
4041 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004042 bool Result = (Index * ElSize) % 128 == 0;
4043
4044 return Result;
4045}
4046
David Greeneccacdc12011-02-04 16:08:29 +00004047/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4048/// operand specifies a subvector insert that is suitable for input to
4049/// VINSERTF128.
4050bool X86::isVINSERTF128Index(SDNode *N) {
4051 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4052 return false;
4053
4054 // The index should be aligned on a 128-bit boundary.
4055 uint64_t Index =
4056 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4057
Craig Topper5141d972013-01-18 08:41:28 +00004058 MVT VT = N->getValueType(0).getSimpleVT();
4059 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004060 bool Result = (Index * ElSize) % 128 == 0;
4061
4062 return Result;
4063}
4064
Evan Cheng63d33002006-03-22 08:01:21 +00004065/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004066/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004067/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004068static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004069 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004070
Craig Topper1a7700a2012-01-19 08:19:12 +00004071 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4072 "Unsupported vector type for PSHUF/SHUFP");
4073
4074 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4075 // independently on 128-bit lanes.
4076 unsigned NumElts = VT.getVectorNumElements();
4077 unsigned NumLanes = VT.getSizeInBits()/128;
4078 unsigned NumLaneElts = NumElts/NumLanes;
4079
4080 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4081 "Only supports 2 or 4 elements per lane");
4082
4083 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004084 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004085 for (unsigned i = 0; i != NumElts; ++i) {
4086 int Elt = N->getMaskElt(i);
4087 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004088 Elt &= NumLaneElts - 1;
4089 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004090 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004091 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004092
Evan Cheng63d33002006-03-22 08:01:21 +00004093 return Mask;
4094}
4095
Evan Cheng506d3df2006-03-29 23:07:14 +00004096/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004097/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004098static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004099 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004100
4101 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4102 "Unsupported vector type for PSHUFHW");
4103
4104 unsigned NumElts = VT.getVectorNumElements();
4105
Evan Cheng506d3df2006-03-29 23:07:14 +00004106 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004107 for (unsigned l = 0; l != NumElts; l += 8) {
4108 // 8 nodes per lane, but we only care about the last 4.
4109 for (unsigned i = 0; i < 4; ++i) {
4110 int Elt = N->getMaskElt(l+i+4);
4111 if (Elt < 0) continue;
4112 Elt &= 0x3; // only 2-bits.
4113 Mask |= Elt << (i * 2);
4114 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004115 }
Craig Topper6b28d352012-05-03 07:12:59 +00004116
Evan Cheng506d3df2006-03-29 23:07:14 +00004117 return Mask;
4118}
4119
4120/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004121/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004122static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004123 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004124
4125 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4126 "Unsupported vector type for PSHUFHW");
4127
4128 unsigned NumElts = VT.getVectorNumElements();
4129
Evan Cheng506d3df2006-03-29 23:07:14 +00004130 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004131 for (unsigned l = 0; l != NumElts; l += 8) {
4132 // 8 nodes per lane, but we only care about the first 4.
4133 for (unsigned i = 0; i < 4; ++i) {
4134 int Elt = N->getMaskElt(l+i);
4135 if (Elt < 0) continue;
4136 Elt &= 0x3; // only 2-bits
4137 Mask |= Elt << (i * 2);
4138 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004139 }
Craig Topper6b28d352012-05-03 07:12:59 +00004140
Evan Cheng506d3df2006-03-29 23:07:14 +00004141 return Mask;
4142}
4143
Nate Begemana09008b2009-10-19 02:17:23 +00004144/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4145/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004146static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004147 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004148 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004149
Craig Topper0e2037b2012-01-20 05:53:00 +00004150 unsigned NumElts = VT.getVectorNumElements();
4151 unsigned NumLanes = VT.getSizeInBits()/128;
4152 unsigned NumLaneElts = NumElts/NumLanes;
4153
4154 int Val = 0;
4155 unsigned i;
4156 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004157 Val = SVOp->getMaskElt(i);
4158 if (Val >= 0)
4159 break;
4160 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004161 if (Val >= (int)NumElts)
4162 Val -= NumElts - NumLaneElts;
4163
Eli Friedman63f8dde2011-07-25 21:36:45 +00004164 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004165 return (Val - i) * EltSize;
4166}
4167
David Greenec38a03e2011-02-03 15:50:00 +00004168/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4169/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4170/// instructions.
4171unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4172 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4173 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4174
4175 uint64_t Index =
4176 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4177
Craig Toppercfcab212013-01-19 08:27:45 +00004178 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4179 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004180
4181 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004182 return Index / NumElemsPerChunk;
4183}
4184
David Greeneccacdc12011-02-04 16:08:29 +00004185/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4186/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4187/// instructions.
4188unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4189 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4190 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4191
4192 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004193 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004194
Craig Toppercfcab212013-01-19 08:27:45 +00004195 MVT VecVT = N->getValueType(0).getSimpleVT();
4196 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004197
4198 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004199 return Index / NumElemsPerChunk;
4200}
4201
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004202/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4203/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4204/// Handles 256-bit.
4205static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004206 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004207
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004208 unsigned NumElts = VT.getVectorNumElements();
4209
Craig Topper095c5282012-04-15 23:48:57 +00004210 assert((VT.is256BitVector() && NumElts == 4) &&
4211 "Unsupported vector type for VPERMQ/VPERMPD");
4212
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004213 unsigned Mask = 0;
4214 for (unsigned i = 0; i != NumElts; ++i) {
4215 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004216 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004217 continue;
4218 Mask |= Elt << (i*2);
4219 }
4220
4221 return Mask;
4222}
Evan Cheng37b73872009-07-30 08:33:02 +00004223/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4224/// constant +0.0.
4225bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004226 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4227 return CN->isNullValue();
4228 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4229 return CFP->getValueAPF().isPosZero();
4230 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004231}
4232
Nate Begeman9008ca62009-04-27 18:41:29 +00004233/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4234/// their permute mask.
4235static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4236 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004237 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004238 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004240
Nate Begeman5a5ca152009-04-29 05:20:52 +00004241 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004242 int Idx = SVOp->getMaskElt(i);
4243 if (Idx >= 0) {
4244 if (Idx < (int)NumElems)
4245 Idx += NumElems;
4246 else
4247 Idx -= NumElems;
4248 }
4249 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004250 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4252 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004253}
4254
Evan Cheng533a0aa2006-04-19 20:35:22 +00004255/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4256/// match movhlps. The lower half elements should come from upper half of
4257/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004258/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004259static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004260 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004261 return false;
4262 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004263 return false;
4264 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004265 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004266 return false;
4267 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004268 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004269 return false;
4270 return true;
4271}
4272
Evan Cheng5ced1d82006-04-06 23:23:56 +00004273/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004274/// is promoted to a vector. It also returns the LoadSDNode by reference if
4275/// required.
4276static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004277 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4278 return false;
4279 N = N->getOperand(0).getNode();
4280 if (!ISD::isNON_EXTLoad(N))
4281 return false;
4282 if (LD)
4283 *LD = cast<LoadSDNode>(N);
4284 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004285}
4286
Dan Gohman65fd6562011-11-03 21:49:52 +00004287// Test whether the given value is a vector value which will be legalized
4288// into a load.
4289static bool WillBeConstantPoolLoad(SDNode *N) {
4290 if (N->getOpcode() != ISD::BUILD_VECTOR)
4291 return false;
4292
4293 // Check for any non-constant elements.
4294 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4295 switch (N->getOperand(i).getNode()->getOpcode()) {
4296 case ISD::UNDEF:
4297 case ISD::ConstantFP:
4298 case ISD::Constant:
4299 break;
4300 default:
4301 return false;
4302 }
4303
4304 // Vectors of all-zeros and all-ones are materialized with special
4305 // instructions rather than being loaded.
4306 return !ISD::isBuildVectorAllZeros(N) &&
4307 !ISD::isBuildVectorAllOnes(N);
4308}
4309
Evan Cheng533a0aa2006-04-19 20:35:22 +00004310/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4311/// match movlp{s|d}. The lower half elements should come from lower half of
4312/// V1 (and in order), and the upper half elements should come from the upper
4313/// half of V2 (and in order). And since V1 will become the source of the
4314/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004315static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004316 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004317 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004318 return false;
4319
Evan Cheng466685d2006-10-09 20:57:25 +00004320 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004321 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004322 // Is V2 is a vector load, don't do this transformation. We will try to use
4323 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004324 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004325 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004326
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004327 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004328
Evan Cheng533a0aa2006-04-19 20:35:22 +00004329 if (NumElems != 2 && NumElems != 4)
4330 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004331 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004332 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004333 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004334 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004335 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004336 return false;
4337 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004338}
4339
Evan Cheng39623da2006-04-20 08:58:49 +00004340/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4341/// all the same.
4342static bool isSplatVector(SDNode *N) {
4343 if (N->getOpcode() != ISD::BUILD_VECTOR)
4344 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004345
Dan Gohman475871a2008-07-27 21:46:04 +00004346 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004347 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4348 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004349 return false;
4350 return true;
4351}
4352
Evan Cheng213d2cf2007-05-17 18:45:50 +00004353/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004354/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004355/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004356static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004357 SDValue V1 = N->getOperand(0);
4358 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004359 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4360 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004362 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004364 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4365 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004366 if (Opc != ISD::BUILD_VECTOR ||
4367 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 return false;
4369 } else if (Idx >= 0) {
4370 unsigned Opc = V1.getOpcode();
4371 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4372 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004373 if (Opc != ISD::BUILD_VECTOR ||
4374 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004375 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004376 }
4377 }
4378 return true;
4379}
4380
4381/// getZeroVector - Returns a vector of specified type with all zero elements.
4382///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004383static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004384 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004385 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004386
Dale Johannesen0488fb62010-09-30 23:57:10 +00004387 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004388 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004389 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004390 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004391 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004392 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4393 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4394 } else { // SSE1
4395 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4396 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4397 }
Craig Topper5a529e42013-01-18 06:44:29 +00004398 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004399 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004400 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4401 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4402 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4403 } else {
4404 // 256-bit logic and arithmetic instructions in AVX are all
4405 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4406 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4407 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4408 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4409 }
Craig Topper9d352402012-04-23 07:24:41 +00004410 } else
4411 llvm_unreachable("Unexpected vector type");
4412
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004413 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004414}
4415
Chris Lattner8a594482007-11-25 00:24:49 +00004416/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004417/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4418/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4419/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004420static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004421 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004422 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004423
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004425 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004426 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004427 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004428 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4429 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4430 } else { // AVX
4431 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004432 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004433 }
Craig Topper5a529e42013-01-18 06:44:29 +00004434 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004435 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004436 } else
4437 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004438
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004439 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004440}
4441
Evan Cheng39623da2006-04-20 08:58:49 +00004442/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4443/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004444static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004445 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004446 if (Mask[i] > (int)NumElems) {
4447 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004448 }
Evan Cheng39623da2006-04-20 08:58:49 +00004449 }
Evan Cheng39623da2006-04-20 08:58:49 +00004450}
4451
Evan Cheng017dcc62006-04-21 01:05:10 +00004452/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4453/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004454static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 SDValue V2) {
4456 unsigned NumElems = VT.getVectorNumElements();
4457 SmallVector<int, 8> Mask;
4458 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004459 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 Mask.push_back(i);
4461 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004462}
4463
Nate Begeman9008ca62009-04-27 18:41:29 +00004464/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004465static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004466 SDValue V2) {
4467 unsigned NumElems = VT.getVectorNumElements();
4468 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004469 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 Mask.push_back(i);
4471 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004472 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004474}
4475
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004477static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 SDValue V2) {
4479 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004481 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 Mask.push_back(i + Half);
4483 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004484 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004486}
4487
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004488// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489// a generic shuffle instruction because the target has no such instructions.
4490// Generate shuffles which repeat i16 and i8 several times until they can be
4491// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004492static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004494 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004496
Nate Begeman9008ca62009-04-27 18:41:29 +00004497 while (NumElems > 4) {
4498 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 EltNo -= NumElems/2;
4503 }
4504 NumElems >>= 1;
4505 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506 return V;
4507}
Eric Christopherfd179292009-08-27 18:07:15 +00004508
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4510static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4511 EVT VT = V.getValueType();
4512 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513
Craig Topper5a529e42013-01-18 06:44:29 +00004514 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004515 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004516 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004517 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4518 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004519 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004520 // To use VPERMILPS to splat scalars, the second half of indicies must
4521 // refer to the higher part, which is a duplication of the lower one,
4522 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004523 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4524 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004525
4526 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4527 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4528 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004529 } else
4530 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004531
4532 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4533}
4534
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004535/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004536static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4537 EVT SrcVT = SV->getValueType(0);
4538 SDValue V1 = SV->getOperand(0);
4539 DebugLoc dl = SV->getDebugLoc();
4540
4541 int EltNo = SV->getSplatIndex();
4542 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004543 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004544
Craig Topper5a529e42013-01-18 06:44:29 +00004545 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4546 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004547
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004548 // Extract the 128-bit part containing the splat element and update
4549 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004550 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004551 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4552 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004553 EltNo -= NumElems/2;
4554 }
4555
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004556 // All i16 and i8 vector types can't be used directly by a generic shuffle
4557 // instruction because the target has no such instruction. Generate shuffles
4558 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004559 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004560 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004561 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004562 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004563
4564 // Recreate the 256-bit vector and place the same 128-bit vector
4565 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004566 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004567 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004568 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004569 }
4570
4571 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004572}
4573
Evan Chengba05f722006-04-21 23:03:30 +00004574/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004575/// vector of zero or undef vector. This produces a shuffle where the low
4576/// element of V2 is swizzled into the zero/undef vector, landing at element
4577/// 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 +00004578static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004579 bool IsZero,
4580 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004581 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004582 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004583 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004584 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004585 unsigned NumElems = VT.getVectorNumElements();
4586 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004587 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004588 // If this is the insertion idx, put the low elt of V2 here.
4589 MaskVec.push_back(i == Idx ? NumElems : i);
4590 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004591}
4592
Craig Toppera1ffc682012-03-20 06:42:26 +00004593/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4594/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004595/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004596static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004597 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004598 unsigned NumElems = VT.getVectorNumElements();
4599 SDValue ImmN;
4600
Craig Topper89f4e662012-03-20 07:17:59 +00004601 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004602 switch(N->getOpcode()) {
4603 case X86ISD::SHUFP:
4604 ImmN = N->getOperand(N->getNumOperands()-1);
4605 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4606 break;
4607 case X86ISD::UNPCKH:
4608 DecodeUNPCKHMask(VT, Mask);
4609 break;
4610 case X86ISD::UNPCKL:
4611 DecodeUNPCKLMask(VT, Mask);
4612 break;
4613 case X86ISD::MOVHLPS:
4614 DecodeMOVHLPSMask(NumElems, Mask);
4615 break;
4616 case X86ISD::MOVLHPS:
4617 DecodeMOVLHPSMask(NumElems, Mask);
4618 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004619 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004620 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004621 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004622 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004623 case X86ISD::PSHUFD:
4624 case X86ISD::VPERMILP:
4625 ImmN = N->getOperand(N->getNumOperands()-1);
4626 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004627 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004628 break;
4629 case X86ISD::PSHUFHW:
4630 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004631 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004632 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004633 break;
4634 case X86ISD::PSHUFLW:
4635 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004636 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004637 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004638 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004639 case X86ISD::VPERMI:
4640 ImmN = N->getOperand(N->getNumOperands()-1);
4641 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4642 IsUnary = true;
4643 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004644 case X86ISD::MOVSS:
4645 case X86ISD::MOVSD: {
4646 // The index 0 always comes from the first element of the second source,
4647 // this is why MOVSS and MOVSD are used in the first place. The other
4648 // elements come from the other positions of the first source vector
4649 Mask.push_back(NumElems);
4650 for (unsigned i = 1; i != NumElems; ++i) {
4651 Mask.push_back(i);
4652 }
4653 break;
4654 }
4655 case X86ISD::VPERM2X128:
4656 ImmN = N->getOperand(N->getNumOperands()-1);
4657 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004658 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004659 break;
4660 case X86ISD::MOVDDUP:
4661 case X86ISD::MOVLHPD:
4662 case X86ISD::MOVLPD:
4663 case X86ISD::MOVLPS:
4664 case X86ISD::MOVSHDUP:
4665 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004666 // Not yet implemented
4667 return false;
4668 default: llvm_unreachable("unknown target shuffle node");
4669 }
4670
4671 return true;
4672}
4673
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004674/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4675/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004676static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004677 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004678 if (Depth == 6)
4679 return SDValue(); // Limit search depth.
4680
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004681 SDValue V = SDValue(N, 0);
4682 EVT VT = V.getValueType();
4683 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684
4685 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4686 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004687 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004688
Craig Topper3d092db2012-03-21 02:14:01 +00004689 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690 return DAG.getUNDEF(VT.getVectorElementType());
4691
Craig Topperd156dc12012-02-06 07:17:51 +00004692 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004693 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4694 : SV->getOperand(1);
4695 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004696 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004697
4698 // Recurse into target specific vector shuffles to find scalars.
4699 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004700 MVT ShufVT = V.getValueType().getSimpleVT();
4701 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004702 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004703 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004704
Craig Topperd978c542012-05-06 19:46:21 +00004705 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004706 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004707
Craig Topper3d092db2012-03-21 02:14:01 +00004708 int Elt = ShuffleMask[Index];
4709 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004710 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004711
Craig Topper3d092db2012-03-21 02:14:01 +00004712 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004713 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004714 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004715 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004716 }
4717
4718 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004719 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004720 V = V.getOperand(0);
4721 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004722 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004724 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725 return SDValue();
4726 }
4727
4728 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4729 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004730 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004731
4732 if (V.getOpcode() == ISD::BUILD_VECTOR)
4733 return V.getOperand(Index);
4734
4735 return SDValue();
4736}
4737
4738/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4739/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004740/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004741static
Craig Topper3d092db2012-03-21 02:14:01 +00004742unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004744 unsigned i;
4745 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004747 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004748 if (!(Elt.getNode() &&
4749 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4750 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004751 }
4752
4753 return i;
4754}
4755
Craig Topper3d092db2012-03-21 02:14:01 +00004756/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4757/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004758/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4759static
Craig Topper3d092db2012-03-21 02:14:01 +00004760bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4761 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4762 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004763 bool SeenV1 = false;
4764 bool SeenV2 = false;
4765
Craig Topper3d092db2012-03-21 02:14:01 +00004766 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004767 int Idx = SVOp->getMaskElt(i);
4768 // Ignore undef indicies
4769 if (Idx < 0)
4770 continue;
4771
Craig Topper3d092db2012-03-21 02:14:01 +00004772 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004773 SeenV1 = true;
4774 else
4775 SeenV2 = true;
4776
4777 // Only accept consecutive elements from the same vector
4778 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4779 return false;
4780 }
4781
4782 OpNum = SeenV1 ? 0 : 1;
4783 return true;
4784}
4785
4786/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4787/// logical left shift of a vector.
4788static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4789 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4790 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4791 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4792 false /* check zeros from right */, DAG);
4793 unsigned OpSrc;
4794
4795 if (!NumZeros)
4796 return false;
4797
4798 // Considering the elements in the mask that are not consecutive zeros,
4799 // check if they consecutively come from only one of the source vectors.
4800 //
4801 // V1 = {X, A, B, C} 0
4802 // \ \ \ /
4803 // vector_shuffle V1, V2 <1, 2, 3, X>
4804 //
4805 if (!isShuffleMaskConsecutive(SVOp,
4806 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004807 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004808 NumZeros, // Where to start looking in the src vector
4809 NumElems, // Number of elements in vector
4810 OpSrc)) // Which source operand ?
4811 return false;
4812
4813 isLeft = false;
4814 ShAmt = NumZeros;
4815 ShVal = SVOp->getOperand(OpSrc);
4816 return true;
4817}
4818
4819/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4820/// logical left shift of a vector.
4821static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4822 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4823 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4824 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4825 true /* check zeros from left */, DAG);
4826 unsigned OpSrc;
4827
4828 if (!NumZeros)
4829 return false;
4830
4831 // Considering the elements in the mask that are not consecutive zeros,
4832 // check if they consecutively come from only one of the source vectors.
4833 //
4834 // 0 { A, B, X, X } = V2
4835 // / \ / /
4836 // vector_shuffle V1, V2 <X, X, 4, 5>
4837 //
4838 if (!isShuffleMaskConsecutive(SVOp,
4839 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004840 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004841 0, // Where to start looking in the src vector
4842 NumElems, // Number of elements in vector
4843 OpSrc)) // Which source operand ?
4844 return false;
4845
4846 isLeft = true;
4847 ShAmt = NumZeros;
4848 ShVal = SVOp->getOperand(OpSrc);
4849 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004850}
4851
4852/// isVectorShift - Returns true if the shuffle can be implemented as a
4853/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004854static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004855 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004856 // Although the logic below support any bitwidth size, there are no
4857 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004858 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004859 return false;
4860
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004861 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4862 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4863 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004864
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004865 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004866}
4867
Evan Chengc78d3b42006-04-24 18:01:45 +00004868/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4869///
Dan Gohman475871a2008-07-27 21:46:04 +00004870static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004872 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004873 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004874 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004876 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004877
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004878 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004879 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 bool First = true;
4881 for (unsigned i = 0; i < 16; ++i) {
4882 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4883 if (ThisIsNonZero && First) {
4884 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004885 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004886 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 First = false;
4889 }
4890
4891 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4894 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004895 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 }
4898 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4900 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4901 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004904 } else
4905 ThisElt = LastElt;
4906
Gabor Greifba36cb52008-08-28 21:40:38 +00004907 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004909 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004910 }
4911 }
4912
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004913 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914}
4915
Bill Wendlinga348c562007-03-22 18:42:45 +00004916/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004917///
Dan Gohman475871a2008-07-27 21:46:04 +00004918static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004919 unsigned NumNonZero, unsigned NumZero,
4920 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004921 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004922 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004923 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004924 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004925
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004926 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004928 bool First = true;
4929 for (unsigned i = 0; i < 8; ++i) {
4930 bool isNonZero = (NonZeros & (1 << i)) != 0;
4931 if (isNonZero) {
4932 if (First) {
4933 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004934 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004935 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004937 First = false;
4938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004939 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004941 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004942 }
4943 }
4944
4945 return V;
4946}
4947
Evan Chengf26ffe92008-05-29 08:22:04 +00004948/// getVShift - Return a vector logical shift node.
4949///
Owen Andersone50ed302009-08-10 22:56:29 +00004950static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004951 unsigned NumBits, SelectionDAG &DAG,
4952 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004953 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004954 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004955 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004956 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4957 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004958 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004959 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004960 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004961}
4962
Dan Gohman475871a2008-07-27 21:46:04 +00004963SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004964X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004965 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004966
Evan Chengc3630942009-12-09 21:00:30 +00004967 // Check if the scalar load can be widened into a vector load. And if
4968 // the address is "base + cst" see if the cst can be "absorbed" into
4969 // the shuffle mask.
4970 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4971 SDValue Ptr = LD->getBasePtr();
4972 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4973 return SDValue();
4974 EVT PVT = LD->getValueType(0);
4975 if (PVT != MVT::i32 && PVT != MVT::f32)
4976 return SDValue();
4977
4978 int FI = -1;
4979 int64_t Offset = 0;
4980 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4981 FI = FINode->getIndex();
4982 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004983 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004984 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4985 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4986 Offset = Ptr.getConstantOperandVal(1);
4987 Ptr = Ptr.getOperand(0);
4988 } else {
4989 return SDValue();
4990 }
4991
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004992 // FIXME: 256-bit vector instructions don't require a strict alignment,
4993 // improve this code to support it better.
4994 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004995 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004997 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004998 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004999 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005000 // Can't change the alignment. FIXME: It's possible to compute
5001 // the exact stack offset and reference FI + adjust offset instead.
5002 // If someone *really* cares about this. That's the way to implement it.
5003 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005004 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005005 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005006 }
5007 }
5008
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005009 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005010 // Ptr + (Offset & ~15).
5011 if (Offset < 0)
5012 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005013 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005014 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005015 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005016 if (StartOffset)
5017 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5018 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5019
5020 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005021 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005022
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005023 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5024 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005025 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005026 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005027
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005028 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005029 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005030 Mask.push_back(EltNo);
5031
Craig Toppercc3000632012-01-30 07:50:31 +00005032 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005033 }
5034
5035 return SDValue();
5036}
5037
Michael J. Spencerec38de22010-10-10 22:04:20 +00005038/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5039/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005040/// load which has the same value as a build_vector whose operands are 'elts'.
5041///
5042/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005043///
Nate Begeman1449f292010-03-24 22:19:06 +00005044/// FIXME: we'd also like to handle the case where the last elements are zero
5045/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5046/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005047static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005048 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005049 EVT EltVT = VT.getVectorElementType();
5050 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005051
Nate Begemanfdea31a2010-03-24 20:49:50 +00005052 LoadSDNode *LDBase = NULL;
5053 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005054
Nate Begeman1449f292010-03-24 22:19:06 +00005055 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005056 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005057 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005058 for (unsigned i = 0; i < NumElems; ++i) {
5059 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005060
Nate Begemanfdea31a2010-03-24 20:49:50 +00005061 if (!Elt.getNode() ||
5062 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5063 return SDValue();
5064 if (!LDBase) {
5065 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5066 return SDValue();
5067 LDBase = cast<LoadSDNode>(Elt.getNode());
5068 LastLoadedElt = i;
5069 continue;
5070 }
5071 if (Elt.getOpcode() == ISD::UNDEF)
5072 continue;
5073
5074 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5075 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5076 return SDValue();
5077 LastLoadedElt = i;
5078 }
Nate Begeman1449f292010-03-24 22:19:06 +00005079
5080 // If we have found an entire vector of loads and undefs, then return a large
5081 // load of the entire vector width starting at the base pointer. If we found
5082 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005083 if (LastLoadedElt == NumElems - 1) {
5084 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005085 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005086 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005087 LDBase->isVolatile(), LDBase->isNonTemporal(),
5088 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005089 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005090 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005091 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005092 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005093 }
5094 if (NumElems == 4 && LastLoadedElt == 1 &&
5095 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005096 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5097 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005098 SDValue ResNode =
5099 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5100 LDBase->getPointerInfo(),
5101 LDBase->getAlignment(),
5102 false/*isVolatile*/, true/*ReadMem*/,
5103 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005104
5105 // Make sure the newly-created LOAD is in the same position as LDBase in
5106 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5107 // update uses of LDBase's output chain to use the TokenFactor.
5108 if (LDBase->hasAnyUseOfValue(1)) {
5109 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5110 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5111 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5112 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5113 SDValue(ResNode.getNode(), 1));
5114 }
5115
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005116 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005117 }
5118 return SDValue();
5119}
5120
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5122/// to generate a splat value for the following cases:
5123/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005124/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005125/// a scalar load, or a constant.
5126/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005127/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005128SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005129X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005130 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005131 return SDValue();
5132
Craig Topper45e1c752013-01-20 00:38:18 +00005133 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005134 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005135
Craig Topper5da8a802012-05-04 05:49:51 +00005136 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5137 "Unsupported vector type for broadcast.");
5138
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005139 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005140 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005141
Nadav Rotem9d68b062012-04-08 12:54:54 +00005142 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005143 default:
5144 // Unknown pattern found.
5145 return SDValue();
5146
5147 case ISD::BUILD_VECTOR: {
5148 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005149 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005150 return SDValue();
5151
Nadav Rotem9d68b062012-04-08 12:54:54 +00005152 Ld = Op.getOperand(0);
5153 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5154 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005155
5156 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005157 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005158 // Constants may have multiple users.
5159 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005160 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005161 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005162 }
5163
5164 case ISD::VECTOR_SHUFFLE: {
5165 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5166
5167 // Shuffles must have a splat mask where the first element is
5168 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005169 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005170 return SDValue();
5171
5172 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005173 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005174 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5175
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005176 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005177 return SDValue();
5178
5179 // Use the register form of the broadcast instruction available on AVX2.
5180 if (VT.is256BitVector())
5181 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5182 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5183 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005184
5185 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005186 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005187 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188
5189 // The scalar_to_vector node and the suspected
5190 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005191 // Constants may have multiple users.
5192 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005193 return SDValue();
5194 break;
5195 }
5196 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005197
Craig Topper7a9a28b2012-08-12 02:23:29 +00005198 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005199
5200 // Handle the broadcasting a single constant scalar from the constant pool
5201 // into a vector. On Sandybridge it is still better to load a constant vector
5202 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005203 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005204 EVT CVT = Ld.getValueType();
5205 assert(!CVT.isVector() && "Must not broadcast a vector type");
5206 unsigned ScalarSize = CVT.getSizeInBits();
5207
Craig Topper5da8a802012-05-04 05:49:51 +00005208 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005209 const Constant *C = 0;
5210 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5211 C = CI->getConstantIntValue();
5212 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5213 C = CF->getConstantFPValue();
5214
5215 assert(C && "Invalid constant type");
5216
Nadav Rotem154819d2012-04-09 07:45:58 +00005217 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005218 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005219 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005220 MachinePointerInfo::getConstantPool(),
5221 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005222
Nadav Rotem9d68b062012-04-08 12:54:54 +00005223 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5224 }
5225 }
5226
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005227 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005228 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5229
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005230 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005231 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005232 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5233 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5234
5235 // The scalar source must be a normal load.
5236 if (!IsLoad)
5237 return SDValue();
5238
Craig Topper5da8a802012-05-04 05:49:51 +00005239 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005240 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005241
Craig Toppera9376332012-01-10 08:23:59 +00005242 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005243 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005244 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005245 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005246 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005247 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005248
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005249 // Unsupported broadcast.
5250 return SDValue();
5251}
5252
Evan Chengc3630942009-12-09 21:00:30 +00005253SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005254X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5255 EVT VT = Op.getValueType();
5256
5257 // Skip if insert_vec_elt is not supported.
5258 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5259 return SDValue();
5260
5261 DebugLoc DL = Op.getDebugLoc();
5262 unsigned NumElems = Op.getNumOperands();
5263
5264 SDValue VecIn1;
5265 SDValue VecIn2;
5266 SmallVector<unsigned, 4> InsertIndices;
5267 SmallVector<int, 8> Mask(NumElems, -1);
5268
5269 for (unsigned i = 0; i != NumElems; ++i) {
5270 unsigned Opc = Op.getOperand(i).getOpcode();
5271
5272 if (Opc == ISD::UNDEF)
5273 continue;
5274
5275 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5276 // Quit if more than 1 elements need inserting.
5277 if (InsertIndices.size() > 1)
5278 return SDValue();
5279
5280 InsertIndices.push_back(i);
5281 continue;
5282 }
5283
5284 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5285 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5286
5287 // Quit if extracted from vector of different type.
5288 if (ExtractedFromVec.getValueType() != VT)
5289 return SDValue();
5290
5291 // Quit if non-constant index.
5292 if (!isa<ConstantSDNode>(ExtIdx))
5293 return SDValue();
5294
5295 if (VecIn1.getNode() == 0)
5296 VecIn1 = ExtractedFromVec;
5297 else if (VecIn1 != ExtractedFromVec) {
5298 if (VecIn2.getNode() == 0)
5299 VecIn2 = ExtractedFromVec;
5300 else if (VecIn2 != ExtractedFromVec)
5301 // Quit if more than 2 vectors to shuffle
5302 return SDValue();
5303 }
5304
5305 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5306
5307 if (ExtractedFromVec == VecIn1)
5308 Mask[i] = Idx;
5309 else if (ExtractedFromVec == VecIn2)
5310 Mask[i] = Idx + NumElems;
5311 }
5312
5313 if (VecIn1.getNode() == 0)
5314 return SDValue();
5315
5316 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5317 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5318 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5319 unsigned Idx = InsertIndices[i];
5320 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5321 DAG.getIntPtrConstant(Idx));
5322 }
5323
5324 return NV;
5325}
5326
5327SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005328X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005329 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005330
Craig Topper45e1c752013-01-20 00:38:18 +00005331 MVT VT = Op.getValueType().getSimpleVT();
5332 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005333 unsigned NumElems = Op.getNumOperands();
5334
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005335 // Vectors containing all zeros can be matched by pxor and xorps later
5336 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5337 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5338 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005339 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005340 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005341
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005342 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005343 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005344
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005345 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005346 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5347 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005348 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005349 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005350 return Op;
5351
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005352 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005353 }
5354
Nadav Rotem154819d2012-04-09 07:45:58 +00005355 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005356 if (Broadcast.getNode())
5357 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005358
Owen Andersone50ed302009-08-10 22:56:29 +00005359 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005360
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361 unsigned NumZero = 0;
5362 unsigned NumNonZero = 0;
5363 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005364 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005365 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005367 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005368 if (Elt.getOpcode() == ISD::UNDEF)
5369 continue;
5370 Values.insert(Elt);
5371 if (Elt.getOpcode() != ISD::Constant &&
5372 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005373 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005374 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005375 NumZero++;
5376 else {
5377 NonZeros |= (1 << i);
5378 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 }
5380 }
5381
Chris Lattner97a2a562010-08-26 05:24:29 +00005382 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5383 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005384 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385
Chris Lattner67f453a2008-03-09 05:42:06 +00005386 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005387 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005389 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005390
Chris Lattner62098042008-03-09 01:05:04 +00005391 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5392 // the value are obviously zero, truncate the value to i32 and do the
5393 // insertion that way. Only do this if the value is non-constant or if the
5394 // value is a constant being inserted into element 0. It is cheaper to do
5395 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005396 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005397 (!IsAllConstants || Idx == 0)) {
5398 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005399 // Handle SSE only.
5400 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5401 EVT VecVT = MVT::v4i32;
5402 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005403
Chris Lattner62098042008-03-09 01:05:04 +00005404 // Truncate the value (which may itself be a constant) to i32, and
5405 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005407 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005408 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005409
Chris Lattner62098042008-03-09 01:05:04 +00005410 // Now we have our 32-bit value zero extended in the low element of
5411 // a vector. If Idx != 0, swizzle it into place.
5412 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 SmallVector<int, 4> Mask;
5414 Mask.push_back(Idx);
5415 for (unsigned i = 1; i != VecElts; ++i)
5416 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005417 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005418 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005419 }
Craig Topper07a27622012-01-22 03:07:48 +00005420 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005421 }
5422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005423
Chris Lattner19f79692008-03-08 22:59:52 +00005424 // If we have a constant or non-constant insertion into the low element of
5425 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5426 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005427 // depending on what the source datatype is.
5428 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005429 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005430 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005431
5432 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005433 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005434 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005435 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005436 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5437 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005438 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005439 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005440 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5441 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005442 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005443 }
5444
5445 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005447 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005448 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005449 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005450 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005451 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005452 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005453 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005454 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005455 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005456 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005457 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005458
5459 // Is it a vector logical left shift?
5460 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005461 X86::isZeroNode(Op.getOperand(0)) &&
5462 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005463 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005464 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005465 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005466 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005467 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005468 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005469
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005470 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005471 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005472
Chris Lattner19f79692008-03-08 22:59:52 +00005473 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5474 // is a non-constant being inserted into an element other than the low one,
5475 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5476 // movd/movss) to move this into the low element, then shuffle it into
5477 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005478 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005479 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005480
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005482 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005483 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005484 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005485 MaskVec.push_back(i == Idx ? 0 : 1);
5486 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 }
5488 }
5489
Chris Lattner67f453a2008-03-09 05:42:06 +00005490 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005491 if (Values.size() == 1) {
5492 if (EVTBits == 32) {
5493 // Instead of a shuffle like this:
5494 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5495 // Check if it's possible to issue this instead.
5496 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5497 unsigned Idx = CountTrailingZeros_32(NonZeros);
5498 SDValue Item = Op.getOperand(Idx);
5499 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5500 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5501 }
Dan Gohman475871a2008-07-27 21:46:04 +00005502 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005503 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005504
Dan Gohmana3941172007-07-24 22:55:08 +00005505 // A vector full of immediates; various special cases are already
5506 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005507 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005508 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005509
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005510 // For AVX-length vectors, build the individual 128-bit pieces and use
5511 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005512 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005513 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005514 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005515 V.push_back(Op.getOperand(i));
5516
5517 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5518
5519 // Build both the lower and upper subvector.
5520 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5521 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5522 NumElems/2);
5523
5524 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005525 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005526 }
5527
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005528 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005529 if (EVTBits == 64) {
5530 if (NumNonZero == 1) {
5531 // One half is zero or undef.
5532 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005533 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005534 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005535 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005536 }
Dan Gohman475871a2008-07-27 21:46:04 +00005537 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005538 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539
5540 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005541 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005542 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005543 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005544 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 }
5546
Bill Wendling826f36f2007-03-28 00:57:11 +00005547 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005548 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005549 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005550 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551 }
5552
5553 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005554 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555 if (NumElems == 4 && NumZero > 0) {
5556 for (unsigned i = 0; i < 4; ++i) {
5557 bool isZero = !(NonZeros & (1 << i));
5558 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005559 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560 else
Dale Johannesenace16102009-02-03 19:33:06 +00005561 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005562 }
5563
5564 for (unsigned i = 0; i < 2; ++i) {
5565 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5566 default: break;
5567 case 0:
5568 V[i] = V[i*2]; // Must be a zero vector.
5569 break;
5570 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005571 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005572 break;
5573 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005574 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005575 break;
5576 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005577 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005578 break;
5579 }
5580 }
5581
Benjamin Kramer9c683542012-01-30 15:16:21 +00005582 bool Reverse1 = (NonZeros & 0x3) == 2;
5583 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5584 int MaskVec[] = {
5585 Reverse1 ? 1 : 0,
5586 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005587 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5588 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005589 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005590 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005591 }
5592
Craig Topper7a9a28b2012-08-12 02:23:29 +00005593 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005594 // Check for a build vector of consecutive loads.
5595 for (unsigned i = 0; i < NumElems; ++i)
5596 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005597
Nate Begemanfdea31a2010-03-24 20:49:50 +00005598 // Check for elements which are consecutive loads.
5599 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5600 if (LD.getNode())
5601 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005602
Michael Liaofacace82012-10-19 17:15:18 +00005603 // Check for a build vector from mostly shuffle plus few inserting.
5604 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5605 if (Sh.getNode())
5606 return Sh;
5607
Michael J. Spencerec38de22010-10-10 22:04:20 +00005608 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005609 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005610 SDValue Result;
5611 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5612 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5613 else
5614 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005615
Chris Lattner24faf612010-08-28 17:59:08 +00005616 for (unsigned i = 1; i < NumElems; ++i) {
5617 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5618 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005620 }
5621 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005623
Chris Lattner6e80e442010-08-28 17:15:43 +00005624 // Otherwise, expand into a number of unpckl*, start by extending each of
5625 // our (non-undef) elements to the full vector width with the element in the
5626 // bottom slot of the vector (which generates no code for SSE).
5627 for (unsigned i = 0; i < NumElems; ++i) {
5628 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5629 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5630 else
5631 V[i] = DAG.getUNDEF(VT);
5632 }
5633
5634 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005635 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5636 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5637 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005638 unsigned EltStride = NumElems >> 1;
5639 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005640 for (unsigned i = 0; i < EltStride; ++i) {
5641 // If V[i+EltStride] is undef and this is the first round of mixing,
5642 // then it is safe to just drop this shuffle: V[i] is already in the
5643 // right place, the one element (since it's the first round) being
5644 // inserted as undef can be dropped. This isn't safe for successive
5645 // rounds because they will permute elements within both vectors.
5646 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5647 EltStride == NumElems/2)
5648 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005649
Chris Lattner6e80e442010-08-28 17:15:43 +00005650 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005651 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005652 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005653 }
5654 return V[0];
5655 }
Dan Gohman475871a2008-07-27 21:46:04 +00005656 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657}
5658
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005659// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5660// to create 256-bit vectors from two other 128-bit ones.
5661static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5662 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005663 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005664
Craig Topper7a9a28b2012-08-12 02:23:29 +00005665 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005666
5667 SDValue V1 = Op.getOperand(0);
5668 SDValue V2 = Op.getOperand(1);
5669 unsigned NumElems = ResVT.getVectorNumElements();
5670
Craig Topper4c7972d2012-04-22 18:15:59 +00005671 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005672}
5673
Craig Topper55b24052012-09-11 06:15:32 +00005674static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005675 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005676
5677 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5678 // from two other 128-bit ones.
5679 return LowerAVXCONCAT_VECTORS(Op, DAG);
5680}
5681
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005682// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005683static SDValue
5684LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5685 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005686 SDValue V1 = SVOp->getOperand(0);
5687 SDValue V2 = SVOp->getOperand(1);
5688 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005689 MVT VT = SVOp->getValueType(0).getSimpleVT();
5690 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005691 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005692
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005693 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5694 return SDValue();
5695 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005696 return SDValue();
5697
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005698 // Check the mask for BLEND and build the value.
5699 unsigned MaskValue = 0;
5700 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005701 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005702 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005703
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005704 // Blend for v16i16 should be symetric for the both lanes.
5705 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005706
Craig Topper9b33ef72013-01-21 06:57:59 +00005707 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005708 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005709 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005710
Craig Topper04f74a12013-01-21 07:25:16 +00005711 if ((EltIdx < 0 || EltIdx == (int)i) &&
5712 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005713 continue;
5714
Craig Topper9b33ef72013-01-21 06:57:59 +00005715 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005716 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005717 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5718 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005719 else
Craig Topper1842ba02012-04-23 06:38:28 +00005720 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005721 }
5722
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005723 // Convert i32 vectors to floating point if it is not AVX2.
5724 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005725 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005726 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005727 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5728 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005729 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5730 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5731 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005732
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005733 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5734 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005735 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005736}
5737
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738// v8i16 shuffles - Prefer shuffles in the following order:
5739// 1. [all] pshuflw, pshufhw, optional move
5740// 2. [ssse3] 1 x pshufb
5741// 3. [ssse3] 2 x pshufb + 1 x por
5742// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005743static SDValue
5744LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5745 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005746 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005747 SDValue V1 = SVOp->getOperand(0);
5748 SDValue V2 = SVOp->getOperand(1);
5749 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005751
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 // Determine if more than 1 of the words in each of the low and high quadwords
5753 // of the result come from the same quadword of one of the two inputs. Undef
5754 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005755 unsigned LoQuad[] = { 0, 0, 0, 0 };
5756 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005757 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005759 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005760 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 MaskVals.push_back(EltIdx);
5762 if (EltIdx < 0) {
5763 ++Quad[0];
5764 ++Quad[1];
5765 ++Quad[2];
5766 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005767 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 }
5769 ++Quad[EltIdx / 4];
5770 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005771 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005772
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005774 unsigned MaxQuad = 1;
5775 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 if (LoQuad[i] > MaxQuad) {
5777 BestLoQuad = i;
5778 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005779 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005780 }
5781
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005783 MaxQuad = 1;
5784 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 if (HiQuad[i] > MaxQuad) {
5786 BestHiQuad = i;
5787 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005788 }
5789 }
5790
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005792 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // single pshufb instruction is necessary. If There are more than 2 input
5794 // quads, disable the next transformation since it does not help SSSE3.
5795 bool V1Used = InputQuads[0] || InputQuads[1];
5796 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005797 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005799 BestLoQuad = InputQuads[0] ? 0 : 1;
5800 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 }
5802 if (InputQuads.count() > 2) {
5803 BestLoQuad = -1;
5804 BestHiQuad = -1;
5805 }
5806 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005807
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5809 // the shuffle mask. If a quad is scored as -1, that means that it contains
5810 // words from all 4 input quadwords.
5811 SDValue NewV;
5812 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005813 int MaskV[] = {
5814 BestLoQuad < 0 ? 0 : BestLoQuad,
5815 BestHiQuad < 0 ? 1 : BestHiQuad
5816 };
Eric Christopherfd179292009-08-27 18:07:15 +00005817 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005818 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5819 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5820 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005821
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5823 // source words for the shuffle, to aid later transformations.
5824 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005825 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005828 if (idx != (int)i)
5829 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005831 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 AllWordsInNewV = false;
5833 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005834 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005835
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5837 if (AllWordsInNewV) {
5838 for (int i = 0; i != 8; ++i) {
5839 int idx = MaskVals[i];
5840 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005841 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005842 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 if ((idx != i) && idx < 4)
5844 pshufhw = false;
5845 if ((idx != i) && idx > 3)
5846 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005847 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 V1 = NewV;
5849 V2Used = false;
5850 BestLoQuad = 0;
5851 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005852 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005853
Nate Begemanb9a47b82009-02-23 08:49:38 +00005854 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5855 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005856 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005857 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5858 unsigned TargetMask = 0;
5859 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005860 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005861 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5862 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5863 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005864 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005865 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005866 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005867 }
Eric Christopherfd179292009-08-27 18:07:15 +00005868
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005869 // Promote splats to a larger type which usually leads to more efficient code.
5870 // FIXME: Is this true if pshufb is available?
5871 if (SVOp->isSplat())
5872 return PromoteSplat(SVOp, DAG);
5873
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 // If we have SSSE3, and all words of the result are from 1 input vector,
5875 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5876 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005877 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005881 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 // mask, and elements that come from V1 in the V2 mask, so that the two
5883 // results can be OR'd together.
5884 bool TwoInputs = V1Used && V2Used;
5885 for (unsigned i = 0; i != 8; ++i) {
5886 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005887 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5888 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005889 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005890 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005892 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005893 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005894 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005897 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005898
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 // Calculate the shuffle mask for the second input, shuffle it, and
5900 // OR it with the first shuffled input.
5901 pshufbMask.clear();
5902 for (unsigned i = 0; i != 8; ++i) {
5903 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005904 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5905 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5906 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5907 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005909 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005910 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005911 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005912 MVT::v16i8, &pshufbMask[0], 16));
5913 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 }
5916
5917 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5918 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005919 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005921 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 for (int i = 0; i != 4; ++i) {
5923 int idx = MaskVals[i];
5924 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 InOrder.set(i);
5926 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005927 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 }
5930 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005932 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005933
Craig Topperdd637ae2012-02-19 05:41:45 +00005934 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5935 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005936 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005937 NewV.getOperand(0),
5938 getShufflePSHUFLWImmediate(SVOp), DAG);
5939 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005940 }
Eric Christopherfd179292009-08-27 18:07:15 +00005941
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5943 // and update MaskVals with the new element order.
5944 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005945 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 for (unsigned i = 4; i != 8; ++i) {
5947 int idx = MaskVals[i];
5948 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 InOrder.set(i);
5950 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005951 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 }
5954 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005956 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005957
Craig Topperdd637ae2012-02-19 05:41:45 +00005958 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5959 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005960 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005961 NewV.getOperand(0),
5962 getShufflePSHUFHWImmediate(SVOp), DAG);
5963 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 }
Eric Christopherfd179292009-08-27 18:07:15 +00005965
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 // In case BestHi & BestLo were both -1, which means each quadword has a word
5967 // from each of the four input quadwords, calculate the InOrder bitvector now
5968 // before falling through to the insert/extract cleanup.
5969 if (BestLoQuad == -1 && BestHiQuad == -1) {
5970 NewV = V1;
5971 for (int i = 0; i != 8; ++i)
5972 if (MaskVals[i] < 0 || MaskVals[i] == i)
5973 InOrder.set(i);
5974 }
Eric Christopherfd179292009-08-27 18:07:15 +00005975
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 // The other elements are put in the right place using pextrw and pinsrw.
5977 for (unsigned i = 0; i != 8; ++i) {
5978 if (InOrder[i])
5979 continue;
5980 int EltIdx = MaskVals[i];
5981 if (EltIdx < 0)
5982 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005983 SDValue ExtOp = (EltIdx < 8) ?
5984 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5985 DAG.getIntPtrConstant(EltIdx)) :
5986 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 DAG.getIntPtrConstant(i));
5990 }
5991 return NewV;
5992}
5993
5994// v16i8 shuffles - Prefer shuffles in the following order:
5995// 1. [ssse3] 1 x pshufb
5996// 2. [ssse3] 2 x pshufb + 1 x por
5997// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5998static
Nate Begeman9008ca62009-04-27 18:41:29 +00005999SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006000 SelectionDAG &DAG,
6001 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006002 SDValue V1 = SVOp->getOperand(0);
6003 SDValue V2 = SVOp->getOperand(1);
6004 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006005 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006006
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006007 // Promote splats to a larger type which usually leads to more efficient code.
6008 // FIXME: Is this true if pshufb is available?
6009 if (SVOp->isSplat())
6010 return PromoteSplat(SVOp, DAG);
6011
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006013 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006015
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006017 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006019
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006021 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 //
6023 // Otherwise, we have elements from both input vectors, and must zero out
6024 // elements that come from V2 in the first mask, and V1 in the second mask
6025 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 for (unsigned i = 0; i != 16; ++i) {
6027 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006028 if (EltIdx < 0 || EltIdx >= 16)
6029 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006033 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006035
6036 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6037 // the 2nd operand if it's undefined or zero.
6038 if (V2.getOpcode() == ISD::UNDEF ||
6039 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006040 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006041
Nate Begemanb9a47b82009-02-23 08:49:38 +00006042 // Calculate the shuffle mask for the second input, shuffle it, and
6043 // OR it with the first shuffled input.
6044 pshufbMask.clear();
6045 for (unsigned i = 0; i != 16; ++i) {
6046 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006047 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006048 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006049 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006051 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 MVT::v16i8, &pshufbMask[0], 16));
6053 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 }
Eric Christopherfd179292009-08-27 18:07:15 +00006055
Nate Begemanb9a47b82009-02-23 08:49:38 +00006056 // No SSSE3 - Calculate in place words and then fix all out of place words
6057 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6058 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006059 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6060 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006061 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006062 for (int i = 0; i != 8; ++i) {
6063 int Elt0 = MaskVals[i*2];
6064 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006065
Nate Begemanb9a47b82009-02-23 08:49:38 +00006066 // This word of the result is all undef, skip it.
6067 if (Elt0 < 0 && Elt1 < 0)
6068 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006069
Nate Begemanb9a47b82009-02-23 08:49:38 +00006070 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006071 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006072 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006073
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6075 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6076 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006077
6078 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6079 // using a single extract together, load it and store it.
6080 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006082 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006083 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006084 DAG.getIntPtrConstant(i));
6085 continue;
6086 }
6087
Nate Begemanb9a47b82009-02-23 08:49:38 +00006088 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006089 // source byte is not also odd, shift the extracted word left 8 bits
6090 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006091 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006092 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 DAG.getIntPtrConstant(Elt1 / 2));
6094 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006096 DAG.getConstant(8,
6097 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006098 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6100 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006101 }
6102 // If Elt0 is defined, extract it from the appropriate source. If the
6103 // source byte is not also even, shift the extracted word right 8 bits. If
6104 // Elt1 was also defined, OR the extracted values together before
6105 // inserting them in the result.
6106 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006107 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006108 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6109 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006110 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006111 DAG.getConstant(8,
6112 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006113 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006114 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6115 DAG.getConstant(0x00FF, MVT::i16));
6116 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006117 : InsElt0;
6118 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006120 DAG.getIntPtrConstant(i));
6121 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006122 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006123}
6124
Elena Demikhovsky41789462012-09-06 12:42:01 +00006125// v32i8 shuffles - Translate to VPSHUFB if possible.
6126static
6127SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006128 const X86Subtarget *Subtarget,
6129 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006130 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006131 SDValue V1 = SVOp->getOperand(0);
6132 SDValue V2 = SVOp->getOperand(1);
6133 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006134 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006135
6136 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006137 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6138 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006139
Michael Liao471b9172012-10-03 23:43:52 +00006140 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006141 // (1) one of input vector is undefined or zeroinitializer.
6142 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6143 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006144 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006145 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006146 return SDValue();
6147
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006148 if (V1IsAllZero && !V2IsAllZero) {
6149 CommuteVectorShuffleMask(MaskVals, 32);
6150 V1 = V2;
6151 }
6152 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006153 for (unsigned i = 0; i != 32; i++) {
6154 int EltIdx = MaskVals[i];
6155 if (EltIdx < 0 || EltIdx >= 32)
6156 EltIdx = 0x80;
6157 else {
6158 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6159 // Cross lane is not allowed.
6160 return SDValue();
6161 EltIdx &= 0xf;
6162 }
6163 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6164 }
6165 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6166 DAG.getNode(ISD::BUILD_VECTOR, dl,
6167 MVT::v32i8, &pshufbMask[0], 32));
6168}
6169
Evan Cheng7a831ce2007-12-15 03:00:47 +00006170/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006171/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006172/// done when every pair / quad of shuffle mask elements point to elements in
6173/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006174/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006175static
Nate Begeman9008ca62009-04-27 18:41:29 +00006176SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006177 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006178 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006179 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006180 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006181 MVT NewVT;
6182 unsigned Scale;
6183 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006184 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006185 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6186 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6187 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6188 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6189 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6190 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006191 }
6192
Nate Begeman9008ca62009-04-27 18:41:29 +00006193 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006194 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006195 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006196 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 int EltIdx = SVOp->getMaskElt(i+j);
6198 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006199 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006200 if (StartIdx < 0)
6201 StartIdx = (EltIdx / Scale);
6202 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006203 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006204 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006205 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006206 }
6207
Craig Topper11ac1f82012-05-04 04:08:44 +00006208 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6209 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006210 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006211}
6212
Evan Chengd880b972008-05-09 21:53:03 +00006213/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006214///
Craig Topperf84b7502013-01-20 00:50:58 +00006215static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006216 SDValue SrcOp, SelectionDAG &DAG,
6217 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006219 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006220 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006221 LD = dyn_cast<LoadSDNode>(SrcOp);
6222 if (!LD) {
6223 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6224 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006225 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006226 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006227 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006228 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006229 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006230 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006232 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006233 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6234 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6235 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006236 SrcOp.getOperand(0)
6237 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006238 }
6239 }
6240 }
6241
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006242 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006243 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006244 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006245 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006246}
6247
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006248/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6249/// which could not be matched by any known target speficic shuffle
6250static SDValue
6251LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006252
6253 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6254 if (NewOp.getNode())
6255 return NewOp;
6256
Craig Topper657a99c2013-01-19 23:36:09 +00006257 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006258
Craig Topper8f35c132012-01-20 09:29:03 +00006259 unsigned NumElems = VT.getVectorNumElements();
6260 unsigned NumLaneElems = NumElems / 2;
6261
Craig Topper8f35c132012-01-20 09:29:03 +00006262 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006263 MVT EltVT = VT.getVectorElementType();
6264 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006265 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006266
Craig Topper9a2b6e12012-04-06 07:45:23 +00006267 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006268 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006269 // Build a shuffle mask for the output, discovering on the fly which
6270 // input vectors to use as shuffle operands (recorded in InputUsed).
6271 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006272 // out with UseBuildVector set.
6273 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006274 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006275 unsigned LaneStart = l * NumLaneElems;
6276 for (unsigned i = 0; i != NumLaneElems; ++i) {
6277 // The mask element. This indexes into the input.
6278 int Idx = SVOp->getMaskElt(i+LaneStart);
6279 if (Idx < 0) {
6280 // the mask element does not index into any input vector.
6281 Mask.push_back(-1);
6282 continue;
6283 }
Craig Topper8f35c132012-01-20 09:29:03 +00006284
Craig Topper9a2b6e12012-04-06 07:45:23 +00006285 // The input vector this mask element indexes into.
6286 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006287
Craig Topper9a2b6e12012-04-06 07:45:23 +00006288 // Turn the index into an offset from the start of the input vector.
6289 Idx -= Input * NumLaneElems;
6290
6291 // Find or create a shuffle vector operand to hold this input.
6292 unsigned OpNo;
6293 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6294 if (InputUsed[OpNo] == Input)
6295 // This input vector is already an operand.
6296 break;
6297 if (InputUsed[OpNo] < 0) {
6298 // Create a new operand for this input vector.
6299 InputUsed[OpNo] = Input;
6300 break;
6301 }
6302 }
6303
6304 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006305 // More than two input vectors used! Give up on trying to create a
6306 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6307 UseBuildVector = true;
6308 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006309 }
6310
6311 // Add the mask index for the new shuffle vector.
6312 Mask.push_back(Idx + OpNo * NumLaneElems);
6313 }
6314
Craig Topper8ae97ba2012-05-21 06:40:16 +00006315 if (UseBuildVector) {
6316 SmallVector<SDValue, 16> SVOps;
6317 for (unsigned i = 0; i != NumLaneElems; ++i) {
6318 // The mask element. This indexes into the input.
6319 int Idx = SVOp->getMaskElt(i+LaneStart);
6320 if (Idx < 0) {
6321 SVOps.push_back(DAG.getUNDEF(EltVT));
6322 continue;
6323 }
6324
6325 // The input vector this mask element indexes into.
6326 int Input = Idx / NumElems;
6327
6328 // Turn the index into an offset from the start of the input vector.
6329 Idx -= Input * NumElems;
6330
6331 // Extract the vector element by hand.
6332 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6333 SVOp->getOperand(Input),
6334 DAG.getIntPtrConstant(Idx)));
6335 }
6336
6337 // Construct the output using a BUILD_VECTOR.
6338 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6339 SVOps.size());
6340 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006341 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006342 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006343 } else {
6344 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006345 (InputUsed[0] % 2) * NumLaneElems,
6346 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006347 // If only one input was used, use an undefined vector for the other.
6348 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6349 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006350 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006351 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006352 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006353 }
6354
6355 Mask.clear();
6356 }
Craig Topper8f35c132012-01-20 09:29:03 +00006357
6358 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006359 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006360}
6361
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006362/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6363/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006364static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006365LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006366 SDValue V1 = SVOp->getOperand(0);
6367 SDValue V2 = SVOp->getOperand(1);
6368 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006369 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006370
Craig Topper7a9a28b2012-08-12 02:23:29 +00006371 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006372
Benjamin Kramer9c683542012-01-30 15:16:21 +00006373 std::pair<int, int> Locs[4];
6374 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006375 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006376
Evan Chengace3c172008-07-22 21:13:36 +00006377 unsigned NumHi = 0;
6378 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006379 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006380 int Idx = PermMask[i];
6381 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006382 Locs[i] = std::make_pair(-1, -1);
6383 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6385 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006386 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006387 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006388 NumLo++;
6389 } else {
6390 Locs[i] = std::make_pair(1, NumHi);
6391 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006392 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006393 NumHi++;
6394 }
6395 }
6396 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006397
Evan Chengace3c172008-07-22 21:13:36 +00006398 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006399 // If no more than two elements come from either vector. This can be
6400 // implemented with two shuffles. First shuffle gather the elements.
6401 // The second shuffle, which takes the first shuffle as both of its
6402 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006403 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006404
Benjamin Kramer9c683542012-01-30 15:16:21 +00006405 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006406
Benjamin Kramer9c683542012-01-30 15:16:21 +00006407 for (unsigned i = 0; i != 4; ++i)
6408 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006409 unsigned Idx = (i < 2) ? 0 : 4;
6410 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006411 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006412 }
Evan Chengace3c172008-07-22 21:13:36 +00006413
Nate Begeman9008ca62009-04-27 18:41:29 +00006414 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006415 }
6416
6417 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006418 // Otherwise, we must have three elements from one vector, call it X, and
6419 // one element from the other, call it Y. First, use a shufps to build an
6420 // intermediate vector with the one element from Y and the element from X
6421 // that will be in the same half in the final destination (the indexes don't
6422 // matter). Then, use a shufps to build the final vector, taking the half
6423 // containing the element from Y from the intermediate, and the other half
6424 // from X.
6425 if (NumHi == 3) {
6426 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006427 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006428 std::swap(V1, V2);
6429 }
6430
6431 // Find the element from V2.
6432 unsigned HiIndex;
6433 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006434 int Val = PermMask[HiIndex];
6435 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006436 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006437 if (Val >= 4)
6438 break;
6439 }
6440
Nate Begeman9008ca62009-04-27 18:41:29 +00006441 Mask1[0] = PermMask[HiIndex];
6442 Mask1[1] = -1;
6443 Mask1[2] = PermMask[HiIndex^1];
6444 Mask1[3] = -1;
6445 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006446
6447 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006448 Mask1[0] = PermMask[0];
6449 Mask1[1] = PermMask[1];
6450 Mask1[2] = HiIndex & 1 ? 6 : 4;
6451 Mask1[3] = HiIndex & 1 ? 4 : 6;
6452 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006453 }
Craig Topper69947b92012-04-23 06:57:04 +00006454
6455 Mask1[0] = HiIndex & 1 ? 2 : 0;
6456 Mask1[1] = HiIndex & 1 ? 0 : 2;
6457 Mask1[2] = PermMask[2];
6458 Mask1[3] = PermMask[3];
6459 if (Mask1[2] >= 0)
6460 Mask1[2] += 4;
6461 if (Mask1[3] >= 0)
6462 Mask1[3] += 4;
6463 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006464 }
6465
6466 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006467 int LoMask[] = { -1, -1, -1, -1 };
6468 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006469
Benjamin Kramer9c683542012-01-30 15:16:21 +00006470 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006471 unsigned MaskIdx = 0;
6472 unsigned LoIdx = 0;
6473 unsigned HiIdx = 2;
6474 for (unsigned i = 0; i != 4; ++i) {
6475 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006476 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006477 MaskIdx = 1;
6478 LoIdx = 0;
6479 HiIdx = 2;
6480 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006481 int Idx = PermMask[i];
6482 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006483 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006484 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006485 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006486 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006487 LoIdx++;
6488 } else {
6489 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006490 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006491 HiIdx++;
6492 }
6493 }
6494
Nate Begeman9008ca62009-04-27 18:41:29 +00006495 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6496 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006497 int MaskOps[] = { -1, -1, -1, -1 };
6498 for (unsigned i = 0; i != 4; ++i)
6499 if (Locs[i].first != -1)
6500 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006501 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006502}
6503
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006504static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006505 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006506 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006507
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006508 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6509 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006510 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6511 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6512 // BUILD_VECTOR (load), undef
6513 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006514
6515 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006516}
6517
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006518static
Evan Cheng835580f2010-10-07 20:50:20 +00006519SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6520 EVT VT = Op.getValueType();
6521
6522 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006523 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6524 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006525 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6526 V1, DAG));
6527}
6528
6529static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006530SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006531 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006532 SDValue V1 = Op.getOperand(0);
6533 SDValue V2 = Op.getOperand(1);
6534 EVT VT = Op.getValueType();
6535
6536 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6537
Craig Topper1accb7e2012-01-10 06:54:16 +00006538 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006539 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6540
Evan Cheng0899f5c2011-08-31 02:05:24 +00006541 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6542 return DAG.getNode(ISD::BITCAST, dl, VT,
6543 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6544 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6545 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006546}
6547
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006548static
6549SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6550 SDValue V1 = Op.getOperand(0);
6551 SDValue V2 = Op.getOperand(1);
6552 EVT VT = Op.getValueType();
6553
6554 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6555 "unsupported shuffle type");
6556
6557 if (V2.getOpcode() == ISD::UNDEF)
6558 V2 = V1;
6559
6560 // v4i32 or v4f32
6561 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6562}
6563
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564static
Craig Topper1accb7e2012-01-10 06:54:16 +00006565SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006566 SDValue V1 = Op.getOperand(0);
6567 SDValue V2 = Op.getOperand(1);
6568 EVT VT = Op.getValueType();
6569 unsigned NumElems = VT.getVectorNumElements();
6570
6571 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6572 // operand of these instructions is only memory, so check if there's a
6573 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6574 // same masks.
6575 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006576
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006577 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006578 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006579 CanFoldLoad = true;
6580
6581 // When V1 is a load, it can be folded later into a store in isel, example:
6582 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6583 // turns into:
6584 // (MOVLPSmr addr:$src1, VR128:$src2)
6585 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006586 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006587 CanFoldLoad = true;
6588
Dan Gohman65fd6562011-11-03 21:49:52 +00006589 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006590 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006591 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6593
6594 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006595 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006596 if (SVOp->getMaskElt(1) != -1)
6597 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006598 }
6599
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006600 // movl and movlp will both match v2i64, but v2i64 is never matched by
6601 // movl earlier because we make it strict to avoid messing with the movlp load
6602 // folding logic (see the code above getMOVLP call). Match it here then,
6603 // this is horrible, but will stay like this until we move all shuffle
6604 // matching to x86 specific nodes. Note that for the 1st condition all
6605 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006606 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006607 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6608 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006609 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006610 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006611 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006612 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006613
6614 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6615
6616 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006617 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006618 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006619}
6620
Michael Liaod9d09602012-10-23 17:34:00 +00006621// Reduce a vector shuffle to zext.
6622SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006623X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006624 // PMOVZX is only available from SSE41.
6625 if (!Subtarget->hasSSE41())
6626 return SDValue();
6627
6628 EVT VT = Op.getValueType();
6629
6630 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006631 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006632 return SDValue();
6633
6634 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6635 DebugLoc DL = Op.getDebugLoc();
6636 SDValue V1 = Op.getOperand(0);
6637 SDValue V2 = Op.getOperand(1);
6638 unsigned NumElems = VT.getVectorNumElements();
6639
6640 // Extending is an unary operation and the element type of the source vector
6641 // won't be equal to or larger than i64.
6642 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6643 VT.getVectorElementType() == MVT::i64)
6644 return SDValue();
6645
6646 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6647 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006648 while ((1U << Shift) < NumElems) {
6649 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006650 break;
6651 Shift += 1;
6652 // The maximal ratio is 8, i.e. from i8 to i64.
6653 if (Shift > 3)
6654 return SDValue();
6655 }
6656
6657 // Check the shuffle mask.
6658 unsigned Mask = (1U << Shift) - 1;
6659 for (unsigned i = 0; i != NumElems; ++i) {
6660 int EltIdx = SVOp->getMaskElt(i);
6661 if ((i & Mask) != 0 && EltIdx != -1)
6662 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006663 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006664 return SDValue();
6665 }
6666
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006667 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006668 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006669 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6670 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006671
6672 if (!isTypeLegal(NVT))
6673 return SDValue();
6674
6675 // Simplify the operand as it's prepared to be fed into shuffle.
6676 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6677 if (V1.getOpcode() == ISD::BITCAST &&
6678 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6679 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6680 V1.getOperand(0)
6681 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6682 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6683 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006684 ConstantSDNode *CIdx =
6685 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006686 // If it's foldable, i.e. normal load with single use, we will let code
6687 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006688 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006689 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6690 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6691 // The "ext_vec_elt" node is wider than the result node.
6692 // In this case we should extract subvector from V.
6693 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6694 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6695 EVT FullVT = V.getValueType();
6696 EVT SubVecVT = EVT::getVectorVT(*Context,
6697 FullVT.getVectorElementType(),
6698 FullVT.getVectorNumElements()/Ratio);
6699 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6700 DAG.getIntPtrConstant(0));
6701 }
Michael Liaod9d09602012-10-23 17:34:00 +00006702 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006703 }
Michael Liaod9d09602012-10-23 17:34:00 +00006704 }
6705
6706 return DAG.getNode(ISD::BITCAST, DL, VT,
6707 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6708}
6709
Nadav Rotem154819d2012-04-09 07:45:58 +00006710SDValue
6711X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006712 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006713 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006714 DebugLoc dl = Op.getDebugLoc();
6715 SDValue V1 = Op.getOperand(0);
6716 SDValue V2 = Op.getOperand(1);
6717
6718 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006719 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006720
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006721 // Handle splat operations
6722 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006723 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006724 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006725 if (Broadcast.getNode())
6726 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006727 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006728
Michael Liaod9d09602012-10-23 17:34:00 +00006729 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006730 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006731 if (NewOp.getNode())
6732 return NewOp;
6733
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006734 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6735 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006736 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6737 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006738 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006739 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006740 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006741 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006742 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006743 // FIXME: Figure out a cleaner way to do this.
6744 // Try to make use of movq to zero out the top part.
6745 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006746 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006747 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006748 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006749 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6750 NewVT, true, false))
6751 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006752 DAG, Subtarget, dl);
6753 }
6754 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006755 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006756 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006757 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006758 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6759 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6760 DAG, Subtarget, dl);
6761 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006762 }
6763 }
6764 return SDValue();
6765}
6766
Dan Gohman475871a2008-07-27 21:46:04 +00006767SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006768X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006769 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006770 SDValue V1 = Op.getOperand(0);
6771 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006772 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006773 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006775 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006776 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006777 bool V1IsSplat = false;
6778 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006779 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006780 bool HasFp256 = Subtarget->hasFp256();
6781 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006782 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006783 bool OptForSize = MF.getFunction()->getAttributes().
6784 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006785
Craig Topper3426a3e2011-11-14 06:46:21 +00006786 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006787
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006788 if (V1IsUndef && V2IsUndef)
6789 return DAG.getUNDEF(VT);
6790
6791 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006792
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006793 // Vector shuffle lowering takes 3 steps:
6794 //
6795 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6796 // narrowing and commutation of operands should be handled.
6797 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6798 // shuffle nodes.
6799 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6800 // so the shuffle can be broken into other shuffles and the legalizer can
6801 // try the lowering again.
6802 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006803 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006804 // be matched during isel, all of them must be converted to a target specific
6805 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006806
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006807 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6808 // narrowing and commutation of operands should be handled. The actual code
6809 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006810 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006811 if (NewOp.getNode())
6812 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006813
Craig Topper5aaffa82012-02-19 02:53:47 +00006814 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6815
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006816 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6817 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006818 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006819 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006820 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006821 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006822
Craig Topperdd637ae2012-02-19 05:41:45 +00006823 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006824 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006825 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006826
Craig Topperdd637ae2012-02-19 05:41:45 +00006827 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006828 return getMOVHighToLow(Op, dl, DAG);
6829
6830 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006831 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006832 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006833 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006834
Craig Topper5aaffa82012-02-19 02:53:47 +00006835 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006836 // The actual implementation will match the mask in the if above and then
6837 // during isel it can match several different instructions, not only pshufd
6838 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006839 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6840 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006841
Craig Topper5aaffa82012-02-19 02:53:47 +00006842 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006843
Craig Topper1accb7e2012-01-10 06:54:16 +00006844 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006845 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6846
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006847 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6848 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6849 DAG);
6850
Craig Topperb3982da2011-12-31 23:50:21 +00006851 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006852 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006853 }
Eric Christopherfd179292009-08-27 18:07:15 +00006854
Evan Chengf26ffe92008-05-29 08:22:04 +00006855 // Check if this can be converted into a logical shift.
6856 bool isLeft = false;
6857 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006858 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006859 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006860 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006861 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006862 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006863 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006864 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006865 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006866 }
Eric Christopherfd179292009-08-27 18:07:15 +00006867
Craig Topper5aaffa82012-02-19 02:53:47 +00006868 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006869 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006870 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006871 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006872 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006873 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6874
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006875 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006876 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6877 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006878 }
Eric Christopherfd179292009-08-27 18:07:15 +00006879
Nate Begeman9008ca62009-04-27 18:41:29 +00006880 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006881 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006882 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006883
Craig Topperdd637ae2012-02-19 05:41:45 +00006884 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006885 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006886
Craig Topperdd637ae2012-02-19 05:41:45 +00006887 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006888 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006889
Craig Topperdd637ae2012-02-19 05:41:45 +00006890 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006891 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006892
Craig Topperdd637ae2012-02-19 05:41:45 +00006893 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006894 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895
Craig Topperdd637ae2012-02-19 05:41:45 +00006896 if (ShouldXformToMOVHLPS(M, VT) ||
6897 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006898 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006899
Evan Chengf26ffe92008-05-29 08:22:04 +00006900 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006901 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006902 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006903 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006904 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006905 }
Eric Christopherfd179292009-08-27 18:07:15 +00006906
Evan Cheng9eca5e82006-10-25 21:49:50 +00006907 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006908 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6909 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006910 V1IsSplat = isSplatVector(V1.getNode());
6911 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006912
Chris Lattner8a594482007-11-25 00:24:49 +00006913 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006914 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6915 CommuteVectorShuffleMask(M, NumElems);
6916 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006917 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006918 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006919 }
6920
Craig Topperbeabc6c2011-12-05 06:56:46 +00006921 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006922 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006923 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006924 return V1;
6925 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6926 // the instruction selector will not match, so get a canonical MOVL with
6927 // swapped operands to undo the commute.
6928 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006929 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006930
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006931 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006932 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006933
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006934 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006935 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006936
Evan Cheng9bbbb982006-10-25 20:48:19 +00006937 if (V2IsSplat) {
6938 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006939 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006940 // new vector_shuffle with the corrected mask.p
6941 SmallVector<int, 8> NewMask(M.begin(), M.end());
6942 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006943 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006944 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006945 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006946 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947 }
6948
Evan Cheng9eca5e82006-10-25 21:49:50 +00006949 if (Commuted) {
6950 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006951 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006952 CommuteVectorShuffleMask(M, NumElems);
6953 std::swap(V1, V2);
6954 std::swap(V1IsSplat, V2IsSplat);
6955 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006956
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006957 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006958 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006959
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006960 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006961 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006962 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963
Nate Begeman9008ca62009-04-27 18:41:29 +00006964 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006965 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006966 return CommuteVectorShuffle(SVOp, DAG);
6967
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006968 // The checks below are all present in isShuffleMaskLegal, but they are
6969 // inlined here right now to enable us to directly emit target specific
6970 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006971
Craig Topper0e2037b2012-01-20 05:53:00 +00006972 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006973 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006974 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006975 DAG);
6976
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006977 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6978 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006979 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006980 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006981 }
6982
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006983 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006984 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006985 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006986 DAG);
6987
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006988 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006989 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006990 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006991 DAG);
6992
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006993 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006994 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006995 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006996
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006997 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006998 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006999 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007000 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007001
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007002 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007003 // Generate target specific nodes for 128 or 256-bit shuffles only
7004 // supported in the AVX instruction set.
7005 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007006
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007007 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007008 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007009 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7010
Craig Topper70b883b2011-11-28 10:14:51 +00007011 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007012 if (isVPERMILPMask(M, VT, HasFp256)) {
7013 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007014 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007015 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007016 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007017 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007018 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007019
Craig Topper70b883b2011-11-28 10:14:51 +00007020 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007021 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007022 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007023 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007024
Craig Topper1842ba02012-04-23 06:38:28 +00007025 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007026 if (BlendOp.getNode())
7027 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007028
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007029 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007030 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007031 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007032 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007033 }
Craig Topper92040742012-04-16 06:43:40 +00007034 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7035 &permclMask[0], 8);
7036 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007037 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007038 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007039 }
Craig Topper095c5282012-04-15 23:48:57 +00007040
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007041 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007042 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007043 getShuffleCLImmediate(SVOp), DAG);
7044
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007045 //===--------------------------------------------------------------------===//
7046 // Since no target specific shuffle was selected for this generic one,
7047 // lower it into other known shuffles. FIXME: this isn't true yet, but
7048 // this is the plan.
7049 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007050
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007051 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7052 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007053 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007054 if (NewOp.getNode())
7055 return NewOp;
7056 }
7057
7058 if (VT == MVT::v16i8) {
7059 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7060 if (NewOp.getNode())
7061 return NewOp;
7062 }
7063
Elena Demikhovsky41789462012-09-06 12:42:01 +00007064 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007065 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007066 if (NewOp.getNode())
7067 return NewOp;
7068 }
7069
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007070 // Handle all 128-bit wide vectors with 4 elements, and match them with
7071 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007072 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007073 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7074
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007075 // Handle general 256-bit shuffles
7076 if (VT.is256BitVector())
7077 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7078
Dan Gohman475871a2008-07-27 21:46:04 +00007079 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007080}
7081
Craig Topperf84b7502013-01-20 00:50:58 +00007082static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007083 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007084 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007085
Craig Topper45e1c752013-01-20 00:38:18 +00007086 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007087 return SDValue();
7088
Duncan Sands83ec4b62008-06-06 12:08:01 +00007089 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007091 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007093 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007094 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007095 }
7096
7097 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007098 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7099 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7100 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007101 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7102 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007103 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007104 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007105 Op.getOperand(0)),
7106 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007108 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007110 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007111 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007112 }
7113
7114 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007115 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7116 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007117 // result has a single use which is a store or a bitcast to i32. And in
7118 // the case of a store, it's not worth it if the index is a constant 0,
7119 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007120 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007121 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007122 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007123 if ((User->getOpcode() != ISD::STORE ||
7124 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7125 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007126 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007127 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007128 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007129 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007130 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007131 Op.getOperand(0)),
7132 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007133 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007134 }
7135
7136 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007137 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007138 if (isa<ConstantSDNode>(Op.getOperand(1)))
7139 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007140 }
Dan Gohman475871a2008-07-27 21:46:04 +00007141 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007142}
7143
Dan Gohman475871a2008-07-27 21:46:04 +00007144SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007145X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7146 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007147 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149
David Greene74a579d2011-02-10 16:57:36 +00007150 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007151 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007152
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007153 // If this is a 256-bit vector result, first extract the 128-bit vector and
7154 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007155 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007156 DebugLoc dl = Op.getNode()->getDebugLoc();
7157 unsigned NumElems = VecVT.getVectorNumElements();
7158 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007159 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7160
7161 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007162 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007163
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007164 if (IdxVal >= NumElems/2)
7165 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007166 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007167 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007168 }
7169
Craig Topper7a9a28b2012-08-12 02:23:29 +00007170 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007171
Craig Topperd0a31172012-01-10 06:37:29 +00007172 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007173 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007174 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007175 return Res;
7176 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007177
Craig Topper45e1c752013-01-20 00:38:18 +00007178 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007179 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007180 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007181 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007182 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007183 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007184 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7186 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007187 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007188 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007189 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007191 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007192 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007193 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007194 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007195 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007196 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007197 }
7198
7199 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007200 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007201 if (Idx == 0)
7202 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007203
Evan Cheng0db9fe62006-04-25 20:13:52 +00007204 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007205 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007206 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007207 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007208 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007209 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007210 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007211 }
7212
7213 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007214 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7215 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7216 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007217 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007218 if (Idx == 0)
7219 return Op;
7220
7221 // UNPCKHPD the element to the lowest double word, then movsd.
7222 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7223 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007224 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007225 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007226 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007227 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007228 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007229 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007230 }
7231
Dan Gohman475871a2008-07-27 21:46:04 +00007232 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007233}
7234
Craig Topperf84b7502013-01-20 00:50:58 +00007235static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007236 MVT VT = Op.getValueType().getSimpleVT();
7237 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007238 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007239
Dan Gohman475871a2008-07-27 21:46:04 +00007240 SDValue N0 = Op.getOperand(0);
7241 SDValue N1 = Op.getOperand(1);
7242 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243
Craig Topper7a9a28b2012-08-12 02:23:29 +00007244 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007245 return SDValue();
7246
Dan Gohman8a55ce42009-09-23 21:02:20 +00007247 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007248 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007249 unsigned Opc;
7250 if (VT == MVT::v8i16)
7251 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007252 else if (VT == MVT::v16i8)
7253 Opc = X86ISD::PINSRB;
7254 else
7255 Opc = X86ISD::PINSRB;
7256
Nate Begeman14d12ca2008-02-11 04:19:36 +00007257 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7258 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007259 if (N1.getValueType() != MVT::i32)
7260 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7261 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007262 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007263 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007264 }
7265
7266 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007267 // Bits [7:6] of the constant are the source select. This will always be
7268 // zero here. The DAG Combiner may combine an extract_elt index into these
7269 // bits. For example (insert (extract, 3), 2) could be matched by putting
7270 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007271 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007272 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007273 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007274 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007275 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007276 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007277 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007278 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007279 }
7280
7281 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007282 // PINSR* works with constant index.
7283 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007284 }
Dan Gohman475871a2008-07-27 21:46:04 +00007285 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007286}
7287
Dan Gohman475871a2008-07-27 21:46:04 +00007288SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007289X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007290 MVT VT = Op.getValueType().getSimpleVT();
7291 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007292
David Greene6b381262011-02-09 15:32:06 +00007293 DebugLoc dl = Op.getDebugLoc();
7294 SDValue N0 = Op.getOperand(0);
7295 SDValue N1 = Op.getOperand(1);
7296 SDValue N2 = Op.getOperand(2);
7297
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007298 // If this is a 256-bit vector result, first extract the 128-bit vector,
7299 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007300 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007301 if (!isa<ConstantSDNode>(N2))
7302 return SDValue();
7303
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007304 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007305 unsigned NumElems = VT.getVectorNumElements();
7306 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007307 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007308
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007309 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007310 bool Upper = IdxVal >= NumElems/2;
7311 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7312 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007313
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007314 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007315 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007316 }
7317
Craig Topperd0a31172012-01-10 06:37:29 +00007318 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007319 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7320
Dan Gohman8a55ce42009-09-23 21:02:20 +00007321 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007322 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007323
Dan Gohman8a55ce42009-09-23 21:02:20 +00007324 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007325 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7326 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 if (N1.getValueType() != MVT::i32)
7328 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7329 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007330 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007331 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007332 }
Dan Gohman475871a2008-07-27 21:46:04 +00007333 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007334}
7335
Craig Topper55b24052012-09-11 06:15:32 +00007336static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007337 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007338 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007339 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007340
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007341 // If this is a 256-bit vector result, first insert into a 128-bit
7342 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007343 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007344 // Insert into a 128-bit vector.
7345 EVT VT128 = EVT::getVectorVT(*Context,
7346 OpVT.getVectorElementType(),
7347 OpVT.getVectorNumElements() / 2);
7348
7349 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7350
7351 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007352 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007353 }
7354
Craig Topperd77d2fe2012-04-29 20:22:05 +00007355 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007356 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007357 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007358
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007360 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007361 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007362 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007363}
7364
David Greene91585092011-01-26 15:38:49 +00007365// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7366// a simple subregister reference or explicit instructions to grab
7367// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007368static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7369 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007370 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007371 DebugLoc dl = Op.getNode()->getDebugLoc();
7372 SDValue Vec = Op.getNode()->getOperand(0);
7373 SDValue Idx = Op.getNode()->getOperand(1);
7374
Craig Topper7a9a28b2012-08-12 02:23:29 +00007375 if (Op.getNode()->getValueType(0).is128BitVector() &&
7376 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007377 isa<ConstantSDNode>(Idx)) {
7378 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7379 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007380 }
David Greene91585092011-01-26 15:38:49 +00007381 }
7382 return SDValue();
7383}
7384
David Greenecfe33c42011-01-26 19:13:22 +00007385// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7386// simple superregister reference or explicit instructions to insert
7387// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007388static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7389 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007390 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007391 DebugLoc dl = Op.getNode()->getDebugLoc();
7392 SDValue Vec = Op.getNode()->getOperand(0);
7393 SDValue SubVec = Op.getNode()->getOperand(1);
7394 SDValue Idx = Op.getNode()->getOperand(2);
7395
Craig Topper7a9a28b2012-08-12 02:23:29 +00007396 if (Op.getNode()->getValueType(0).is256BitVector() &&
7397 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007398 isa<ConstantSDNode>(Idx)) {
7399 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7400 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007401 }
7402 }
7403 return SDValue();
7404}
7405
Bill Wendling056292f2008-09-16 21:48:12 +00007406// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7407// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7408// one of the above mentioned nodes. It has to be wrapped because otherwise
7409// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7410// be used to form addressing mode. These wrapped nodes will be selected
7411// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007412SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007413X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007414 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007415
Chris Lattner41621a22009-06-26 19:22:52 +00007416 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7417 // global base reg.
7418 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007419 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007420 CodeModel::Model M = getTargetMachine().getCodeModel();
7421
Chris Lattner4f066492009-07-11 20:29:19 +00007422 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007423 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007424 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007425 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007426 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007427 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007428 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007429
Evan Cheng1606e8e2009-03-13 07:51:59 +00007430 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007431 CP->getAlignment(),
7432 CP->getOffset(), OpFlag);
7433 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007434 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007435 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007436 if (OpFlag) {
7437 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007438 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007439 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007440 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007441 }
7442
7443 return Result;
7444}
7445
Dan Gohmand858e902010-04-17 15:26:15 +00007446SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007447 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007448
Chris Lattner18c59872009-06-27 04:16:01 +00007449 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7450 // global base reg.
7451 unsigned char OpFlag = 0;
7452 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007453 CodeModel::Model M = getTargetMachine().getCodeModel();
7454
Chris Lattner4f066492009-07-11 20:29:19 +00007455 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007456 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007457 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007458 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007459 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007460 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007461 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007462
Chris Lattner18c59872009-06-27 04:16:01 +00007463 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7464 OpFlag);
7465 DebugLoc DL = JT->getDebugLoc();
7466 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007467
Chris Lattner18c59872009-06-27 04:16:01 +00007468 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007469 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007470 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7471 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007472 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007473 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007474
Chris Lattner18c59872009-06-27 04:16:01 +00007475 return Result;
7476}
7477
7478SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007479X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007480 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007481
Chris Lattner18c59872009-06-27 04:16:01 +00007482 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7483 // global base reg.
7484 unsigned char OpFlag = 0;
7485 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007486 CodeModel::Model M = getTargetMachine().getCodeModel();
7487
Chris Lattner4f066492009-07-11 20:29:19 +00007488 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007489 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7490 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7491 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007492 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007493 } else if (Subtarget->isPICStyleGOT()) {
7494 OpFlag = X86II::MO_GOT;
7495 } else if (Subtarget->isPICStyleStubPIC()) {
7496 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7497 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7498 OpFlag = X86II::MO_DARWIN_NONLAZY;
7499 }
Eric Christopherfd179292009-08-27 18:07:15 +00007500
Chris Lattner18c59872009-06-27 04:16:01 +00007501 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007502
Chris Lattner18c59872009-06-27 04:16:01 +00007503 DebugLoc DL = Op.getDebugLoc();
7504 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007505
Chris Lattner18c59872009-06-27 04:16:01 +00007506 // With PIC, the address is actually $g + Offset.
7507 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007508 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007509 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7510 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007511 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007512 Result);
7513 }
Eric Christopherfd179292009-08-27 18:07:15 +00007514
Eli Friedman586272d2011-08-11 01:48:05 +00007515 // For symbols that require a load from a stub to get the address, emit the
7516 // load.
7517 if (isGlobalStubReference(OpFlag))
7518 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007519 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007520
Chris Lattner18c59872009-06-27 04:16:01 +00007521 return Result;
7522}
7523
Dan Gohman475871a2008-07-27 21:46:04 +00007524SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007525X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007526 // Create the TargetBlockAddressAddress node.
7527 unsigned char OpFlags =
7528 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007529 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007530 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007531 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007532 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007533 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7534 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007535
Dan Gohmanf705adb2009-10-30 01:28:02 +00007536 if (Subtarget->isPICStyleRIPRel() &&
7537 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007538 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7539 else
7540 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007541
Dan Gohman29cbade2009-11-20 23:18:13 +00007542 // With PIC, the address is actually $g + Offset.
7543 if (isGlobalRelativeToPICBase(OpFlags)) {
7544 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7545 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7546 Result);
7547 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007548
7549 return Result;
7550}
7551
7552SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007553X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007554 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007555 // Create the TargetGlobalAddress node, folding in the constant
7556 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007557 unsigned char OpFlags =
7558 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007559 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007560 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007561 if (OpFlags == X86II::MO_NO_FLAG &&
7562 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007563 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007564 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007565 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007566 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007567 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007568 }
Eric Christopherfd179292009-08-27 18:07:15 +00007569
Chris Lattner4f066492009-07-11 20:29:19 +00007570 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007571 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007572 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7573 else
7574 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007575
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007576 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007577 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007578 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7579 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007580 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007582
Chris Lattner36c25012009-07-10 07:34:39 +00007583 // For globals that require a load from a stub to get the address, emit the
7584 // load.
7585 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007586 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007587 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007588
Dan Gohman6520e202008-10-18 02:06:02 +00007589 // If there was a non-zero offset that we didn't fold, create an explicit
7590 // addition for it.
7591 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007592 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007593 DAG.getConstant(Offset, getPointerTy()));
7594
Evan Cheng0db9fe62006-04-25 20:13:52 +00007595 return Result;
7596}
7597
Evan Chengda43bcf2008-09-24 00:05:32 +00007598SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007599X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007600 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007601 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007602 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007603}
7604
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007605static SDValue
7606GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007607 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007608 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007609 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007610 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007611 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007612 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007613 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007614 GA->getOffset(),
7615 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007616
7617 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7618 : X86ISD::TLSADDR;
7619
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007620 if (InFlag) {
7621 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007622 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007623 } else {
7624 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007625 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007626 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007627
7628 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007629 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007630
Rafael Espindola15f1b662009-04-24 12:59:40 +00007631 SDValue Flag = Chain.getValue(1);
7632 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007633}
7634
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007635// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007636static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007637LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007638 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007639 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007640 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7641 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007642 DAG.getNode(X86ISD::GlobalBaseReg,
7643 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007644 InFlag = Chain.getValue(1);
7645
Chris Lattnerb903bed2009-06-26 21:20:29 +00007646 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007647}
7648
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007649// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007650static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007651LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007652 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007653 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7654 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007655}
7656
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007657static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7658 SelectionDAG &DAG,
7659 const EVT PtrVT,
7660 bool is64Bit) {
7661 DebugLoc dl = GA->getDebugLoc();
7662
7663 // Get the start address of the TLS block for this module.
7664 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7665 .getInfo<X86MachineFunctionInfo>();
7666 MFI->incNumLocalDynamicTLSAccesses();
7667
7668 SDValue Base;
7669 if (is64Bit) {
7670 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7671 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7672 } else {
7673 SDValue InFlag;
7674 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7675 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7676 InFlag = Chain.getValue(1);
7677 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7678 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7679 }
7680
7681 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7682 // of Base.
7683
7684 // Build x@dtpoff.
7685 unsigned char OperandFlags = X86II::MO_DTPOFF;
7686 unsigned WrapperKind = X86ISD::Wrapper;
7687 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7688 GA->getValueType(0),
7689 GA->getOffset(), OperandFlags);
7690 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7691
7692 // Add x@dtpoff with the base.
7693 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7694}
7695
Hans Wennborg228756c2012-05-11 10:11:01 +00007696// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007697static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007698 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007699 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007700 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007701
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007702 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7703 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7704 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007705
Michael J. Spencerec38de22010-10-10 22:04:20 +00007706 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007707 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007708 MachinePointerInfo(Ptr),
7709 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007710
Chris Lattnerb903bed2009-06-26 21:20:29 +00007711 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007712 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7713 // initialexec.
7714 unsigned WrapperKind = X86ISD::Wrapper;
7715 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007716 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007717 } else if (model == TLSModel::InitialExec) {
7718 if (is64Bit) {
7719 OperandFlags = X86II::MO_GOTTPOFF;
7720 WrapperKind = X86ISD::WrapperRIP;
7721 } else {
7722 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7723 }
Chris Lattner18c59872009-06-27 04:16:01 +00007724 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007725 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007726 }
Eric Christopherfd179292009-08-27 18:07:15 +00007727
Hans Wennborg228756c2012-05-11 10:11:01 +00007728 // emit "addl x@ntpoff,%eax" (local exec)
7729 // or "addl x@indntpoff,%eax" (initial exec)
7730 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007731 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007732 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007733 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007734 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007735
Hans Wennborg228756c2012-05-11 10:11:01 +00007736 if (model == TLSModel::InitialExec) {
7737 if (isPIC && !is64Bit) {
7738 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7739 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7740 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007741 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007742
7743 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7744 MachinePointerInfo::getGOT(), false, false, false,
7745 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007746 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007747
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007748 // The address of the thread local variable is the add of the thread
7749 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007750 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007751}
7752
Dan Gohman475871a2008-07-27 21:46:04 +00007753SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007754X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007755
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007756 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007757 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007758
Eric Christopher30ef0e52010-06-03 04:07:48 +00007759 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007760 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007761
Eric Christopher30ef0e52010-06-03 04:07:48 +00007762 switch (model) {
7763 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 if (Subtarget->is64Bit())
7765 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7766 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007767 case TLSModel::LocalDynamic:
7768 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7769 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007770 case TLSModel::InitialExec:
7771 case TLSModel::LocalExec:
7772 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007773 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007774 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007776 llvm_unreachable("Unknown TLS model.");
7777 }
7778
7779 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007780 // Darwin only has one model of TLS. Lower to that.
7781 unsigned char OpFlag = 0;
7782 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7783 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007784
Eric Christopher30ef0e52010-06-03 04:07:48 +00007785 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7786 // global base reg.
7787 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7788 !Subtarget->is64Bit();
7789 if (PIC32)
7790 OpFlag = X86II::MO_TLVP_PIC_BASE;
7791 else
7792 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007793 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007794 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007795 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007796 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007797 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007798
Eric Christopher30ef0e52010-06-03 04:07:48 +00007799 // With PIC32, the address is actually $g + Offset.
7800 if (PIC32)
7801 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7802 DAG.getNode(X86ISD::GlobalBaseReg,
7803 DebugLoc(), getPointerTy()),
7804 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007805
Eric Christopher30ef0e52010-06-03 04:07:48 +00007806 // Lowering the machine isd will make sure everything is in the right
7807 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007808 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007809 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007810 SDValue Args[] = { Chain, Offset };
7811 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007812
Eric Christopher30ef0e52010-06-03 04:07:48 +00007813 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7814 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7815 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007816
Eric Christopher30ef0e52010-06-03 04:07:48 +00007817 // And our return value (tls address) is in the standard call return value
7818 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007819 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007820 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7821 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007822 }
7823
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007824 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007825 // Just use the implicit TLS architecture
7826 // Need to generate someting similar to:
7827 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7828 // ; from TEB
7829 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7830 // mov rcx, qword [rdx+rcx*8]
7831 // mov eax, .tls$:tlsvar
7832 // [rax+rcx] contains the address
7833 // Windows 64bit: gs:0x58
7834 // Windows 32bit: fs:__tls_array
7835
7836 // If GV is an alias then use the aliasee for determining
7837 // thread-localness.
7838 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7839 GV = GA->resolveAliasedGlobal(false);
7840 DebugLoc dl = GA->getDebugLoc();
7841 SDValue Chain = DAG.getEntryNode();
7842
7843 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007844 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7845 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007846 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7847 ? Type::getInt8PtrTy(*DAG.getContext(),
7848 256)
7849 : Type::getInt32PtrTy(*DAG.getContext(),
7850 257));
7851
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007852 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7853 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7854 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7855
7856 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007857 MachinePointerInfo(Ptr),
7858 false, false, false, 0);
7859
7860 // Load the _tls_index variable
7861 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7862 if (Subtarget->is64Bit())
7863 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7864 IDX, MachinePointerInfo(), MVT::i32,
7865 false, false, 0);
7866 else
7867 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7868 false, false, false, 0);
7869
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007870 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007871 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007872 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7873
7874 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7875 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7876 false, false, false, 0);
7877
7878 // Get the offset of start of .tls section
7879 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7880 GA->getValueType(0),
7881 GA->getOffset(), X86II::MO_SECREL);
7882 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7883
7884 // The address of the thread local variable is the add of the thread
7885 // pointer with the offset of the variable.
7886 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007887 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007888
David Blaikie4d6ccb52012-01-20 21:51:11 +00007889 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007890}
7891
Chad Rosierb90d2a92012-01-03 23:19:12 +00007892/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7893/// and take a 2 x i32 value to shift plus a shift amount.
7894SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007895 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007896 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007897 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007898 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007899 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007900 SDValue ShOpLo = Op.getOperand(0);
7901 SDValue ShOpHi = Op.getOperand(1);
7902 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007903 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007904 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007905 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007906
Dan Gohman475871a2008-07-27 21:46:04 +00007907 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007908 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007909 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7910 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007911 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007912 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7913 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007914 }
Evan Chenge3413162006-01-09 18:33:28 +00007915
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7917 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007918 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007919 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007920
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007923 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7924 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007925
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007926 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007927 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7928 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007929 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007930 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7931 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007932 }
7933
Dan Gohman475871a2008-07-27 21:46:04 +00007934 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007935 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007936}
Evan Chenga3195e82006-01-12 22:54:21 +00007937
Dan Gohmand858e902010-04-17 15:26:15 +00007938SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7939 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007940 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007941
Dale Johannesen0488fb62010-09-30 23:57:10 +00007942 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007943 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007944
Owen Anderson825b72b2009-08-11 20:47:22 +00007945 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007946 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007947
Eli Friedman36df4992009-05-27 00:47:34 +00007948 // These are really Legal; return the operand so the caller accepts it as
7949 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007950 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007951 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007952 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007953 Subtarget->is64Bit()) {
7954 return Op;
7955 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007956
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007957 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007958 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007960 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007961 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007962 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007963 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007964 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007965 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007966 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7967}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007968
Owen Andersone50ed302009-08-10 22:56:29 +00007969SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007970 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007971 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007972 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007973 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007974 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007975 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007976 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007977 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007978 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007979 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007980
Chris Lattner492a43e2010-09-22 01:28:21 +00007981 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007982
Stuart Hastings84be9582011-06-02 15:57:11 +00007983 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7984 MachineMemOperand *MMO;
7985 if (FI) {
7986 int SSFI = FI->getIndex();
7987 MMO =
7988 DAG.getMachineFunction()
7989 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7990 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7991 } else {
7992 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7993 StackSlot = StackSlot.getOperand(1);
7994 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007995 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007996 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7997 X86ISD::FILD, DL,
7998 Tys, Ops, array_lengthof(Ops),
7999 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008000
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008001 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008002 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008003 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008004
8005 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8006 // shouldn't be necessary except that RFP cannot be live across
8007 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008008 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008009 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8010 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008011 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008012 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008013 SDValue Ops[] = {
8014 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8015 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008016 MachineMemOperand *MMO =
8017 DAG.getMachineFunction()
8018 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008019 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008020
Chris Lattner492a43e2010-09-22 01:28:21 +00008021 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8022 Ops, array_lengthof(Ops),
8023 Op.getValueType(), MMO);
8024 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008025 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008026 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008027 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008028
Evan Cheng0db9fe62006-04-25 20:13:52 +00008029 return Result;
8030}
8031
Bill Wendling8b8a6362009-01-17 03:56:04 +00008032// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008033SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8034 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008035 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008036 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008037 movq %rax, %xmm0
8038 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8039 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8040 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008041 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008042 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008043 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008044 addpd %xmm1, %xmm0
8045 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008046 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008047
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008048 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008049 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008050
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008051 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008052 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8053 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008054 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008055
Chris Lattner97484792012-01-25 09:56:22 +00008056 SmallVector<Constant*,2> CV1;
8057 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008058 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8059 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008060 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008061 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8062 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008063 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008064 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008065
Bill Wendling397ae212012-01-05 02:13:20 +00008066 // Load the 64-bit value into an XMM register.
8067 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8068 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008069 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008070 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008071 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008072 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8073 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8074 CLod0);
8075
Owen Anderson825b72b2009-08-11 20:47:22 +00008076 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008077 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008078 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008079 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008081 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008082
Craig Topperd0a31172012-01-10 06:37:29 +00008083 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008084 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8085 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8086 } else {
8087 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8088 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8089 S2F, 0x4E, DAG);
8090 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8091 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8092 Sub);
8093 }
8094
8095 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008096 DAG.getIntPtrConstant(0));
8097}
8098
Bill Wendling8b8a6362009-01-17 03:56:04 +00008099// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008100SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8101 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008102 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008103 // FP constant to bias correct the final result.
8104 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008105 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008106
8107 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008109 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008110
Eli Friedmanf3704762011-08-29 21:15:46 +00008111 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008112 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008113
Owen Anderson825b72b2009-08-11 20:47:22 +00008114 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008115 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008116 DAG.getIntPtrConstant(0));
8117
8118 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008119 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008120 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008121 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008123 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008124 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008125 MVT::v2f64, Bias)));
8126 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008127 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008128 DAG.getIntPtrConstant(0));
8129
8130 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008131 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008132
8133 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008134 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008135
Craig Topper69947b92012-04-23 06:57:04 +00008136 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008137 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008138 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008139 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008140 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008141
8142 // Handle final rounding.
8143 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008144}
8145
Michael Liaoa7554632012-10-23 17:36:08 +00008146SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8147 SelectionDAG &DAG) const {
8148 SDValue N0 = Op.getOperand(0);
8149 EVT SVT = N0.getValueType();
8150 DebugLoc dl = Op.getDebugLoc();
8151
8152 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8153 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8154 "Custom UINT_TO_FP is not supported!");
8155
Craig Topperb99bafe2013-01-21 06:21:54 +00008156 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8157 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008158 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8159 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8160}
8161
Dan Gohmand858e902010-04-17 15:26:15 +00008162SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8163 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008164 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008165 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008166
Michael Liaoa7554632012-10-23 17:36:08 +00008167 if (Op.getValueType().isVector())
8168 return lowerUINT_TO_FP_vec(Op, DAG);
8169
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008170 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008171 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8172 // the optimization here.
8173 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008174 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008175
Owen Andersone50ed302009-08-10 22:56:29 +00008176 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008177 EVT DstVT = Op.getValueType();
8178 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008179 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008180 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008181 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008182 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008183 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008184
8185 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008186 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008187 if (SrcVT == MVT::i32) {
8188 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8189 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8190 getPointerTy(), StackSlot, WordOff);
8191 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008192 StackSlot, MachinePointerInfo(),
8193 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008194 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008195 OffsetSlot, MachinePointerInfo(),
8196 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008197 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8198 return Fild;
8199 }
8200
8201 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8202 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008203 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008204 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008205 // For i64 source, we need to add the appropriate power of 2 if the input
8206 // was negative. This is the same as the optimization in
8207 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8208 // we must be careful to do the computation in x87 extended precision, not
8209 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008210 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8211 MachineMemOperand *MMO =
8212 DAG.getMachineFunction()
8213 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8214 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008215
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008216 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8217 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008218 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8219 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008220
8221 APInt FF(32, 0x5F800000ULL);
8222
8223 // Check whether the sign bit is set.
8224 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8225 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8226 ISD::SETLT);
8227
8228 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8229 SDValue FudgePtr = DAG.getConstantPool(
8230 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8231 getPointerTy());
8232
8233 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8234 SDValue Zero = DAG.getIntPtrConstant(0);
8235 SDValue Four = DAG.getIntPtrConstant(4);
8236 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8237 Zero, Four);
8238 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8239
8240 // Load the value out, extending it from f32 to f80.
8241 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008242 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008243 FudgePtr, MachinePointerInfo::getConstantPool(),
8244 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008245 // Extend everything to 80 bits to force it to be done on x87.
8246 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8247 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008248}
8249
Craig Topperb99bafe2013-01-21 06:21:54 +00008250std::pair<SDValue,SDValue>
8251X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8252 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008253 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008254
Owen Andersone50ed302009-08-10 22:56:29 +00008255 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008256
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008257 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008258 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8259 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008260 }
8261
Owen Anderson825b72b2009-08-11 20:47:22 +00008262 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8263 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008264 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008266 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008268 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008269 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008270 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008272 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008273 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008274
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008275 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8276 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008277 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008278 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008279 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008280 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008281
Evan Cheng0db9fe62006-04-25 20:13:52 +00008282 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008283 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8284 Opc = X86ISD::WIN_FTOL;
8285 else
8286 switch (DstTy.getSimpleVT().SimpleTy) {
8287 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8288 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8289 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8290 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8291 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008292
Dan Gohman475871a2008-07-27 21:46:04 +00008293 SDValue Chain = DAG.getEntryNode();
8294 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008295 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008296 // FIXME This causes a redundant load/store if the SSE-class value is already
8297 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008298 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008299 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008300 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008301 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008302 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008304 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008305 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008306 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008307
Chris Lattner492a43e2010-09-22 01:28:21 +00008308 MachineMemOperand *MMO =
8309 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8310 MachineMemOperand::MOLoad, MemSize, MemSize);
8311 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8312 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008313 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008314 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008315 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8316 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008317
Chris Lattner07290932010-09-22 01:05:16 +00008318 MachineMemOperand *MMO =
8319 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8320 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008321
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008322 if (Opc != X86ISD::WIN_FTOL) {
8323 // Build the FP_TO_INT*_IN_MEM
8324 SDValue Ops[] = { Chain, Value, StackSlot };
8325 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8326 Ops, 3, DstTy, MMO);
8327 return std::make_pair(FIST, StackSlot);
8328 } else {
8329 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8330 DAG.getVTList(MVT::Other, MVT::Glue),
8331 Chain, Value);
8332 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8333 MVT::i32, ftol.getValue(1));
8334 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8335 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008336 SDValue Ops[] = { eax, edx };
8337 SDValue pair = IsReplace
8338 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8339 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008340 return std::make_pair(pair, SDValue());
8341 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008342}
8343
Nadav Rotem0509db22012-12-28 05:45:24 +00008344static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8345 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008346 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008347 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008348 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008349 DebugLoc dl = Op->getDebugLoc();
8350
8351 // Optimize vectors in AVX mode:
8352 //
8353 // v8i16 -> v8i32
8354 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8355 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8356 // Concat upper and lower parts.
8357 //
8358 // v4i32 -> v4i64
8359 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8360 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8361 // Concat upper and lower parts.
8362 //
8363
8364 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8365 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8366 return SDValue();
8367
8368 if (Subtarget->hasInt256())
8369 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8370
8371 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8372 SDValue Undef = DAG.getUNDEF(InVT);
8373 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8374 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8375 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8376
Craig Toppera080daf2013-01-20 21:50:27 +00008377 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008378 VT.getVectorNumElements()/2);
8379
8380 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8381 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8382
8383 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8384}
8385
8386SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8387 SelectionDAG &DAG) const {
8388 if (Subtarget->hasFp256()) {
8389 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8390 if (Res.getNode())
8391 return Res;
8392 }
8393
8394 return SDValue();
8395}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008396SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8397 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008398 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008399 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008400 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008401 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008402
Nadav Rotem0509db22012-12-28 05:45:24 +00008403 if (Subtarget->hasFp256()) {
8404 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8405 if (Res.getNode())
8406 return Res;
8407 }
8408
Michael Liaoa7554632012-10-23 17:36:08 +00008409 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8410 VT.getVectorNumElements() != SVT.getVectorNumElements())
8411 return SDValue();
8412
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008413 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008414
8415 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008416 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008417 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8418
8419 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8420 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8421 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008422 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8423 DAG.getUNDEF(MVT::v8i16),
8424 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008425
8426 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8427}
8428
Craig Topperd713c0f2013-01-20 21:34:37 +00008429SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008430 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008431 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008432 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008433 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008434
Nadav Rotem3c22a442012-12-27 07:45:10 +00008435 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8436 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8437 if (Subtarget->hasInt256()) {
8438 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8439 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8440 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8441 ShufMask);
8442 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8443 DAG.getIntPtrConstant(0));
8444 }
8445
8446 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8447 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8448 DAG.getIntPtrConstant(0));
8449 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8450 DAG.getIntPtrConstant(2));
8451
8452 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8453 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8454
8455 // The PSHUFD mask:
8456 static const int ShufMask1[] = {0, 2, 0, 0};
8457 SDValue Undef = DAG.getUNDEF(VT);
8458 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8459 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8460
8461 // The MOVLHPS mask:
8462 static const int ShufMask2[] = {0, 1, 4, 5};
8463 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8464 }
8465
8466 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8467 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8468 if (Subtarget->hasInt256()) {
8469 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8470
8471 SmallVector<SDValue,32> pshufbMask;
8472 for (unsigned i = 0; i < 2; ++i) {
8473 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8474 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8475 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8476 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8477 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8478 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8479 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8480 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8481 for (unsigned j = 0; j < 8; ++j)
8482 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8483 }
8484 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8485 &pshufbMask[0], 32);
8486 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8487 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8488
8489 static const int ShufMask[] = {0, 2, -1, -1};
8490 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8491 &ShufMask[0]);
8492 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8493 DAG.getIntPtrConstant(0));
8494 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8495 }
8496
8497 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8498 DAG.getIntPtrConstant(0));
8499
8500 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8501 DAG.getIntPtrConstant(4));
8502
8503 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8504 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8505
8506 // The PSHUFB mask:
8507 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8508 -1, -1, -1, -1, -1, -1, -1, -1};
8509
8510 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8511 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8512 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8513
8514 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8515 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8516
8517 // The MOVLHPS Mask:
8518 static const int ShufMask2[] = {0, 1, 4, 5};
8519 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8520 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8521 }
8522
8523 // Handle truncation of V256 to V128 using shuffles.
8524 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008525 return SDValue();
8526
Nadav Rotem3c22a442012-12-27 07:45:10 +00008527 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8528 "Invalid op");
8529 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008530
8531 unsigned NumElems = VT.getVectorNumElements();
8532 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8533 NumElems * 2);
8534
Michael Liaobedcbd42012-10-16 18:14:11 +00008535 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8536 // Prepare truncation shuffle mask
8537 for (unsigned i = 0; i != NumElems; ++i)
8538 MaskVec[i] = i * 2;
8539 SDValue V = DAG.getVectorShuffle(NVT, DL,
8540 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8541 DAG.getUNDEF(NVT), &MaskVec[0]);
8542 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8543 DAG.getIntPtrConstant(0));
8544}
8545
Dan Gohmand858e902010-04-17 15:26:15 +00008546SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8547 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008548 MVT VT = Op.getValueType().getSimpleVT();
8549 if (VT.isVector()) {
8550 if (VT == MVT::v8i16)
8551 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008552 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8553 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008554 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008555 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008556
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008557 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8558 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008560 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8561 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008562
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008563 if (StackSlot.getNode())
8564 // Load the result.
8565 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8566 FIST, StackSlot, MachinePointerInfo(),
8567 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008568
8569 // The node is the result.
8570 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008571}
8572
Dan Gohmand858e902010-04-17 15:26:15 +00008573SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8574 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008575 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8576 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008577 SDValue FIST = Vals.first, StackSlot = Vals.second;
8578 assert(FIST.getNode() && "Unexpected failure");
8579
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008580 if (StackSlot.getNode())
8581 // Load the result.
8582 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8583 FIST, StackSlot, MachinePointerInfo(),
8584 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008585
8586 // The node is the result.
8587 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008588}
8589
Craig Topperb84b4232013-01-21 06:13:28 +00008590static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008591 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008592 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008593 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008594 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008595
8596 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8597
8598 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8599 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8600 In, DAG.getUNDEF(SVT)));
8601}
8602
Craig Topper43620672012-09-08 07:31:51 +00008603SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008604 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008605 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008606 MVT VT = Op.getValueType().getSimpleVT();
8607 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008608 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8609 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008610 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008611 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008612 }
Craig Topper43620672012-09-08 07:31:51 +00008613 Constant *C;
8614 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008615 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8616 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008617 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008618 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8619 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008620 C = ConstantVector::getSplat(NumElts, C);
8621 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8622 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008623 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008624 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008625 false, false, false, Alignment);
8626 if (VT.isVector()) {
8627 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8628 return DAG.getNode(ISD::BITCAST, dl, VT,
8629 DAG.getNode(ISD::AND, dl, ANDVT,
8630 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8631 Op.getOperand(0)),
8632 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8633 }
Dale Johannesenace16102009-02-03 19:33:06 +00008634 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008635}
8636
Dan Gohmand858e902010-04-17 15:26:15 +00008637SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008638 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008639 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008640 MVT VT = Op.getValueType().getSimpleVT();
8641 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008642 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8643 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008644 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008645 NumElts = VT.getVectorNumElements();
8646 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008647 Constant *C;
8648 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008649 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8650 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008651 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008652 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8653 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008654 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008655 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8656 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008657 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008658 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008659 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008660 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008661 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008662 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008663 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008664 DAG.getNode(ISD::BITCAST, dl, XORVT,
8665 Op.getOperand(0)),
8666 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008667 }
Craig Topper69947b92012-04-23 06:57:04 +00008668
8669 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008670}
8671
Dan Gohmand858e902010-04-17 15:26:15 +00008672SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008673 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008674 SDValue Op0 = Op.getOperand(0);
8675 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008676 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008677 MVT VT = Op.getValueType().getSimpleVT();
8678 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008679
8680 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008681 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008682 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008683 SrcVT = VT;
8684 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008685 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008686 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008687 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008688 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008689 }
8690
8691 // At this point the operands and the result should have the same
8692 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008693
Evan Cheng68c47cb2007-01-05 07:55:56 +00008694 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008695 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008696 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008697 const fltSemantics &Sem = APFloat::IEEEdouble;
8698 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8699 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008700 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008701 const fltSemantics &Sem = APFloat::IEEEsingle;
8702 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8703 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8704 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8705 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008706 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008707 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008708 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008709 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008710 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008711 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008712 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008713
8714 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008715 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 // Op0 is MVT::f32, Op1 is MVT::f64.
8717 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8718 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8719 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008720 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008721 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008722 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008723 }
8724
Evan Cheng73d6cf12007-01-05 21:37:56 +00008725 // Clear first operand sign bit.
8726 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008727 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008728 const fltSemantics &Sem = APFloat::IEEEdouble;
8729 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8730 APInt(64, ~(1ULL << 63)))));
8731 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008732 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008733 const fltSemantics &Sem = APFloat::IEEEsingle;
8734 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8735 APInt(32, ~(1U << 31)))));
8736 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8737 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8738 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008739 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008740 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008741 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008742 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008743 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008744 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008745 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008746
8747 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008748 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008749}
8750
Craig Topper55b24052012-09-11 06:15:32 +00008751static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008752 SDValue N0 = Op.getOperand(0);
8753 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008754 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008755
8756 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8757 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8758 DAG.getConstant(1, VT));
8759 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8760}
8761
Michael Liaof966e4e2012-09-13 20:24:54 +00008762// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8763//
Craig Topperb99bafe2013-01-21 06:21:54 +00008764SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8765 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008766 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8767
8768 if (!Subtarget->hasSSE41())
8769 return SDValue();
8770
8771 if (!Op->hasOneUse())
8772 return SDValue();
8773
8774 SDNode *N = Op.getNode();
8775 DebugLoc DL = N->getDebugLoc();
8776
8777 SmallVector<SDValue, 8> Opnds;
8778 DenseMap<SDValue, unsigned> VecInMap;
8779 EVT VT = MVT::Other;
8780
8781 // Recognize a special case where a vector is casted into wide integer to
8782 // test all 0s.
8783 Opnds.push_back(N->getOperand(0));
8784 Opnds.push_back(N->getOperand(1));
8785
8786 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8787 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8788 // BFS traverse all OR'd operands.
8789 if (I->getOpcode() == ISD::OR) {
8790 Opnds.push_back(I->getOperand(0));
8791 Opnds.push_back(I->getOperand(1));
8792 // Re-evaluate the number of nodes to be traversed.
8793 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8794 continue;
8795 }
8796
8797 // Quit if a non-EXTRACT_VECTOR_ELT
8798 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8799 return SDValue();
8800
8801 // Quit if without a constant index.
8802 SDValue Idx = I->getOperand(1);
8803 if (!isa<ConstantSDNode>(Idx))
8804 return SDValue();
8805
8806 SDValue ExtractedFromVec = I->getOperand(0);
8807 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8808 if (M == VecInMap.end()) {
8809 VT = ExtractedFromVec.getValueType();
8810 // Quit if not 128/256-bit vector.
8811 if (!VT.is128BitVector() && !VT.is256BitVector())
8812 return SDValue();
8813 // Quit if not the same type.
8814 if (VecInMap.begin() != VecInMap.end() &&
8815 VT != VecInMap.begin()->first.getValueType())
8816 return SDValue();
8817 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8818 }
8819 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8820 }
8821
8822 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008823 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008824
8825 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8826 SmallVector<SDValue, 8> VecIns;
8827
8828 for (DenseMap<SDValue, unsigned>::const_iterator
8829 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8830 // Quit if not all elements are used.
8831 if (I->second != FullMask)
8832 return SDValue();
8833 VecIns.push_back(I->first);
8834 }
8835
8836 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8837
8838 // Cast all vectors into TestVT for PTEST.
8839 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8840 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8841
8842 // If more than one full vectors are evaluated, OR them first before PTEST.
8843 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8844 // Each iteration will OR 2 nodes and append the result until there is only
8845 // 1 node left, i.e. the final OR'd value of all vectors.
8846 SDValue LHS = VecIns[Slot];
8847 SDValue RHS = VecIns[Slot + 1];
8848 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8849 }
8850
8851 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8852 VecIns.back(), VecIns.back());
8853}
8854
Dan Gohman076aee32009-03-04 19:44:21 +00008855/// Emit nodes that will be selected as "test Op0,Op0", or something
8856/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008857SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008858 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008859 DebugLoc dl = Op.getDebugLoc();
8860
Dan Gohman31125812009-03-07 01:58:32 +00008861 // CF and OF aren't always set the way we want. Determine which
8862 // of these we need.
8863 bool NeedCF = false;
8864 bool NeedOF = false;
8865 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008866 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008867 case X86::COND_A: case X86::COND_AE:
8868 case X86::COND_B: case X86::COND_BE:
8869 NeedCF = true;
8870 break;
8871 case X86::COND_G: case X86::COND_GE:
8872 case X86::COND_L: case X86::COND_LE:
8873 case X86::COND_O: case X86::COND_NO:
8874 NeedOF = true;
8875 break;
Dan Gohman31125812009-03-07 01:58:32 +00008876 }
8877
Dan Gohman076aee32009-03-04 19:44:21 +00008878 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008879 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8880 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008881 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8882 // Emit a CMP with 0, which is the TEST pattern.
8883 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8884 DAG.getConstant(0, Op.getValueType()));
8885
8886 unsigned Opcode = 0;
8887 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008888
8889 // Truncate operations may prevent the merge of the SETCC instruction
8890 // and the arithmetic intruction before it. Attempt to truncate the operands
8891 // of the arithmetic instruction and use a reduced bit-width instruction.
8892 bool NeedTruncation = false;
8893 SDValue ArithOp = Op;
8894 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8895 SDValue Arith = Op->getOperand(0);
8896 // Both the trunc and the arithmetic op need to have one user each.
8897 if (Arith->hasOneUse())
8898 switch (Arith.getOpcode()) {
8899 default: break;
8900 case ISD::ADD:
8901 case ISD::SUB:
8902 case ISD::AND:
8903 case ISD::OR:
8904 case ISD::XOR: {
8905 NeedTruncation = true;
8906 ArithOp = Arith;
8907 }
8908 }
8909 }
8910
8911 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8912 // which may be the result of a CAST. We use the variable 'Op', which is the
8913 // non-casted variable when we check for possible users.
8914 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008915 case ISD::ADD:
8916 // Due to an isel shortcoming, be conservative if this add is likely to be
8917 // selected as part of a load-modify-store instruction. When the root node
8918 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8919 // uses of other nodes in the match, such as the ADD in this case. This
8920 // leads to the ADD being left around and reselected, with the result being
8921 // two adds in the output. Alas, even if none our users are stores, that
8922 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8923 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8924 // climbing the DAG back to the root, and it doesn't seem to be worth the
8925 // effort.
8926 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008927 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8928 if (UI->getOpcode() != ISD::CopyToReg &&
8929 UI->getOpcode() != ISD::SETCC &&
8930 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008931 goto default_case;
8932
8933 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008934 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008935 // An add of one will be selected as an INC.
8936 if (C->getAPIntValue() == 1) {
8937 Opcode = X86ISD::INC;
8938 NumOperands = 1;
8939 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008940 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008941
8942 // An add of negative one (subtract of one) will be selected as a DEC.
8943 if (C->getAPIntValue().isAllOnesValue()) {
8944 Opcode = X86ISD::DEC;
8945 NumOperands = 1;
8946 break;
8947 }
Dan Gohman076aee32009-03-04 19:44:21 +00008948 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008949
8950 // Otherwise use a regular EFLAGS-setting add.
8951 Opcode = X86ISD::ADD;
8952 NumOperands = 2;
8953 break;
8954 case ISD::AND: {
8955 // If the primary and result isn't used, don't bother using X86ISD::AND,
8956 // because a TEST instruction will be better.
8957 bool NonFlagUse = false;
8958 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8959 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8960 SDNode *User = *UI;
8961 unsigned UOpNo = UI.getOperandNo();
8962 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8963 // Look pass truncate.
8964 UOpNo = User->use_begin().getOperandNo();
8965 User = *User->use_begin();
8966 }
8967
8968 if (User->getOpcode() != ISD::BRCOND &&
8969 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008970 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008971 NonFlagUse = true;
8972 break;
8973 }
Dan Gohman076aee32009-03-04 19:44:21 +00008974 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008975
8976 if (!NonFlagUse)
8977 break;
8978 }
8979 // FALL THROUGH
8980 case ISD::SUB:
8981 case ISD::OR:
8982 case ISD::XOR:
8983 // Due to the ISEL shortcoming noted above, be conservative if this op is
8984 // likely to be selected as part of a load-modify-store instruction.
8985 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8986 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8987 if (UI->getOpcode() == ISD::STORE)
8988 goto default_case;
8989
8990 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008991 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008992 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008993 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008994 case ISD::XOR: Opcode = X86ISD::XOR; break;
8995 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008996 case ISD::OR: {
8997 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8998 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8999 if (EFLAGS.getNode())
9000 return EFLAGS;
9001 }
9002 Opcode = X86ISD::OR;
9003 break;
9004 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009005 }
9006
9007 NumOperands = 2;
9008 break;
9009 case X86ISD::ADD:
9010 case X86ISD::SUB:
9011 case X86ISD::INC:
9012 case X86ISD::DEC:
9013 case X86ISD::OR:
9014 case X86ISD::XOR:
9015 case X86ISD::AND:
9016 return SDValue(Op.getNode(), 1);
9017 default:
9018 default_case:
9019 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009020 }
9021
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009022 // If we found that truncation is beneficial, perform the truncation and
9023 // update 'Op'.
9024 if (NeedTruncation) {
9025 EVT VT = Op.getValueType();
9026 SDValue WideVal = Op->getOperand(0);
9027 EVT WideVT = WideVal.getValueType();
9028 unsigned ConvertedOp = 0;
9029 // Use a target machine opcode to prevent further DAGCombine
9030 // optimizations that may separate the arithmetic operations
9031 // from the setcc node.
9032 switch (WideVal.getOpcode()) {
9033 default: break;
9034 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9035 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9036 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9037 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9038 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9039 }
9040
9041 if (ConvertedOp) {
9042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9043 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9044 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9045 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9046 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9047 }
9048 }
9049 }
9050
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009051 if (Opcode == 0)
9052 // Emit a CMP with 0, which is the TEST pattern.
9053 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9054 DAG.getConstant(0, Op.getValueType()));
9055
9056 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9057 SmallVector<SDValue, 4> Ops;
9058 for (unsigned i = 0; i != NumOperands; ++i)
9059 Ops.push_back(Op.getOperand(i));
9060
9061 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9062 DAG.ReplaceAllUsesWith(Op, New);
9063 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009064}
9065
9066/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9067/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009068SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009069 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009070 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9071 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009072 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009073
9074 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009075 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9076 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9077 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9078 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9079 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9080 Op0, Op1);
9081 return SDValue(Sub.getNode(), 1);
9082 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009083 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009084}
9085
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009086/// Convert a comparison if required by the subtarget.
9087SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9088 SelectionDAG &DAG) const {
9089 // If the subtarget does not support the FUCOMI instruction, floating-point
9090 // comparisons have to be converted.
9091 if (Subtarget->hasCMov() ||
9092 Cmp.getOpcode() != X86ISD::CMP ||
9093 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9094 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9095 return Cmp;
9096
9097 // The instruction selector will select an FUCOM instruction instead of
9098 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9099 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9100 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9101 DebugLoc dl = Cmp.getDebugLoc();
9102 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9103 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9104 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9105 DAG.getConstant(8, MVT::i8));
9106 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9107 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9108}
9109
Evan Cheng4e544802012-12-05 00:10:38 +00009110static bool isAllOnes(SDValue V) {
9111 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9112 return C && C->isAllOnesValue();
9113}
9114
Evan Chengd40d03e2010-01-06 19:38:29 +00009115/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9116/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009117SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9118 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009119 SDValue Op0 = And.getOperand(0);
9120 SDValue Op1 = And.getOperand(1);
9121 if (Op0.getOpcode() == ISD::TRUNCATE)
9122 Op0 = Op0.getOperand(0);
9123 if (Op1.getOpcode() == ISD::TRUNCATE)
9124 Op1 = Op1.getOperand(0);
9125
Evan Chengd40d03e2010-01-06 19:38:29 +00009126 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009127 if (Op1.getOpcode() == ISD::SHL)
9128 std::swap(Op0, Op1);
9129 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009130 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9131 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009132 // If we looked past a truncate, check that it's only truncating away
9133 // known zeros.
9134 unsigned BitWidth = Op0.getValueSizeInBits();
9135 unsigned AndBitWidth = And.getValueSizeInBits();
9136 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009137 APInt Zeros, Ones;
9138 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009139 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9140 return SDValue();
9141 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009142 LHS = Op1;
9143 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009144 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009145 } else if (Op1.getOpcode() == ISD::Constant) {
9146 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009147 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009148 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009149
9150 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009151 LHS = AndLHS.getOperand(0);
9152 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009153 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009154
9155 // Use BT if the immediate can't be encoded in a TEST instruction.
9156 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9157 LHS = AndLHS;
9158 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9159 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009160 }
Evan Cheng0488db92007-09-25 01:57:46 +00009161
Evan Chengd40d03e2010-01-06 19:38:29 +00009162 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009163 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9164 // the condition code later.
9165 bool Invert = false;
9166 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9167 Invert = true;
9168 LHS = LHS.getOperand(0);
9169 }
9170
Evan Chenge5b51ac2010-04-17 06:13:15 +00009171 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009172 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009173 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009174 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009175 // Also promote i16 to i32 for performance / code size reason.
9176 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009177 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009178 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009179
Evan Chengd40d03e2010-01-06 19:38:29 +00009180 // If the operand types disagree, extend the shift amount to match. Since
9181 // BT ignores high bits (like shifts) we can use anyextend.
9182 if (LHS.getValueType() != RHS.getValueType())
9183 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009184
Evan Chengd40d03e2010-01-06 19:38:29 +00009185 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009186 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9187 // Flip the condition if the LHS was a not instruction
9188 if (Invert)
9189 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009190 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9191 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009192 }
9193
Evan Cheng54de3ea2010-01-05 06:52:31 +00009194 return SDValue();
9195}
9196
Craig Topper89af15e2011-09-18 08:03:58 +00009197// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009198// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009199static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009200 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009201
Craig Topper7a9a28b2012-08-12 02:23:29 +00009202 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009203 "Unsupported value type for operation");
9204
Craig Topper66ddd152012-04-27 22:54:43 +00009205 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009206 DebugLoc dl = Op.getDebugLoc();
9207 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009208
9209 // Extract the LHS vectors
9210 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009211 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9212 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009213
9214 // Extract the RHS vectors
9215 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009216 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9217 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009218
9219 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009220 MVT EltVT = VT.getVectorElementType();
9221 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009222 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9223 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9224 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9225}
9226
Craig Topper26827f32013-01-20 09:02:22 +00009227static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9228 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009229 SDValue Cond;
9230 SDValue Op0 = Op.getOperand(0);
9231 SDValue Op1 = Op.getOperand(1);
9232 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009233 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009234 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009235 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009236 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009237
9238 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009239#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009240 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009241 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9242#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009243
Craig Topper523908d2012-08-13 02:34:03 +00009244 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009245 bool Swap = false;
9246
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009247 // SSE Condition code mapping:
9248 // 0 - EQ
9249 // 1 - LT
9250 // 2 - LE
9251 // 3 - UNORD
9252 // 4 - NEQ
9253 // 5 - NLT
9254 // 6 - NLE
9255 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009256 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009257 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009258 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009259 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009260 case ISD::SETOGT:
9261 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009262 case ISD::SETLT:
9263 case ISD::SETOLT: SSECC = 1; break;
9264 case ISD::SETOGE:
9265 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009266 case ISD::SETLE:
9267 case ISD::SETOLE: SSECC = 2; break;
9268 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009269 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009270 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009271 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009272 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009273 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009274 case ISD::SETUGT: SSECC = 6; break;
9275 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009276 case ISD::SETUEQ:
9277 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009278 }
9279 if (Swap)
9280 std::swap(Op0, Op1);
9281
Nate Begemanfb8ead02008-07-25 19:05:58 +00009282 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009283 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009284 unsigned CC0, CC1;
9285 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009286 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009287 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9288 } else {
9289 assert(SetCCOpcode == ISD::SETONE);
9290 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009291 }
Craig Topper523908d2012-08-13 02:34:03 +00009292
9293 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9294 DAG.getConstant(CC0, MVT::i8));
9295 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9296 DAG.getConstant(CC1, MVT::i8));
9297 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009298 }
9299 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009300 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9301 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009303
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009304 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009305 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009306 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009307
Nate Begeman30a0de92008-07-17 16:51:19 +00009308 // We are handling one of the integer comparisons here. Since SSE only has
9309 // GT and EQ comparisons for integer, swapping operands and multiple
9310 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009311 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009312 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009313
Nate Begeman30a0de92008-07-17 16:51:19 +00009314 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009315 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009316 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009317 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009318 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009319 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009320 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009321 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009322 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009323 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009324 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009325 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009326 }
9327 if (Swap)
9328 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009329
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009330 // Check that the operation in question is available (most are plain SSE2,
9331 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009332 if (VT == MVT::v2i64) {
9333 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9334 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009335 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9336 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009337 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009338 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9339
9340 // First cast everything to the right type,
9341 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9342 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9343
9344 // Do the compare.
9345 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9346
9347 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009348 const int Mask[] = { 1, 0, 3, 2 };
9349 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9350 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009351
9352 if (Invert)
9353 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9354
9355 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9356 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009357 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009358
Nate Begeman30a0de92008-07-17 16:51:19 +00009359 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9360 // bits of the inputs before performing those operations.
9361 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009362 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009363 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9364 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009365 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009366 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9367 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009368 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9369 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009371
Dale Johannesenace16102009-02-03 19:33:06 +00009372 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009373
9374 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009375 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009376 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009377
Nate Begeman30a0de92008-07-17 16:51:19 +00009378 return Result;
9379}
Evan Cheng0488db92007-09-25 01:57:46 +00009380
Craig Topper26827f32013-01-20 09:02:22 +00009381SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9382
9383 MVT VT = Op.getValueType().getSimpleVT();
9384
9385 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9386
9387 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9388 SDValue Op0 = Op.getOperand(0);
9389 SDValue Op1 = Op.getOperand(1);
9390 DebugLoc dl = Op.getDebugLoc();
9391 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9392
9393 // Optimize to BT if possible.
9394 // Lower (X & (1 << N)) == 0 to BT(X, N).
9395 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9396 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9397 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9398 Op1.getOpcode() == ISD::Constant &&
9399 cast<ConstantSDNode>(Op1)->isNullValue() &&
9400 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9401 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9402 if (NewSetCC.getNode())
9403 return NewSetCC;
9404 }
9405
9406 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9407 // these.
9408 if (Op1.getOpcode() == ISD::Constant &&
9409 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9410 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9411 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9412
9413 // If the input is a setcc, then reuse the input setcc or use a new one with
9414 // the inverted condition.
9415 if (Op0.getOpcode() == X86ISD::SETCC) {
9416 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9417 bool Invert = (CC == ISD::SETNE) ^
9418 cast<ConstantSDNode>(Op1)->isNullValue();
9419 if (!Invert) return Op0;
9420
9421 CCode = X86::GetOppositeBranchCondition(CCode);
9422 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9423 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9424 }
9425 }
9426
9427 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9428 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9429 if (X86CC == X86::COND_INVALID)
9430 return SDValue();
9431
9432 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9433 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9434 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9435 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9436}
9437
Evan Cheng370e5342008-12-03 08:38:43 +00009438// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009439static bool isX86LogicalCmp(SDValue Op) {
9440 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009441 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9442 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009443 return true;
9444 if (Op.getResNo() == 1 &&
9445 (Opc == X86ISD::ADD ||
9446 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009447 Opc == X86ISD::ADC ||
9448 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009449 Opc == X86ISD::SMUL ||
9450 Opc == X86ISD::UMUL ||
9451 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009452 Opc == X86ISD::DEC ||
9453 Opc == X86ISD::OR ||
9454 Opc == X86ISD::XOR ||
9455 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009456 return true;
9457
Chris Lattner9637d5b2010-12-05 07:49:54 +00009458 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9459 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009460
Dan Gohman076aee32009-03-04 19:44:21 +00009461 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009462}
9463
Chris Lattnera2b56002010-12-05 01:23:24 +00009464static bool isZero(SDValue V) {
9465 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9466 return C && C->isNullValue();
9467}
9468
Evan Chengb64dd5f2012-08-07 22:21:00 +00009469static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9470 if (V.getOpcode() != ISD::TRUNCATE)
9471 return false;
9472
9473 SDValue VOp0 = V.getOperand(0);
9474 unsigned InBits = VOp0.getValueSizeInBits();
9475 unsigned Bits = V.getValueSizeInBits();
9476 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9477}
9478
Dan Gohmand858e902010-04-17 15:26:15 +00009479SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009480 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009481 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009482 SDValue Op1 = Op.getOperand(1);
9483 SDValue Op2 = Op.getOperand(2);
9484 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009485 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009486
Dan Gohman1a492952009-10-20 16:22:37 +00009487 if (Cond.getOpcode() == ISD::SETCC) {
9488 SDValue NewCond = LowerSETCC(Cond, DAG);
9489 if (NewCond.getNode())
9490 Cond = NewCond;
9491 }
Evan Cheng734503b2006-09-11 02:19:56 +00009492
Chris Lattnera2b56002010-12-05 01:23:24 +00009493 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009494 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009495 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009496 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009497 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009498 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9499 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009500 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009501
Chris Lattnera2b56002010-12-05 01:23:24 +00009502 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009503
9504 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009505 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9506 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009507
9508 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009509 // Apply further optimizations for special cases
9510 // (select (x != 0), -1, 0) -> neg & sbb
9511 // (select (x == 0), 0, -1) -> neg & sbb
9512 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009513 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009514 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9515 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009516 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9517 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009518 CmpOp0);
9519 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9520 DAG.getConstant(X86::COND_B, MVT::i8),
9521 SDValue(Neg.getNode(), 1));
9522 return Res;
9523 }
9524
Chris Lattnera2b56002010-12-05 01:23:24 +00009525 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9526 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009527 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009528
Chris Lattner96908b12010-12-05 02:00:51 +00009529 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009530 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9531 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009532
Chris Lattner96908b12010-12-05 02:00:51 +00009533 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9534 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009535
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009536 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009537 if (N2C == 0 || !N2C->isNullValue())
9538 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9539 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009540 }
9541 }
9542
Chris Lattnera2b56002010-12-05 01:23:24 +00009543 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009544 if (Cond.getOpcode() == ISD::AND &&
9545 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9546 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009547 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009548 Cond = Cond.getOperand(0);
9549 }
9550
Evan Cheng3f41d662007-10-08 22:16:29 +00009551 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9552 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009553 unsigned CondOpcode = Cond.getOpcode();
9554 if (CondOpcode == X86ISD::SETCC ||
9555 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009556 CC = Cond.getOperand(0);
9557
Dan Gohman475871a2008-07-27 21:46:04 +00009558 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009559 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009560 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009561
Evan Cheng3f41d662007-10-08 22:16:29 +00009562 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009563 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009564 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009565 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009566
Chris Lattnerd1980a52009-03-12 06:52:53 +00009567 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9568 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009569 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009570 addTest = false;
9571 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009572 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9573 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9574 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9575 Cond.getOperand(0).getValueType() != MVT::i8)) {
9576 SDValue LHS = Cond.getOperand(0);
9577 SDValue RHS = Cond.getOperand(1);
9578 unsigned X86Opcode;
9579 unsigned X86Cond;
9580 SDVTList VTs;
9581 switch (CondOpcode) {
9582 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9583 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9584 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9585 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9586 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9587 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9588 default: llvm_unreachable("unexpected overflowing operator");
9589 }
9590 if (CondOpcode == ISD::UMULO)
9591 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9592 MVT::i32);
9593 else
9594 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9595
9596 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9597
9598 if (CondOpcode == ISD::UMULO)
9599 Cond = X86Op.getValue(2);
9600 else
9601 Cond = X86Op.getValue(1);
9602
9603 CC = DAG.getConstant(X86Cond, MVT::i8);
9604 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009605 }
9606
9607 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009608 // Look pass the truncate if the high bits are known zero.
9609 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9610 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009611
9612 // We know the result of AND is compared against zero. Try to match
9613 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009614 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009615 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009616 if (NewSetCC.getNode()) {
9617 CC = NewSetCC.getOperand(0);
9618 Cond = NewSetCC.getOperand(1);
9619 addTest = false;
9620 }
9621 }
9622 }
9623
9624 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009625 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009626 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009627 }
9628
Benjamin Kramere915ff32010-12-22 23:09:28 +00009629 // a < b ? -1 : 0 -> RES = ~setcc_carry
9630 // a < b ? 0 : -1 -> RES = setcc_carry
9631 // a >= b ? -1 : 0 -> RES = setcc_carry
9632 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009633 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009634 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009635 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9636
9637 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9638 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9639 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9640 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9641 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9642 return DAG.getNOT(DL, Res, Res.getValueType());
9643 return Res;
9644 }
9645 }
9646
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009647 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9648 // widen the cmov and push the truncate through. This avoids introducing a new
9649 // branch during isel and doesn't add any extensions.
9650 if (Op.getValueType() == MVT::i8 &&
9651 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9652 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9653 if (T1.getValueType() == T2.getValueType() &&
9654 // Blacklist CopyFromReg to avoid partial register stalls.
9655 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9656 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009657 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009658 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9659 }
9660 }
9661
Evan Cheng0488db92007-09-25 01:57:46 +00009662 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9663 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009664 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009665 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009666 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009667}
9668
Nadav Rotem1a330af2012-12-27 22:47:16 +00009669SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9670 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009671 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009672 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009673 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009674 DebugLoc dl = Op->getDebugLoc();
9675
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009676 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9677 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9678 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009679
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009680 if (Subtarget->hasInt256())
9681 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009682
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009683 // Optimize vectors in AVX mode
9684 // Sign extend v8i16 to v8i32 and
9685 // v4i32 to v4i64
9686 //
9687 // Divide input vector into two parts
9688 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9689 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9690 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009691
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009692 unsigned NumElems = InVT.getVectorNumElements();
9693 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009694
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009695 SmallVector<int,8> ShufMask1(NumElems, -1);
9696 for (unsigned i = 0; i != NumElems/2; ++i)
9697 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009698
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009699 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009700
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009701 SmallVector<int,8> ShufMask2(NumElems, -1);
9702 for (unsigned i = 0; i != NumElems/2; ++i)
9703 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009704
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009705 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009706
Craig Toppera080daf2013-01-20 21:50:27 +00009707 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009708 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009709
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009710 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9711 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009712
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009713 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009714}
9715
Evan Cheng370e5342008-12-03 08:38:43 +00009716// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9717// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9718// from the AND / OR.
9719static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9720 Opc = Op.getOpcode();
9721 if (Opc != ISD::OR && Opc != ISD::AND)
9722 return false;
9723 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9724 Op.getOperand(0).hasOneUse() &&
9725 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9726 Op.getOperand(1).hasOneUse());
9727}
9728
Evan Cheng961d6d42009-02-02 08:19:07 +00009729// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9730// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009731static bool isXor1OfSetCC(SDValue Op) {
9732 if (Op.getOpcode() != ISD::XOR)
9733 return false;
9734 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9735 if (N1C && N1C->getAPIntValue() == 1) {
9736 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9737 Op.getOperand(0).hasOneUse();
9738 }
9739 return false;
9740}
9741
Dan Gohmand858e902010-04-17 15:26:15 +00009742SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009743 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009744 SDValue Chain = Op.getOperand(0);
9745 SDValue Cond = Op.getOperand(1);
9746 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009747 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009748 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009749 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009750
Dan Gohman1a492952009-10-20 16:22:37 +00009751 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009752 // Check for setcc([su]{add,sub,mul}o == 0).
9753 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9754 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9755 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9756 Cond.getOperand(0).getResNo() == 1 &&
9757 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9758 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9759 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9760 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9761 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9762 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9763 Inverted = true;
9764 Cond = Cond.getOperand(0);
9765 } else {
9766 SDValue NewCond = LowerSETCC(Cond, DAG);
9767 if (NewCond.getNode())
9768 Cond = NewCond;
9769 }
Dan Gohman1a492952009-10-20 16:22:37 +00009770 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009771#if 0
9772 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009773 else if (Cond.getOpcode() == X86ISD::ADD ||
9774 Cond.getOpcode() == X86ISD::SUB ||
9775 Cond.getOpcode() == X86ISD::SMUL ||
9776 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009777 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009778#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009779
Evan Chengad9c0a32009-12-15 00:53:42 +00009780 // Look pass (and (setcc_carry (cmp ...)), 1).
9781 if (Cond.getOpcode() == ISD::AND &&
9782 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9783 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009784 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009785 Cond = Cond.getOperand(0);
9786 }
9787
Evan Cheng3f41d662007-10-08 22:16:29 +00009788 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9789 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009790 unsigned CondOpcode = Cond.getOpcode();
9791 if (CondOpcode == X86ISD::SETCC ||
9792 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009793 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009794
Dan Gohman475871a2008-07-27 21:46:04 +00009795 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009796 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009797 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009798 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009799 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009800 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009801 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009802 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009803 default: break;
9804 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009805 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009806 // These can only come from an arithmetic instruction with overflow,
9807 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009808 Cond = Cond.getNode()->getOperand(1);
9809 addTest = false;
9810 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009811 }
Evan Cheng0488db92007-09-25 01:57:46 +00009812 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009813 }
9814 CondOpcode = Cond.getOpcode();
9815 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9816 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9817 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9818 Cond.getOperand(0).getValueType() != MVT::i8)) {
9819 SDValue LHS = Cond.getOperand(0);
9820 SDValue RHS = Cond.getOperand(1);
9821 unsigned X86Opcode;
9822 unsigned X86Cond;
9823 SDVTList VTs;
9824 switch (CondOpcode) {
9825 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9826 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9827 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9828 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9829 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9830 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9831 default: llvm_unreachable("unexpected overflowing operator");
9832 }
9833 if (Inverted)
9834 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9835 if (CondOpcode == ISD::UMULO)
9836 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9837 MVT::i32);
9838 else
9839 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9840
9841 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9842
9843 if (CondOpcode == ISD::UMULO)
9844 Cond = X86Op.getValue(2);
9845 else
9846 Cond = X86Op.getValue(1);
9847
9848 CC = DAG.getConstant(X86Cond, MVT::i8);
9849 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009850 } else {
9851 unsigned CondOpc;
9852 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9853 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009854 if (CondOpc == ISD::OR) {
9855 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9856 // two branches instead of an explicit OR instruction with a
9857 // separate test.
9858 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009859 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009860 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009861 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009862 Chain, Dest, CC, Cmp);
9863 CC = Cond.getOperand(1).getOperand(0);
9864 Cond = Cmp;
9865 addTest = false;
9866 }
9867 } else { // ISD::AND
9868 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9869 // two branches instead of an explicit AND instruction with a
9870 // separate test. However, we only do this if this block doesn't
9871 // have a fall-through edge, because this requires an explicit
9872 // jmp when the condition is false.
9873 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009874 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009875 Op.getNode()->hasOneUse()) {
9876 X86::CondCode CCode =
9877 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9878 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009879 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009880 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009881 // Look for an unconditional branch following this conditional branch.
9882 // We need this because we need to reverse the successors in order
9883 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009884 if (User->getOpcode() == ISD::BR) {
9885 SDValue FalseBB = User->getOperand(1);
9886 SDNode *NewBR =
9887 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009888 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009889 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009890 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009891
Dale Johannesene4d209d2009-02-03 20:21:25 +00009892 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009893 Chain, Dest, CC, Cmp);
9894 X86::CondCode CCode =
9895 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9896 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009897 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009898 Cond = Cmp;
9899 addTest = false;
9900 }
9901 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009902 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009903 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9904 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9905 // It should be transformed during dag combiner except when the condition
9906 // is set by a arithmetics with overflow node.
9907 X86::CondCode CCode =
9908 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9909 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009910 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009911 Cond = Cond.getOperand(0).getOperand(1);
9912 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009913 } else if (Cond.getOpcode() == ISD::SETCC &&
9914 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9915 // For FCMP_OEQ, we can emit
9916 // two branches instead of an explicit AND instruction with a
9917 // separate test. However, we only do this if this block doesn't
9918 // have a fall-through edge, because this requires an explicit
9919 // jmp when the condition is false.
9920 if (Op.getNode()->hasOneUse()) {
9921 SDNode *User = *Op.getNode()->use_begin();
9922 // Look for an unconditional branch following this conditional branch.
9923 // We need this because we need to reverse the successors in order
9924 // to implement FCMP_OEQ.
9925 if (User->getOpcode() == ISD::BR) {
9926 SDValue FalseBB = User->getOperand(1);
9927 SDNode *NewBR =
9928 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9929 assert(NewBR == User);
9930 (void)NewBR;
9931 Dest = FalseBB;
9932
9933 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9934 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009935 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009936 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9937 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9938 Chain, Dest, CC, Cmp);
9939 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9940 Cond = Cmp;
9941 addTest = false;
9942 }
9943 }
9944 } else if (Cond.getOpcode() == ISD::SETCC &&
9945 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9946 // For FCMP_UNE, we can emit
9947 // two branches instead of an explicit AND instruction with a
9948 // separate test. However, we only do this if this block doesn't
9949 // have a fall-through edge, because this requires an explicit
9950 // jmp when the condition is false.
9951 if (Op.getNode()->hasOneUse()) {
9952 SDNode *User = *Op.getNode()->use_begin();
9953 // Look for an unconditional branch following this conditional branch.
9954 // We need this because we need to reverse the successors in order
9955 // to implement FCMP_UNE.
9956 if (User->getOpcode() == ISD::BR) {
9957 SDValue FalseBB = User->getOperand(1);
9958 SDNode *NewBR =
9959 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9960 assert(NewBR == User);
9961 (void)NewBR;
9962
9963 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9964 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009965 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009966 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9967 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9968 Chain, Dest, CC, Cmp);
9969 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9970 Cond = Cmp;
9971 addTest = false;
9972 Dest = FalseBB;
9973 }
9974 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009975 }
Evan Cheng0488db92007-09-25 01:57:46 +00009976 }
9977
9978 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009979 // Look pass the truncate if the high bits are known zero.
9980 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9981 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009982
9983 // We know the result of AND is compared against zero. Try to match
9984 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009985 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009986 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9987 if (NewSetCC.getNode()) {
9988 CC = NewSetCC.getOperand(0);
9989 Cond = NewSetCC.getOperand(1);
9990 addTest = false;
9991 }
9992 }
9993 }
9994
9995 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009996 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009997 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009998 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009999 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010000 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010001 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010002}
10003
Anton Korobeynikove060b532007-04-17 19:34:00 +000010004// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10005// Calls to _alloca is needed to probe the stack when allocating more than 4k
10006// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10007// that the guard pages used by the OS virtual memory manager are allocated in
10008// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010009SDValue
10010X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010011 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010012 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010013 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010014 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010015 "are being used");
10016 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010017 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010018
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010019 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010020 SDValue Chain = Op.getOperand(0);
10021 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010022 // FIXME: Ensure alignment here
10023
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010024 bool Is64Bit = Subtarget->is64Bit();
10025 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010026
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010027 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010028 MachineFunction &MF = DAG.getMachineFunction();
10029 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010030
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010031 if (Is64Bit) {
10032 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010033 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010034 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010035
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010036 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010037 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010038 if (I->hasNestAttr())
10039 report_fatal_error("Cannot use segmented stacks with functions that "
10040 "have nested arguments.");
10041 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010042
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010043 const TargetRegisterClass *AddrRegClass =
10044 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10045 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10046 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10047 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10048 DAG.getRegister(Vreg, SPTy));
10049 SDValue Ops1[2] = { Value, Chain };
10050 return DAG.getMergeValues(Ops1, 2, dl);
10051 } else {
10052 SDValue Flag;
10053 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010054
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010055 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10056 Flag = Chain.getValue(1);
10057 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010058
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010059 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10060 Flag = Chain.getValue(1);
10061
Michael Liaoc5c970e2012-10-31 04:14:09 +000010062 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10063 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010064
10065 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10066 return DAG.getMergeValues(Ops1, 2, dl);
10067 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010068}
10069
Dan Gohmand858e902010-04-17 15:26:15 +000010070SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010071 MachineFunction &MF = DAG.getMachineFunction();
10072 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10073
Dan Gohman69de1932008-02-06 22:27:42 +000010074 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010075 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010076
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010077 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010078 // vastart just stores the address of the VarArgsFrameIndex slot into the
10079 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010080 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10081 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010082 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10083 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010084 }
10085
10086 // __va_list_tag:
10087 // gp_offset (0 - 6 * 8)
10088 // fp_offset (48 - 48 + 8 * 16)
10089 // overflow_arg_area (point to parameters coming in memory).
10090 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010091 SmallVector<SDValue, 8> MemOps;
10092 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010093 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010094 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010095 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10096 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010097 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010098 MemOps.push_back(Store);
10099
10100 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010101 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010102 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010103 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010104 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10105 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010106 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010107 MemOps.push_back(Store);
10108
10109 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010110 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010111 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010112 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10113 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010114 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10115 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010116 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010117 MemOps.push_back(Store);
10118
10119 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010120 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010121 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010122 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10123 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010124 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10125 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010126 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010127 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010128 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010129}
10130
Dan Gohmand858e902010-04-17 15:26:15 +000010131SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010132 assert(Subtarget->is64Bit() &&
10133 "LowerVAARG only handles 64-bit va_arg!");
10134 assert((Subtarget->isTargetLinux() ||
10135 Subtarget->isTargetDarwin()) &&
10136 "Unhandled target in LowerVAARG");
10137 assert(Op.getNode()->getNumOperands() == 4);
10138 SDValue Chain = Op.getOperand(0);
10139 SDValue SrcPtr = Op.getOperand(1);
10140 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10141 unsigned Align = Op.getConstantOperandVal(3);
10142 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010143
Dan Gohman320afb82010-10-12 18:00:49 +000010144 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010145 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010146 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010147 uint8_t ArgMode;
10148
10149 // Decide which area this value should be read from.
10150 // TODO: Implement the AMD64 ABI in its entirety. This simple
10151 // selection mechanism works only for the basic types.
10152 if (ArgVT == MVT::f80) {
10153 llvm_unreachable("va_arg for f80 not yet implemented");
10154 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10155 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10156 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10157 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10158 } else {
10159 llvm_unreachable("Unhandled argument type in LowerVAARG");
10160 }
10161
10162 if (ArgMode == 2) {
10163 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010164 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010165 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010166 .getFunction()->getAttributes()
10167 .hasAttribute(AttributeSet::FunctionIndex,
10168 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010169 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010170 }
10171
10172 // Insert VAARG_64 node into the DAG
10173 // VAARG_64 returns two values: Variable Argument Address, Chain
10174 SmallVector<SDValue, 11> InstOps;
10175 InstOps.push_back(Chain);
10176 InstOps.push_back(SrcPtr);
10177 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10178 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10179 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10180 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10181 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10182 VTs, &InstOps[0], InstOps.size(),
10183 MVT::i64,
10184 MachinePointerInfo(SV),
10185 /*Align=*/0,
10186 /*Volatile=*/false,
10187 /*ReadMem=*/true,
10188 /*WriteMem=*/true);
10189 Chain = VAARG.getValue(1);
10190
10191 // Load the next argument and return it
10192 return DAG.getLoad(ArgVT, dl,
10193 Chain,
10194 VAARG,
10195 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010196 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010197}
10198
Craig Topper55b24052012-09-11 06:15:32 +000010199static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10200 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010201 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010202 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010203 SDValue Chain = Op.getOperand(0);
10204 SDValue DstPtr = Op.getOperand(1);
10205 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010206 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10207 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010208 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010209
Chris Lattnere72f2022010-09-21 05:40:29 +000010210 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010211 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010212 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010213 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010214}
10215
Craig Topperff3139f2013-02-19 07:43:59 +000010216// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010217// may or may not be a constant. Takes immediate version of shift as input.
10218static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10219 SDValue SrcOp, SDValue ShAmt,
10220 SelectionDAG &DAG) {
10221 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10222
10223 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010224 // Constant may be a TargetConstant. Use a regular constant.
10225 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010226 switch (Opc) {
10227 default: llvm_unreachable("Unknown target vector shift node");
10228 case X86ISD::VSHLI:
10229 case X86ISD::VSRLI:
10230 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010231 return DAG.getNode(Opc, dl, VT, SrcOp,
10232 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010233 }
10234 }
10235
10236 // Change opcode to non-immediate version
10237 switch (Opc) {
10238 default: llvm_unreachable("Unknown target vector shift node");
10239 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10240 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10241 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10242 }
10243
10244 // Need to build a vector containing shift amount
10245 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10246 SDValue ShOps[4];
10247 ShOps[0] = ShAmt;
10248 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010249 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010250 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010251
10252 // The return type has to be a 128-bit type with the same element
10253 // type as the input type.
10254 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10255 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10256
10257 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010258 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10259}
10260
Craig Topper55b24052012-09-11 06:15:32 +000010261static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010262 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010263 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010264 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010265 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010266 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010267 case Intrinsic::x86_sse_comieq_ss:
10268 case Intrinsic::x86_sse_comilt_ss:
10269 case Intrinsic::x86_sse_comile_ss:
10270 case Intrinsic::x86_sse_comigt_ss:
10271 case Intrinsic::x86_sse_comige_ss:
10272 case Intrinsic::x86_sse_comineq_ss:
10273 case Intrinsic::x86_sse_ucomieq_ss:
10274 case Intrinsic::x86_sse_ucomilt_ss:
10275 case Intrinsic::x86_sse_ucomile_ss:
10276 case Intrinsic::x86_sse_ucomigt_ss:
10277 case Intrinsic::x86_sse_ucomige_ss:
10278 case Intrinsic::x86_sse_ucomineq_ss:
10279 case Intrinsic::x86_sse2_comieq_sd:
10280 case Intrinsic::x86_sse2_comilt_sd:
10281 case Intrinsic::x86_sse2_comile_sd:
10282 case Intrinsic::x86_sse2_comigt_sd:
10283 case Intrinsic::x86_sse2_comige_sd:
10284 case Intrinsic::x86_sse2_comineq_sd:
10285 case Intrinsic::x86_sse2_ucomieq_sd:
10286 case Intrinsic::x86_sse2_ucomilt_sd:
10287 case Intrinsic::x86_sse2_ucomile_sd:
10288 case Intrinsic::x86_sse2_ucomigt_sd:
10289 case Intrinsic::x86_sse2_ucomige_sd:
10290 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010291 unsigned Opc;
10292 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010293 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010294 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010295 case Intrinsic::x86_sse_comieq_ss:
10296 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010297 Opc = X86ISD::COMI;
10298 CC = ISD::SETEQ;
10299 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010300 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010301 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010302 Opc = X86ISD::COMI;
10303 CC = ISD::SETLT;
10304 break;
10305 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010306 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010307 Opc = X86ISD::COMI;
10308 CC = ISD::SETLE;
10309 break;
10310 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010311 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010312 Opc = X86ISD::COMI;
10313 CC = ISD::SETGT;
10314 break;
10315 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010316 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010317 Opc = X86ISD::COMI;
10318 CC = ISD::SETGE;
10319 break;
10320 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010321 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010322 Opc = X86ISD::COMI;
10323 CC = ISD::SETNE;
10324 break;
10325 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010326 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010327 Opc = X86ISD::UCOMI;
10328 CC = ISD::SETEQ;
10329 break;
10330 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010331 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010332 Opc = X86ISD::UCOMI;
10333 CC = ISD::SETLT;
10334 break;
10335 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010336 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010337 Opc = X86ISD::UCOMI;
10338 CC = ISD::SETLE;
10339 break;
10340 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010341 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010342 Opc = X86ISD::UCOMI;
10343 CC = ISD::SETGT;
10344 break;
10345 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010346 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010347 Opc = X86ISD::UCOMI;
10348 CC = ISD::SETGE;
10349 break;
10350 case Intrinsic::x86_sse_ucomineq_ss:
10351 case Intrinsic::x86_sse2_ucomineq_sd:
10352 Opc = X86ISD::UCOMI;
10353 CC = ISD::SETNE;
10354 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010355 }
Evan Cheng734503b2006-09-11 02:19:56 +000010356
Dan Gohman475871a2008-07-27 21:46:04 +000010357 SDValue LHS = Op.getOperand(1);
10358 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010359 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010360 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010361 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10362 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10363 DAG.getConstant(X86CC, MVT::i8), Cond);
10364 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010365 }
Craig Topper6d688152012-08-14 07:43:25 +000010366
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010367 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010368 case Intrinsic::x86_sse2_pmulu_dq:
10369 case Intrinsic::x86_avx2_pmulu_dq:
10370 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10371 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010372
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010373 // SSE2/AVX2 sub with unsigned saturation intrinsics
10374 case Intrinsic::x86_sse2_psubus_b:
10375 case Intrinsic::x86_sse2_psubus_w:
10376 case Intrinsic::x86_avx2_psubus_b:
10377 case Intrinsic::x86_avx2_psubus_w:
10378 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10379 Op.getOperand(1), Op.getOperand(2));
10380
Craig Topper6d688152012-08-14 07:43:25 +000010381 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010382 case Intrinsic::x86_sse3_hadd_ps:
10383 case Intrinsic::x86_sse3_hadd_pd:
10384 case Intrinsic::x86_avx_hadd_ps_256:
10385 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010386 case Intrinsic::x86_sse3_hsub_ps:
10387 case Intrinsic::x86_sse3_hsub_pd:
10388 case Intrinsic::x86_avx_hsub_ps_256:
10389 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010390 case Intrinsic::x86_ssse3_phadd_w_128:
10391 case Intrinsic::x86_ssse3_phadd_d_128:
10392 case Intrinsic::x86_avx2_phadd_w:
10393 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010394 case Intrinsic::x86_ssse3_phsub_w_128:
10395 case Intrinsic::x86_ssse3_phsub_d_128:
10396 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010397 case Intrinsic::x86_avx2_phsub_d: {
10398 unsigned Opcode;
10399 switch (IntNo) {
10400 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10401 case Intrinsic::x86_sse3_hadd_ps:
10402 case Intrinsic::x86_sse3_hadd_pd:
10403 case Intrinsic::x86_avx_hadd_ps_256:
10404 case Intrinsic::x86_avx_hadd_pd_256:
10405 Opcode = X86ISD::FHADD;
10406 break;
10407 case Intrinsic::x86_sse3_hsub_ps:
10408 case Intrinsic::x86_sse3_hsub_pd:
10409 case Intrinsic::x86_avx_hsub_ps_256:
10410 case Intrinsic::x86_avx_hsub_pd_256:
10411 Opcode = X86ISD::FHSUB;
10412 break;
10413 case Intrinsic::x86_ssse3_phadd_w_128:
10414 case Intrinsic::x86_ssse3_phadd_d_128:
10415 case Intrinsic::x86_avx2_phadd_w:
10416 case Intrinsic::x86_avx2_phadd_d:
10417 Opcode = X86ISD::HADD;
10418 break;
10419 case Intrinsic::x86_ssse3_phsub_w_128:
10420 case Intrinsic::x86_ssse3_phsub_d_128:
10421 case Intrinsic::x86_avx2_phsub_w:
10422 case Intrinsic::x86_avx2_phsub_d:
10423 Opcode = X86ISD::HSUB;
10424 break;
10425 }
10426 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010427 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010428 }
10429
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010430 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10431 case Intrinsic::x86_sse2_pmaxu_b:
10432 case Intrinsic::x86_sse41_pmaxuw:
10433 case Intrinsic::x86_sse41_pmaxud:
10434 case Intrinsic::x86_avx2_pmaxu_b:
10435 case Intrinsic::x86_avx2_pmaxu_w:
10436 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010437 case Intrinsic::x86_sse2_pminu_b:
10438 case Intrinsic::x86_sse41_pminuw:
10439 case Intrinsic::x86_sse41_pminud:
10440 case Intrinsic::x86_avx2_pminu_b:
10441 case Intrinsic::x86_avx2_pminu_w:
10442 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010443 case Intrinsic::x86_sse41_pmaxsb:
10444 case Intrinsic::x86_sse2_pmaxs_w:
10445 case Intrinsic::x86_sse41_pmaxsd:
10446 case Intrinsic::x86_avx2_pmaxs_b:
10447 case Intrinsic::x86_avx2_pmaxs_w:
10448 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010449 case Intrinsic::x86_sse41_pminsb:
10450 case Intrinsic::x86_sse2_pmins_w:
10451 case Intrinsic::x86_sse41_pminsd:
10452 case Intrinsic::x86_avx2_pmins_b:
10453 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010454 case Intrinsic::x86_avx2_pmins_d: {
10455 unsigned Opcode;
10456 switch (IntNo) {
10457 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10458 case Intrinsic::x86_sse2_pmaxu_b:
10459 case Intrinsic::x86_sse41_pmaxuw:
10460 case Intrinsic::x86_sse41_pmaxud:
10461 case Intrinsic::x86_avx2_pmaxu_b:
10462 case Intrinsic::x86_avx2_pmaxu_w:
10463 case Intrinsic::x86_avx2_pmaxu_d:
10464 Opcode = X86ISD::UMAX;
10465 break;
10466 case Intrinsic::x86_sse2_pminu_b:
10467 case Intrinsic::x86_sse41_pminuw:
10468 case Intrinsic::x86_sse41_pminud:
10469 case Intrinsic::x86_avx2_pminu_b:
10470 case Intrinsic::x86_avx2_pminu_w:
10471 case Intrinsic::x86_avx2_pminu_d:
10472 Opcode = X86ISD::UMIN;
10473 break;
10474 case Intrinsic::x86_sse41_pmaxsb:
10475 case Intrinsic::x86_sse2_pmaxs_w:
10476 case Intrinsic::x86_sse41_pmaxsd:
10477 case Intrinsic::x86_avx2_pmaxs_b:
10478 case Intrinsic::x86_avx2_pmaxs_w:
10479 case Intrinsic::x86_avx2_pmaxs_d:
10480 Opcode = X86ISD::SMAX;
10481 break;
10482 case Intrinsic::x86_sse41_pminsb:
10483 case Intrinsic::x86_sse2_pmins_w:
10484 case Intrinsic::x86_sse41_pminsd:
10485 case Intrinsic::x86_avx2_pmins_b:
10486 case Intrinsic::x86_avx2_pmins_w:
10487 case Intrinsic::x86_avx2_pmins_d:
10488 Opcode = X86ISD::SMIN;
10489 break;
10490 }
10491 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010492 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010493 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010494
Craig Topper6d183e42012-12-29 16:44:25 +000010495 // SSE/SSE2/AVX floating point max/min intrinsics.
10496 case Intrinsic::x86_sse_max_ps:
10497 case Intrinsic::x86_sse2_max_pd:
10498 case Intrinsic::x86_avx_max_ps_256:
10499 case Intrinsic::x86_avx_max_pd_256:
10500 case Intrinsic::x86_sse_min_ps:
10501 case Intrinsic::x86_sse2_min_pd:
10502 case Intrinsic::x86_avx_min_ps_256:
10503 case Intrinsic::x86_avx_min_pd_256: {
10504 unsigned Opcode;
10505 switch (IntNo) {
10506 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10507 case Intrinsic::x86_sse_max_ps:
10508 case Intrinsic::x86_sse2_max_pd:
10509 case Intrinsic::x86_avx_max_ps_256:
10510 case Intrinsic::x86_avx_max_pd_256:
10511 Opcode = X86ISD::FMAX;
10512 break;
10513 case Intrinsic::x86_sse_min_ps:
10514 case Intrinsic::x86_sse2_min_pd:
10515 case Intrinsic::x86_avx_min_ps_256:
10516 case Intrinsic::x86_avx_min_pd_256:
10517 Opcode = X86ISD::FMIN;
10518 break;
10519 }
10520 return DAG.getNode(Opcode, dl, Op.getValueType(),
10521 Op.getOperand(1), Op.getOperand(2));
10522 }
10523
Craig Topper6d688152012-08-14 07:43:25 +000010524 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010525 case Intrinsic::x86_avx2_psllv_d:
10526 case Intrinsic::x86_avx2_psllv_q:
10527 case Intrinsic::x86_avx2_psllv_d_256:
10528 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010529 case Intrinsic::x86_avx2_psrlv_d:
10530 case Intrinsic::x86_avx2_psrlv_q:
10531 case Intrinsic::x86_avx2_psrlv_d_256:
10532 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010533 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010534 case Intrinsic::x86_avx2_psrav_d_256: {
10535 unsigned Opcode;
10536 switch (IntNo) {
10537 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10538 case Intrinsic::x86_avx2_psllv_d:
10539 case Intrinsic::x86_avx2_psllv_q:
10540 case Intrinsic::x86_avx2_psllv_d_256:
10541 case Intrinsic::x86_avx2_psllv_q_256:
10542 Opcode = ISD::SHL;
10543 break;
10544 case Intrinsic::x86_avx2_psrlv_d:
10545 case Intrinsic::x86_avx2_psrlv_q:
10546 case Intrinsic::x86_avx2_psrlv_d_256:
10547 case Intrinsic::x86_avx2_psrlv_q_256:
10548 Opcode = ISD::SRL;
10549 break;
10550 case Intrinsic::x86_avx2_psrav_d:
10551 case Intrinsic::x86_avx2_psrav_d_256:
10552 Opcode = ISD::SRA;
10553 break;
10554 }
10555 return DAG.getNode(Opcode, dl, Op.getValueType(),
10556 Op.getOperand(1), Op.getOperand(2));
10557 }
10558
Craig Topper969ba282012-01-25 06:43:11 +000010559 case Intrinsic::x86_ssse3_pshuf_b_128:
10560 case Intrinsic::x86_avx2_pshuf_b:
10561 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10562 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010563
Craig Topper969ba282012-01-25 06:43:11 +000010564 case Intrinsic::x86_ssse3_psign_b_128:
10565 case Intrinsic::x86_ssse3_psign_w_128:
10566 case Intrinsic::x86_ssse3_psign_d_128:
10567 case Intrinsic::x86_avx2_psign_b:
10568 case Intrinsic::x86_avx2_psign_w:
10569 case Intrinsic::x86_avx2_psign_d:
10570 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10571 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010572
Craig Toppere566cd02012-01-26 07:18:03 +000010573 case Intrinsic::x86_sse41_insertps:
10574 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10575 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010576
Craig Toppere566cd02012-01-26 07:18:03 +000010577 case Intrinsic::x86_avx_vperm2f128_ps_256:
10578 case Intrinsic::x86_avx_vperm2f128_pd_256:
10579 case Intrinsic::x86_avx_vperm2f128_si_256:
10580 case Intrinsic::x86_avx2_vperm2i128:
10581 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10582 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010583
Craig Topperffa6c402012-04-16 07:13:00 +000010584 case Intrinsic::x86_avx2_permd:
10585 case Intrinsic::x86_avx2_permps:
10586 // Operands intentionally swapped. Mask is last operand to intrinsic,
10587 // but second operand for node/intruction.
10588 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10589 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010590
Craig Topper22d8f0d2012-12-29 18:18:20 +000010591 case Intrinsic::x86_sse_sqrt_ps:
10592 case Intrinsic::x86_sse2_sqrt_pd:
10593 case Intrinsic::x86_avx_sqrt_ps_256:
10594 case Intrinsic::x86_avx_sqrt_pd_256:
10595 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10596
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010597 // ptest and testp intrinsics. The intrinsic these come from are designed to
10598 // return an integer value, not just an instruction so lower it to the ptest
10599 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010600 case Intrinsic::x86_sse41_ptestz:
10601 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010602 case Intrinsic::x86_sse41_ptestnzc:
10603 case Intrinsic::x86_avx_ptestz_256:
10604 case Intrinsic::x86_avx_ptestc_256:
10605 case Intrinsic::x86_avx_ptestnzc_256:
10606 case Intrinsic::x86_avx_vtestz_ps:
10607 case Intrinsic::x86_avx_vtestc_ps:
10608 case Intrinsic::x86_avx_vtestnzc_ps:
10609 case Intrinsic::x86_avx_vtestz_pd:
10610 case Intrinsic::x86_avx_vtestc_pd:
10611 case Intrinsic::x86_avx_vtestnzc_pd:
10612 case Intrinsic::x86_avx_vtestz_ps_256:
10613 case Intrinsic::x86_avx_vtestc_ps_256:
10614 case Intrinsic::x86_avx_vtestnzc_ps_256:
10615 case Intrinsic::x86_avx_vtestz_pd_256:
10616 case Intrinsic::x86_avx_vtestc_pd_256:
10617 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10618 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010619 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010620 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010621 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010622 case Intrinsic::x86_avx_vtestz_ps:
10623 case Intrinsic::x86_avx_vtestz_pd:
10624 case Intrinsic::x86_avx_vtestz_ps_256:
10625 case Intrinsic::x86_avx_vtestz_pd_256:
10626 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010627 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010628 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010629 // ZF = 1
10630 X86CC = X86::COND_E;
10631 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010632 case Intrinsic::x86_avx_vtestc_ps:
10633 case Intrinsic::x86_avx_vtestc_pd:
10634 case Intrinsic::x86_avx_vtestc_ps_256:
10635 case Intrinsic::x86_avx_vtestc_pd_256:
10636 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010637 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010638 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010639 // CF = 1
10640 X86CC = X86::COND_B;
10641 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010642 case Intrinsic::x86_avx_vtestnzc_ps:
10643 case Intrinsic::x86_avx_vtestnzc_pd:
10644 case Intrinsic::x86_avx_vtestnzc_ps_256:
10645 case Intrinsic::x86_avx_vtestnzc_pd_256:
10646 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010647 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010648 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010649 // ZF and CF = 0
10650 X86CC = X86::COND_A;
10651 break;
10652 }
Eric Christopherfd179292009-08-27 18:07:15 +000010653
Eric Christopher71c67532009-07-29 00:28:05 +000010654 SDValue LHS = Op.getOperand(1);
10655 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010656 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10657 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010658 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10659 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10660 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010661 }
Evan Cheng5759f972008-05-04 09:15:50 +000010662
Craig Topper80e46362012-01-23 06:16:53 +000010663 // SSE/AVX shift intrinsics
10664 case Intrinsic::x86_sse2_psll_w:
10665 case Intrinsic::x86_sse2_psll_d:
10666 case Intrinsic::x86_sse2_psll_q:
10667 case Intrinsic::x86_avx2_psll_w:
10668 case Intrinsic::x86_avx2_psll_d:
10669 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010670 case Intrinsic::x86_sse2_psrl_w:
10671 case Intrinsic::x86_sse2_psrl_d:
10672 case Intrinsic::x86_sse2_psrl_q:
10673 case Intrinsic::x86_avx2_psrl_w:
10674 case Intrinsic::x86_avx2_psrl_d:
10675 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010676 case Intrinsic::x86_sse2_psra_w:
10677 case Intrinsic::x86_sse2_psra_d:
10678 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010679 case Intrinsic::x86_avx2_psra_d: {
10680 unsigned Opcode;
10681 switch (IntNo) {
10682 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10683 case Intrinsic::x86_sse2_psll_w:
10684 case Intrinsic::x86_sse2_psll_d:
10685 case Intrinsic::x86_sse2_psll_q:
10686 case Intrinsic::x86_avx2_psll_w:
10687 case Intrinsic::x86_avx2_psll_d:
10688 case Intrinsic::x86_avx2_psll_q:
10689 Opcode = X86ISD::VSHL;
10690 break;
10691 case Intrinsic::x86_sse2_psrl_w:
10692 case Intrinsic::x86_sse2_psrl_d:
10693 case Intrinsic::x86_sse2_psrl_q:
10694 case Intrinsic::x86_avx2_psrl_w:
10695 case Intrinsic::x86_avx2_psrl_d:
10696 case Intrinsic::x86_avx2_psrl_q:
10697 Opcode = X86ISD::VSRL;
10698 break;
10699 case Intrinsic::x86_sse2_psra_w:
10700 case Intrinsic::x86_sse2_psra_d:
10701 case Intrinsic::x86_avx2_psra_w:
10702 case Intrinsic::x86_avx2_psra_d:
10703 Opcode = X86ISD::VSRA;
10704 break;
10705 }
10706 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010707 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010708 }
10709
10710 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010711 case Intrinsic::x86_sse2_pslli_w:
10712 case Intrinsic::x86_sse2_pslli_d:
10713 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010714 case Intrinsic::x86_avx2_pslli_w:
10715 case Intrinsic::x86_avx2_pslli_d:
10716 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010717 case Intrinsic::x86_sse2_psrli_w:
10718 case Intrinsic::x86_sse2_psrli_d:
10719 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010720 case Intrinsic::x86_avx2_psrli_w:
10721 case Intrinsic::x86_avx2_psrli_d:
10722 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010723 case Intrinsic::x86_sse2_psrai_w:
10724 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010725 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010726 case Intrinsic::x86_avx2_psrai_d: {
10727 unsigned Opcode;
10728 switch (IntNo) {
10729 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10730 case Intrinsic::x86_sse2_pslli_w:
10731 case Intrinsic::x86_sse2_pslli_d:
10732 case Intrinsic::x86_sse2_pslli_q:
10733 case Intrinsic::x86_avx2_pslli_w:
10734 case Intrinsic::x86_avx2_pslli_d:
10735 case Intrinsic::x86_avx2_pslli_q:
10736 Opcode = X86ISD::VSHLI;
10737 break;
10738 case Intrinsic::x86_sse2_psrli_w:
10739 case Intrinsic::x86_sse2_psrli_d:
10740 case Intrinsic::x86_sse2_psrli_q:
10741 case Intrinsic::x86_avx2_psrli_w:
10742 case Intrinsic::x86_avx2_psrli_d:
10743 case Intrinsic::x86_avx2_psrli_q:
10744 Opcode = X86ISD::VSRLI;
10745 break;
10746 case Intrinsic::x86_sse2_psrai_w:
10747 case Intrinsic::x86_sse2_psrai_d:
10748 case Intrinsic::x86_avx2_psrai_w:
10749 case Intrinsic::x86_avx2_psrai_d:
10750 Opcode = X86ISD::VSRAI;
10751 break;
10752 }
10753 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010754 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010755 }
10756
Craig Topper4feb6472012-08-06 06:22:36 +000010757 case Intrinsic::x86_sse42_pcmpistria128:
10758 case Intrinsic::x86_sse42_pcmpestria128:
10759 case Intrinsic::x86_sse42_pcmpistric128:
10760 case Intrinsic::x86_sse42_pcmpestric128:
10761 case Intrinsic::x86_sse42_pcmpistrio128:
10762 case Intrinsic::x86_sse42_pcmpestrio128:
10763 case Intrinsic::x86_sse42_pcmpistris128:
10764 case Intrinsic::x86_sse42_pcmpestris128:
10765 case Intrinsic::x86_sse42_pcmpistriz128:
10766 case Intrinsic::x86_sse42_pcmpestriz128: {
10767 unsigned Opcode;
10768 unsigned X86CC;
10769 switch (IntNo) {
10770 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10771 case Intrinsic::x86_sse42_pcmpistria128:
10772 Opcode = X86ISD::PCMPISTRI;
10773 X86CC = X86::COND_A;
10774 break;
10775 case Intrinsic::x86_sse42_pcmpestria128:
10776 Opcode = X86ISD::PCMPESTRI;
10777 X86CC = X86::COND_A;
10778 break;
10779 case Intrinsic::x86_sse42_pcmpistric128:
10780 Opcode = X86ISD::PCMPISTRI;
10781 X86CC = X86::COND_B;
10782 break;
10783 case Intrinsic::x86_sse42_pcmpestric128:
10784 Opcode = X86ISD::PCMPESTRI;
10785 X86CC = X86::COND_B;
10786 break;
10787 case Intrinsic::x86_sse42_pcmpistrio128:
10788 Opcode = X86ISD::PCMPISTRI;
10789 X86CC = X86::COND_O;
10790 break;
10791 case Intrinsic::x86_sse42_pcmpestrio128:
10792 Opcode = X86ISD::PCMPESTRI;
10793 X86CC = X86::COND_O;
10794 break;
10795 case Intrinsic::x86_sse42_pcmpistris128:
10796 Opcode = X86ISD::PCMPISTRI;
10797 X86CC = X86::COND_S;
10798 break;
10799 case Intrinsic::x86_sse42_pcmpestris128:
10800 Opcode = X86ISD::PCMPESTRI;
10801 X86CC = X86::COND_S;
10802 break;
10803 case Intrinsic::x86_sse42_pcmpistriz128:
10804 Opcode = X86ISD::PCMPISTRI;
10805 X86CC = X86::COND_E;
10806 break;
10807 case Intrinsic::x86_sse42_pcmpestriz128:
10808 Opcode = X86ISD::PCMPESTRI;
10809 X86CC = X86::COND_E;
10810 break;
10811 }
10812 SmallVector<SDValue, 5> NewOps;
10813 NewOps.append(Op->op_begin()+1, Op->op_end());
10814 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10815 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10816 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10817 DAG.getConstant(X86CC, MVT::i8),
10818 SDValue(PCMP.getNode(), 1));
10819 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10820 }
Craig Topper6d688152012-08-14 07:43:25 +000010821
Craig Topper4feb6472012-08-06 06:22:36 +000010822 case Intrinsic::x86_sse42_pcmpistri128:
10823 case Intrinsic::x86_sse42_pcmpestri128: {
10824 unsigned Opcode;
10825 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10826 Opcode = X86ISD::PCMPISTRI;
10827 else
10828 Opcode = X86ISD::PCMPESTRI;
10829
10830 SmallVector<SDValue, 5> NewOps;
10831 NewOps.append(Op->op_begin()+1, Op->op_end());
10832 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10833 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10834 }
Craig Topper0e292372012-08-24 04:03:22 +000010835 case Intrinsic::x86_fma_vfmadd_ps:
10836 case Intrinsic::x86_fma_vfmadd_pd:
10837 case Intrinsic::x86_fma_vfmsub_ps:
10838 case Intrinsic::x86_fma_vfmsub_pd:
10839 case Intrinsic::x86_fma_vfnmadd_ps:
10840 case Intrinsic::x86_fma_vfnmadd_pd:
10841 case Intrinsic::x86_fma_vfnmsub_ps:
10842 case Intrinsic::x86_fma_vfnmsub_pd:
10843 case Intrinsic::x86_fma_vfmaddsub_ps:
10844 case Intrinsic::x86_fma_vfmaddsub_pd:
10845 case Intrinsic::x86_fma_vfmsubadd_ps:
10846 case Intrinsic::x86_fma_vfmsubadd_pd:
10847 case Intrinsic::x86_fma_vfmadd_ps_256:
10848 case Intrinsic::x86_fma_vfmadd_pd_256:
10849 case Intrinsic::x86_fma_vfmsub_ps_256:
10850 case Intrinsic::x86_fma_vfmsub_pd_256:
10851 case Intrinsic::x86_fma_vfnmadd_ps_256:
10852 case Intrinsic::x86_fma_vfnmadd_pd_256:
10853 case Intrinsic::x86_fma_vfnmsub_ps_256:
10854 case Intrinsic::x86_fma_vfnmsub_pd_256:
10855 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10856 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10857 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10858 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010859 unsigned Opc;
10860 switch (IntNo) {
10861 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10862 case Intrinsic::x86_fma_vfmadd_ps:
10863 case Intrinsic::x86_fma_vfmadd_pd:
10864 case Intrinsic::x86_fma_vfmadd_ps_256:
10865 case Intrinsic::x86_fma_vfmadd_pd_256:
10866 Opc = X86ISD::FMADD;
10867 break;
10868 case Intrinsic::x86_fma_vfmsub_ps:
10869 case Intrinsic::x86_fma_vfmsub_pd:
10870 case Intrinsic::x86_fma_vfmsub_ps_256:
10871 case Intrinsic::x86_fma_vfmsub_pd_256:
10872 Opc = X86ISD::FMSUB;
10873 break;
10874 case Intrinsic::x86_fma_vfnmadd_ps:
10875 case Intrinsic::x86_fma_vfnmadd_pd:
10876 case Intrinsic::x86_fma_vfnmadd_ps_256:
10877 case Intrinsic::x86_fma_vfnmadd_pd_256:
10878 Opc = X86ISD::FNMADD;
10879 break;
10880 case Intrinsic::x86_fma_vfnmsub_ps:
10881 case Intrinsic::x86_fma_vfnmsub_pd:
10882 case Intrinsic::x86_fma_vfnmsub_ps_256:
10883 case Intrinsic::x86_fma_vfnmsub_pd_256:
10884 Opc = X86ISD::FNMSUB;
10885 break;
10886 case Intrinsic::x86_fma_vfmaddsub_ps:
10887 case Intrinsic::x86_fma_vfmaddsub_pd:
10888 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10889 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10890 Opc = X86ISD::FMADDSUB;
10891 break;
10892 case Intrinsic::x86_fma_vfmsubadd_ps:
10893 case Intrinsic::x86_fma_vfmsubadd_pd:
10894 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10895 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10896 Opc = X86ISD::FMSUBADD;
10897 break;
10898 }
10899
10900 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10901 Op.getOperand(2), Op.getOperand(3));
10902 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010903 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010904}
Evan Cheng72261582005-12-20 06:22:03 +000010905
Craig Topper55b24052012-09-11 06:15:32 +000010906static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010907 DebugLoc dl = Op.getDebugLoc();
10908 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10909 switch (IntNo) {
10910 default: return SDValue(); // Don't custom lower most intrinsics.
10911
10912 // RDRAND intrinsics.
10913 case Intrinsic::x86_rdrand_16:
10914 case Intrinsic::x86_rdrand_32:
10915 case Intrinsic::x86_rdrand_64: {
10916 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010917 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10918 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010919
10920 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10921 // return the value from Rand, which is always 0, casted to i32.
10922 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10923 DAG.getConstant(1, Op->getValueType(1)),
10924 DAG.getConstant(X86::COND_B, MVT::i32),
10925 SDValue(Result.getNode(), 1) };
10926 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10927 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10928 Ops, 4);
10929
10930 // Return { result, isValid, chain }.
10931 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010932 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010933 }
Michael Liaof8fd8832013-03-26 22:47:01 +000010934
10935 // XTEST intrinsics.
10936 case Intrinsic::x86_xtest: {
10937 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
10938 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
10939 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10940 DAG.getConstant(X86::COND_NE, MVT::i8),
10941 InTrans);
10942 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
10943 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
10944 Ret, SDValue(InTrans.getNode(), 1));
10945 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010946 }
10947}
10948
Dan Gohmand858e902010-04-17 15:26:15 +000010949SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10950 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010951 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10952 MFI->setReturnAddressIsTaken(true);
10953
Bill Wendling64e87322009-01-16 19:25:27 +000010954 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010955 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010956 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010957
10958 if (Depth > 0) {
10959 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10960 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010961 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10962 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10963 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010964 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010965 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010966 }
10967
10968 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010969 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010970 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010971 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010972}
10973
Dan Gohmand858e902010-04-17 15:26:15 +000010974SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010975 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10976 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010977
Owen Andersone50ed302009-08-10 22:56:29 +000010978 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010979 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010980 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10981 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010982 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010983 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010984 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10985 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010986 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010987 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010988}
10989
Dan Gohman475871a2008-07-27 21:46:04 +000010990SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010991 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010992 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010993}
10994
Dan Gohmand858e902010-04-17 15:26:15 +000010995SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010996 SDValue Chain = Op.getOperand(0);
10997 SDValue Offset = Op.getOperand(1);
10998 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010999 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011000
Dan Gohmand8816272010-08-11 18:14:00 +000011001 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
11002 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
11003 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011004 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011005
Dan Gohmand8816272010-08-11 18:14:00 +000011006 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011007 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011008 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011009 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11010 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011011 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011012
Dale Johannesene4d209d2009-02-03 20:21:25 +000011013 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011014 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011015 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011016}
11017
Michael Liao6c0e04c2012-10-15 22:39:43 +000011018SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11019 SelectionDAG &DAG) const {
11020 DebugLoc DL = Op.getDebugLoc();
11021 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11022 DAG.getVTList(MVT::i32, MVT::Other),
11023 Op.getOperand(0), Op.getOperand(1));
11024}
11025
11026SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11027 SelectionDAG &DAG) const {
11028 DebugLoc DL = Op.getDebugLoc();
11029 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11030 Op.getOperand(0), Op.getOperand(1));
11031}
11032
Craig Topper55b24052012-09-11 06:15:32 +000011033static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011034 return Op.getOperand(0);
11035}
11036
11037SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11038 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011039 SDValue Root = Op.getOperand(0);
11040 SDValue Trmp = Op.getOperand(1); // trampoline
11041 SDValue FPtr = Op.getOperand(2); // nested function
11042 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011043 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011044
Dan Gohman69de1932008-02-06 22:27:42 +000011045 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011046 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011047
11048 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011049 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011050
11051 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011052 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11053 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011054
Michael Liao7abf67a2012-10-04 19:50:43 +000011055 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11056 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011057
11058 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11059
11060 // Load the pointer to the nested function into R11.
11061 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011062 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011063 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011064 Addr, MachinePointerInfo(TrmpAddr),
11065 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011066
Owen Anderson825b72b2009-08-11 20:47:22 +000011067 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11068 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011069 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11070 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011071 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011072
11073 // Load the 'nest' parameter value into R10.
11074 // R10 is specified in X86CallingConv.td
11075 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011076 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11077 DAG.getConstant(10, MVT::i64));
11078 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011079 Addr, MachinePointerInfo(TrmpAddr, 10),
11080 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011081
Owen Anderson825b72b2009-08-11 20:47:22 +000011082 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11083 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011084 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11085 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011086 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011087
11088 // Jump to the nested function.
11089 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011090 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11091 DAG.getConstant(20, MVT::i64));
11092 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011093 Addr, MachinePointerInfo(TrmpAddr, 20),
11094 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011095
11096 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011097 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11098 DAG.getConstant(22, MVT::i64));
11099 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011100 MachinePointerInfo(TrmpAddr, 22),
11101 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011102
Duncan Sands4a544a72011-09-06 13:37:06 +000011103 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011104 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011105 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011106 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011107 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011108 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011109
11110 switch (CC) {
11111 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011112 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011113 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011114 case CallingConv::X86_StdCall: {
11115 // Pass 'nest' parameter in ECX.
11116 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011117 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011118
11119 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011120 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011121 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011122
Chris Lattner58d74912008-03-12 17:45:29 +000011123 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011124 unsigned InRegCount = 0;
11125 unsigned Idx = 1;
11126
11127 for (FunctionType::param_iterator I = FTy->param_begin(),
11128 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011129 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011130 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011131 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011132
11133 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011134 report_fatal_error("Nest register in use - reduce number of inreg"
11135 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011136 }
11137 }
11138 break;
11139 }
11140 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011141 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011142 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011143 // Pass 'nest' parameter in EAX.
11144 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011145 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011146 break;
11147 }
11148
Dan Gohman475871a2008-07-27 21:46:04 +000011149 SDValue OutChains[4];
11150 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011151
Owen Anderson825b72b2009-08-11 20:47:22 +000011152 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11153 DAG.getConstant(10, MVT::i32));
11154 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011155
Chris Lattnera62fe662010-02-05 19:20:30 +000011156 // This is storing the opcode for MOV32ri.
11157 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011158 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011159 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011160 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011161 Trmp, MachinePointerInfo(TrmpAddr),
11162 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011163
Owen Anderson825b72b2009-08-11 20:47:22 +000011164 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11165 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011166 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11167 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011168 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011169
Chris Lattnera62fe662010-02-05 19:20:30 +000011170 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011171 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11172 DAG.getConstant(5, MVT::i32));
11173 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011174 MachinePointerInfo(TrmpAddr, 5),
11175 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011176
Owen Anderson825b72b2009-08-11 20:47:22 +000011177 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11178 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011179 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11180 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011181 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011182
Duncan Sands4a544a72011-09-06 13:37:06 +000011183 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011184 }
11185}
11186
Dan Gohmand858e902010-04-17 15:26:15 +000011187SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11188 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011189 /*
11190 The rounding mode is in bits 11:10 of FPSR, and has the following
11191 settings:
11192 00 Round to nearest
11193 01 Round to -inf
11194 10 Round to +inf
11195 11 Round to 0
11196
11197 FLT_ROUNDS, on the other hand, expects the following:
11198 -1 Undefined
11199 0 Round to 0
11200 1 Round to nearest
11201 2 Round to +inf
11202 3 Round to -inf
11203
11204 To perform the conversion, we do:
11205 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11206 */
11207
11208 MachineFunction &MF = DAG.getMachineFunction();
11209 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011210 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011211 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011212 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011213 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011214
11215 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011216 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011217 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011218
Chris Lattner2156b792010-09-22 01:11:26 +000011219 MachineMemOperand *MMO =
11220 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11221 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011222
Chris Lattner2156b792010-09-22 01:11:26 +000011223 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11224 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11225 DAG.getVTList(MVT::Other),
11226 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011227
11228 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011229 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011230 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011231
11232 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011233 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011234 DAG.getNode(ISD::SRL, DL, MVT::i16,
11235 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011236 CWD, DAG.getConstant(0x800, MVT::i16)),
11237 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011238 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011239 DAG.getNode(ISD::SRL, DL, MVT::i16,
11240 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011241 CWD, DAG.getConstant(0x400, MVT::i16)),
11242 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011243
Dan Gohman475871a2008-07-27 21:46:04 +000011244 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011245 DAG.getNode(ISD::AND, DL, MVT::i16,
11246 DAG.getNode(ISD::ADD, DL, MVT::i16,
11247 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011248 DAG.getConstant(1, MVT::i16)),
11249 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011250
Duncan Sands83ec4b62008-06-06 12:08:01 +000011251 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011252 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011253}
11254
Craig Topper55b24052012-09-11 06:15:32 +000011255static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011256 EVT VT = Op.getValueType();
11257 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011258 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011259 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011260
11261 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011262 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011263 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011264 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011265 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011266 }
Evan Cheng18efe262007-12-14 02:13:44 +000011267
Evan Cheng152804e2007-12-14 08:30:15 +000011268 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011269 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011270 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011271
11272 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011273 SDValue Ops[] = {
11274 Op,
11275 DAG.getConstant(NumBits+NumBits-1, OpVT),
11276 DAG.getConstant(X86::COND_E, MVT::i8),
11277 Op.getValue(1)
11278 };
11279 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011280
11281 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011282 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011283
Owen Anderson825b72b2009-08-11 20:47:22 +000011284 if (VT == MVT::i8)
11285 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011286 return Op;
11287}
11288
Craig Topper55b24052012-09-11 06:15:32 +000011289static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011290 EVT VT = Op.getValueType();
11291 EVT OpVT = VT;
11292 unsigned NumBits = VT.getSizeInBits();
11293 DebugLoc dl = Op.getDebugLoc();
11294
11295 Op = Op.getOperand(0);
11296 if (VT == MVT::i8) {
11297 // Zero extend to i32 since there is not an i8 bsr.
11298 OpVT = MVT::i32;
11299 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11300 }
11301
11302 // Issue a bsr (scan bits in reverse).
11303 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11304 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11305
11306 // And xor with NumBits-1.
11307 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11308
11309 if (VT == MVT::i8)
11310 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11311 return Op;
11312}
11313
Craig Topper55b24052012-09-11 06:15:32 +000011314static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011315 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011316 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011317 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011318 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011319
11320 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011321 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011322 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011323
11324 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011325 SDValue Ops[] = {
11326 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011327 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011328 DAG.getConstant(X86::COND_E, MVT::i8),
11329 Op.getValue(1)
11330 };
Chandler Carruth77821022011-12-24 12:12:34 +000011331 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011332}
11333
Craig Topper13894fa2011-08-24 06:14:18 +000011334// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11335// ones, and then concatenate the result back.
11336static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011337 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011338
Craig Topper7a9a28b2012-08-12 02:23:29 +000011339 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011340 "Unsupported value type for operation");
11341
Craig Topper66ddd152012-04-27 22:54:43 +000011342 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011343 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011344
11345 // Extract the LHS vectors
11346 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011347 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11348 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011349
11350 // Extract the RHS vectors
11351 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011352 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11353 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011354
11355 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11356 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11357
11358 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11359 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11360 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11361}
11362
Craig Topper55b24052012-09-11 06:15:32 +000011363static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011364 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011365 Op.getValueType().isInteger() &&
11366 "Only handle AVX 256-bit vector integer operation");
11367 return Lower256IntArith(Op, DAG);
11368}
11369
Craig Topper55b24052012-09-11 06:15:32 +000011370static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011371 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011372 Op.getValueType().isInteger() &&
11373 "Only handle AVX 256-bit vector integer operation");
11374 return Lower256IntArith(Op, DAG);
11375}
11376
Craig Topper55b24052012-09-11 06:15:32 +000011377static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11378 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011379 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011380 EVT VT = Op.getValueType();
11381
11382 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011383 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011384 return Lower256IntArith(Op, DAG);
11385
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011386 SDValue A = Op.getOperand(0);
11387 SDValue B = Op.getOperand(1);
11388
11389 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11390 if (VT == MVT::v4i32) {
11391 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11392 "Should not custom lower when pmuldq is available!");
11393
11394 // Extract the odd parts.
11395 const int UnpackMask[] = { 1, -1, 3, -1 };
11396 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11397 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11398
11399 // Multiply the even parts.
11400 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11401 // Now multiply odd parts.
11402 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11403
11404 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11405 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11406
11407 // Merge the two vectors back together with a shuffle. This expands into 2
11408 // shuffles.
11409 const int ShufMask[] = { 0, 4, 2, 6 };
11410 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11411 }
11412
Craig Topper5b209e82012-02-05 03:14:49 +000011413 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11414 "Only know how to lower V2I64/V4I64 multiply");
11415
Craig Topper5b209e82012-02-05 03:14:49 +000011416 // Ahi = psrlqi(a, 32);
11417 // Bhi = psrlqi(b, 32);
11418 //
11419 // AloBlo = pmuludq(a, b);
11420 // AloBhi = pmuludq(a, Bhi);
11421 // AhiBlo = pmuludq(Ahi, b);
11422
11423 // AloBhi = psllqi(AloBhi, 32);
11424 // AhiBlo = psllqi(AhiBlo, 32);
11425 // return AloBlo + AloBhi + AhiBlo;
11426
Craig Topper5b209e82012-02-05 03:14:49 +000011427 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011428
Craig Topper5b209e82012-02-05 03:14:49 +000011429 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11430 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011431
Craig Topper5b209e82012-02-05 03:14:49 +000011432 // Bit cast to 32-bit vectors for MULUDQ
11433 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11434 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11435 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11436 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11437 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011438
Craig Topper5b209e82012-02-05 03:14:49 +000011439 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11440 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11441 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011442
Craig Topper5b209e82012-02-05 03:14:49 +000011443 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11444 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011445
Dale Johannesene4d209d2009-02-03 20:21:25 +000011446 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011447 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011448}
11449
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011450SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11451 EVT VT = Op.getValueType();
11452 EVT EltTy = VT.getVectorElementType();
11453 unsigned NumElts = VT.getVectorNumElements();
11454 SDValue N0 = Op.getOperand(0);
11455 DebugLoc dl = Op.getDebugLoc();
11456
11457 // Lower sdiv X, pow2-const.
11458 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11459 if (!C)
11460 return SDValue();
11461
11462 APInt SplatValue, SplatUndef;
11463 unsigned MinSplatBits;
11464 bool HasAnyUndefs;
11465 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11466 return SDValue();
11467
11468 if ((SplatValue != 0) &&
11469 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11470 unsigned lg2 = SplatValue.countTrailingZeros();
11471 // Splat the sign bit.
11472 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11473 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11474 // Add (N0 < 0) ? abs2 - 1 : 0;
11475 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11476 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11477 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11478 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11479 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11480
11481 // If we're dividing by a positive value, we're done. Otherwise, we must
11482 // negate the result.
11483 if (SplatValue.isNonNegative())
11484 return SRA;
11485
11486 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11487 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11488 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11489 }
11490 return SDValue();
11491}
11492
Michael Liao4b7ab122013-03-20 02:20:36 +000011493static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11494 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011495 EVT VT = Op.getValueType();
11496 DebugLoc dl = Op.getDebugLoc();
11497 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011498 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011499
Nadav Rotem43012222011-05-11 08:12:09 +000011500 // Optimize shl/srl/sra with constant shift amount.
11501 if (isSplatVector(Amt.getNode())) {
11502 SDValue SclrAmt = Amt->getOperand(0);
11503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11504 uint64_t ShiftAmt = C->getZExtValue();
11505
Craig Toppered2e13d2012-01-22 19:15:14 +000011506 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011507 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011508 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11509 if (Op.getOpcode() == ISD::SHL)
11510 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11511 DAG.getConstant(ShiftAmt, MVT::i32));
11512 if (Op.getOpcode() == ISD::SRL)
11513 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11514 DAG.getConstant(ShiftAmt, MVT::i32));
11515 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11516 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11517 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011518 }
11519
Craig Toppered2e13d2012-01-22 19:15:14 +000011520 if (VT == MVT::v16i8) {
11521 if (Op.getOpcode() == ISD::SHL) {
11522 // Make a large shift.
11523 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11524 DAG.getConstant(ShiftAmt, MVT::i32));
11525 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11526 // Zero out the rightmost bits.
11527 SmallVector<SDValue, 16> V(16,
11528 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11529 MVT::i8));
11530 return DAG.getNode(ISD::AND, dl, VT, SHL,
11531 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011532 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011533 if (Op.getOpcode() == ISD::SRL) {
11534 // Make a large shift.
11535 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11536 DAG.getConstant(ShiftAmt, MVT::i32));
11537 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11538 // Zero out the leftmost bits.
11539 SmallVector<SDValue, 16> V(16,
11540 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11541 MVT::i8));
11542 return DAG.getNode(ISD::AND, dl, VT, SRL,
11543 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11544 }
11545 if (Op.getOpcode() == ISD::SRA) {
11546 if (ShiftAmt == 7) {
11547 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011548 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011549 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011550 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011551
Craig Toppered2e13d2012-01-22 19:15:14 +000011552 // R s>> a === ((R u>> a) ^ m) - m
11553 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11554 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11555 MVT::i8));
11556 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11557 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11558 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11559 return Res;
11560 }
Craig Topper731dfd02012-04-23 03:42:40 +000011561 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011562 }
Craig Topper46154eb2011-11-11 07:39:23 +000011563
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011564 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011565 if (Op.getOpcode() == ISD::SHL) {
11566 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011567 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11568 DAG.getConstant(ShiftAmt, MVT::i32));
11569 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011570 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011571 SmallVector<SDValue, 32> V(32,
11572 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11573 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011574 return DAG.getNode(ISD::AND, dl, VT, SHL,
11575 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011576 }
Craig Topper0d86d462011-11-20 00:12:05 +000011577 if (Op.getOpcode() == ISD::SRL) {
11578 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011579 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11580 DAG.getConstant(ShiftAmt, MVT::i32));
11581 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011582 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011583 SmallVector<SDValue, 32> V(32,
11584 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11585 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011586 return DAG.getNode(ISD::AND, dl, VT, SRL,
11587 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11588 }
11589 if (Op.getOpcode() == ISD::SRA) {
11590 if (ShiftAmt == 7) {
11591 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011592 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011593 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011594 }
11595
11596 // R s>> a === ((R u>> a) ^ m) - m
11597 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11598 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11599 MVT::i8));
11600 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11601 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11602 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11603 return Res;
11604 }
Craig Topper731dfd02012-04-23 03:42:40 +000011605 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011606 }
Nadav Rotem43012222011-05-11 08:12:09 +000011607 }
11608 }
11609
Michael Liao42317cc2013-03-20 02:33:21 +000011610 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11611 if (!Subtarget->is64Bit() &&
11612 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11613 Amt.getOpcode() == ISD::BITCAST &&
11614 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11615 Amt = Amt.getOperand(0);
11616 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11617 VT.getVectorNumElements();
11618 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11619 uint64_t ShiftAmt = 0;
11620 for (unsigned i = 0; i != Ratio; ++i) {
11621 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11622 if (C == 0)
11623 return SDValue();
11624 // 6 == Log2(64)
11625 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11626 }
11627 // Check remaining shift amounts.
11628 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11629 uint64_t ShAmt = 0;
11630 for (unsigned j = 0; j != Ratio; ++j) {
11631 ConstantSDNode *C =
11632 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
11633 if (C == 0)
11634 return SDValue();
11635 // 6 == Log2(64)
11636 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
11637 }
11638 if (ShAmt != ShiftAmt)
11639 return SDValue();
11640 }
11641 switch (Op.getOpcode()) {
11642 default:
11643 llvm_unreachable("Unknown shift opcode!");
11644 case ISD::SHL:
11645 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11646 DAG.getConstant(ShiftAmt, MVT::i32));
11647 case ISD::SRL:
11648 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11649 DAG.getConstant(ShiftAmt, MVT::i32));
11650 case ISD::SRA:
11651 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11652 DAG.getConstant(ShiftAmt, MVT::i32));
11653 }
11654 }
11655
11656 return SDValue();
11657}
11658
11659static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
11660 const X86Subtarget* Subtarget) {
11661 EVT VT = Op.getValueType();
11662 DebugLoc dl = Op.getDebugLoc();
11663 SDValue R = Op.getOperand(0);
11664 SDValue Amt = Op.getOperand(1);
11665
11666 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
11667 VT == MVT::v4i32 || VT == MVT::v8i16 ||
11668 (Subtarget->hasInt256() &&
11669 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
11670 VT == MVT::v8i32 || VT == MVT::v16i16))) {
11671 SDValue BaseShAmt;
11672 EVT EltVT = VT.getVectorElementType();
11673
11674 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11675 unsigned NumElts = VT.getVectorNumElements();
11676 unsigned i, j;
11677 for (i = 0; i != NumElts; ++i) {
11678 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
11679 continue;
11680 break;
11681 }
11682 for (j = i; j != NumElts; ++j) {
11683 SDValue Arg = Amt.getOperand(j);
11684 if (Arg.getOpcode() == ISD::UNDEF) continue;
11685 if (Arg != Amt.getOperand(i))
11686 break;
11687 }
11688 if (i != NumElts && j == NumElts)
11689 BaseShAmt = Amt.getOperand(i);
11690 } else {
11691 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11692 Amt = Amt.getOperand(0);
11693 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
11694 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
11695 SDValue InVec = Amt.getOperand(0);
11696 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11697 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11698 unsigned i = 0;
11699 for (; i != NumElts; ++i) {
11700 SDValue Arg = InVec.getOperand(i);
11701 if (Arg.getOpcode() == ISD::UNDEF) continue;
11702 BaseShAmt = Arg;
11703 break;
11704 }
11705 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11706 if (ConstantSDNode *C =
11707 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
11708 unsigned SplatIdx =
11709 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
11710 if (C->getZExtValue() == SplatIdx)
11711 BaseShAmt = InVec.getOperand(1);
11712 }
11713 }
11714 if (BaseShAmt.getNode() == 0)
11715 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
11716 DAG.getIntPtrConstant(0));
11717 }
11718 }
11719
11720 if (BaseShAmt.getNode()) {
11721 if (EltVT.bitsGT(MVT::i32))
11722 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
11723 else if (EltVT.bitsLT(MVT::i32))
11724 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
11725
11726 switch (Op.getOpcode()) {
11727 default:
11728 llvm_unreachable("Unknown shift opcode!");
11729 case ISD::SHL:
11730 switch (VT.getSimpleVT().SimpleTy) {
11731 default: return SDValue();
11732 case MVT::v2i64:
11733 case MVT::v4i32:
11734 case MVT::v8i16:
11735 case MVT::v4i64:
11736 case MVT::v8i32:
11737 case MVT::v16i16:
11738 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
11739 }
11740 case ISD::SRA:
11741 switch (VT.getSimpleVT().SimpleTy) {
11742 default: return SDValue();
11743 case MVT::v4i32:
11744 case MVT::v8i16:
11745 case MVT::v8i32:
11746 case MVT::v16i16:
11747 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
11748 }
11749 case ISD::SRL:
11750 switch (VT.getSimpleVT().SimpleTy) {
11751 default: return SDValue();
11752 case MVT::v2i64:
11753 case MVT::v4i32:
11754 case MVT::v8i16:
11755 case MVT::v4i64:
11756 case MVT::v8i32:
11757 case MVT::v16i16:
11758 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
11759 }
11760 }
11761 }
11762 }
11763
11764 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11765 if (!Subtarget->is64Bit() &&
11766 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11767 Amt.getOpcode() == ISD::BITCAST &&
11768 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11769 Amt = Amt.getOperand(0);
11770 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11771 VT.getVectorNumElements();
11772 std::vector<SDValue> Vals(Ratio);
11773 for (unsigned i = 0; i != Ratio; ++i)
11774 Vals[i] = Amt.getOperand(i);
11775 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11776 for (unsigned j = 0; j != Ratio; ++j)
11777 if (Vals[j] != Amt.getOperand(i + j))
11778 return SDValue();
11779 }
11780 switch (Op.getOpcode()) {
11781 default:
11782 llvm_unreachable("Unknown shift opcode!");
11783 case ISD::SHL:
11784 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
11785 case ISD::SRL:
11786 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
11787 case ISD::SRA:
11788 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
11789 }
11790 }
11791
Michael Liao4b7ab122013-03-20 02:20:36 +000011792 return SDValue();
11793}
11794
11795SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11796
11797 EVT VT = Op.getValueType();
11798 DebugLoc dl = Op.getDebugLoc();
11799 SDValue R = Op.getOperand(0);
11800 SDValue Amt = Op.getOperand(1);
11801 SDValue V;
11802
11803 if (!Subtarget->hasSSE2())
11804 return SDValue();
11805
11806 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11807 if (V.getNode())
11808 return V;
11809
Michael Liao42317cc2013-03-20 02:33:21 +000011810 V = LowerScalarVariableShift(Op, DAG, Subtarget);
11811 if (V.getNode())
11812 return V;
11813
Michael Liao5c5f1902013-03-20 02:28:20 +000011814 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
11815 if (Subtarget->hasInt256()) {
11816 if (Op.getOpcode() == ISD::SRL &&
11817 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11818 VT == MVT::v4i64 || VT == MVT::v8i32))
11819 return Op;
11820 if (Op.getOpcode() == ISD::SHL &&
11821 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11822 VT == MVT::v4i64 || VT == MVT::v8i32))
11823 return Op;
11824 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
11825 return Op;
11826 }
11827
Nadav Rotem43012222011-05-11 08:12:09 +000011828 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011829 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011830 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011831
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011832 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011833 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011834 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11835 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11836 }
Nadav Rotem43012222011-05-11 08:12:09 +000011837 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011838 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011839
Nate Begeman51409212010-07-28 00:21:48 +000011840 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011841 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011842 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011843
Lang Hames8b99c1e2011-12-17 01:08:46 +000011844 // Turn 'a' into a mask suitable for VSELECT
11845 SDValue VSelM = DAG.getConstant(0x80, VT);
11846 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011847 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011848
Lang Hames8b99c1e2011-12-17 01:08:46 +000011849 SDValue CM1 = DAG.getConstant(0x0f, VT);
11850 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011851
Lang Hames8b99c1e2011-12-17 01:08:46 +000011852 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11853 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011854 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11855 DAG.getConstant(4, MVT::i32), DAG);
11856 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011857 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11858
Nate Begeman51409212010-07-28 00:21:48 +000011859 // a += a
11860 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011861 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011862 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011863
Lang Hames8b99c1e2011-12-17 01:08:46 +000011864 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11865 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011866 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11867 DAG.getConstant(2, MVT::i32), DAG);
11868 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011869 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11870
Nate Begeman51409212010-07-28 00:21:48 +000011871 // a += a
11872 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011873 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011874 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011875
Lang Hames8b99c1e2011-12-17 01:08:46 +000011876 // return VSELECT(r, r+r, a);
11877 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011878 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011879 return R;
11880 }
Craig Topper46154eb2011-11-11 07:39:23 +000011881
11882 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011883 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011884 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011885 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11886 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11887
11888 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011889 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11890 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011891
11892 // Recreate the shift amount vectors
11893 SDValue Amt1, Amt2;
11894 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11895 // Constant shift amount
11896 SmallVector<SDValue, 4> Amt1Csts;
11897 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011898 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011899 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011900 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011901 Amt2Csts.push_back(Amt->getOperand(i));
11902
11903 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11904 &Amt1Csts[0], NumElems/2);
11905 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11906 &Amt2Csts[0], NumElems/2);
11907 } else {
11908 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011909 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11910 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011911 }
11912
11913 // Issue new vector shifts for the smaller types
11914 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11915 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11916
11917 // Concatenate the result back
11918 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11919 }
11920
Nate Begeman51409212010-07-28 00:21:48 +000011921 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011922}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011923
Craig Topper55b24052012-09-11 06:15:32 +000011924static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011925 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11926 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011927 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11928 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011929 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011930 SDValue LHS = N->getOperand(0);
11931 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011932 unsigned BaseOp = 0;
11933 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011934 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011935 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011936 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011937 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011938 // A subtract of one will be selected as a INC. Note that INC doesn't
11939 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011940 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11941 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011942 BaseOp = X86ISD::INC;
11943 Cond = X86::COND_O;
11944 break;
11945 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011946 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011947 Cond = X86::COND_O;
11948 break;
11949 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011950 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011951 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011952 break;
11953 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011954 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11955 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011956 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11957 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011958 BaseOp = X86ISD::DEC;
11959 Cond = X86::COND_O;
11960 break;
11961 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011962 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011963 Cond = X86::COND_O;
11964 break;
11965 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011966 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011967 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011968 break;
11969 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011970 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011971 Cond = X86::COND_O;
11972 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011973 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11974 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11975 MVT::i32);
11976 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011977
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011978 SDValue SetCC =
11979 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11980 DAG.getConstant(X86::COND_O, MVT::i32),
11981 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011982
Dan Gohman6e5fda22011-07-22 18:45:15 +000011983 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011984 }
Bill Wendling74c37652008-12-09 22:08:41 +000011985 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011986
Bill Wendling61edeb52008-12-02 01:06:39 +000011987 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011988 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011989 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011990
Bill Wendling61edeb52008-12-02 01:06:39 +000011991 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011992 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11993 DAG.getConstant(Cond, MVT::i32),
11994 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011995
Dan Gohman6e5fda22011-07-22 18:45:15 +000011996 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011997}
11998
Chad Rosier30450e82011-12-22 22:35:21 +000011999SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12000 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012001 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000012002 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12003 EVT VT = Op.getValueType();
12004
Craig Toppered2e13d2012-01-22 19:15:14 +000012005 if (!Subtarget->hasSSE2() || !VT.isVector())
12006 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012007
Craig Toppered2e13d2012-01-22 19:15:14 +000012008 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12009 ExtraVT.getScalarType().getSizeInBits();
12010 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12011
12012 switch (VT.getSimpleVT().SimpleTy) {
12013 default: return SDValue();
12014 case MVT::v8i32:
12015 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012016 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012017 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012018 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012019 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012020 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012021
Craig Toppered2e13d2012-01-22 19:15:14 +000012022 // Extract the LHS vectors
12023 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012024 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12025 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012026
Craig Toppered2e13d2012-01-22 19:15:14 +000012027 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12028 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012029
Craig Toppered2e13d2012-01-22 19:15:14 +000012030 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012031 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012032 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12033 ExtraNumElems/2);
12034 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012035
Craig Toppered2e13d2012-01-22 19:15:14 +000012036 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12037 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012038
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012039 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012040 }
12041 // fall through
12042 case MVT::v4i32:
12043 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012044 // (sext (vzext x)) -> (vsext x)
12045 SDValue Op0 = Op.getOperand(0);
12046 SDValue Op00 = Op0.getOperand(0);
12047 SDValue Tmp1;
12048 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12049 if (Op0.getOpcode() == ISD::BITCAST &&
12050 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12051 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12052 if (Tmp1.getNode()) {
12053 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12054 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12055 "This optimization is invalid without a VZEXT.");
12056 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12057 }
12058
12059 // If the above didn't work, then just use Shift-Left + Shift-Right.
12060 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012061 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012062 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012063 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012064}
12065
Craig Topper55b24052012-09-11 06:15:32 +000012066static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
12067 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000012068 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012069
Eric Christopher77ed1352011-07-08 00:04:56 +000012070 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
12071 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012072 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000012073 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000012074 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000012075 SDValue Ops[] = {
12076 DAG.getRegister(X86::ESP, MVT::i32), // Base
12077 DAG.getTargetConstant(1, MVT::i8), // Scale
12078 DAG.getRegister(0, MVT::i32), // Index
12079 DAG.getTargetConstant(0, MVT::i32), // Disp
12080 DAG.getRegister(0, MVT::i32), // Segment.
12081 Zero,
12082 Chain
12083 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000012084 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000012085 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12086 array_lengthof(Ops));
12087 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000012088 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012089
Eric Christopher9a9d2752010-07-22 02:48:34 +000012090 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000012091 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000012092 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012093
Chris Lattner132929a2010-08-14 17:26:09 +000012094 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
12095 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
12096 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
12097 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012098
Chris Lattner132929a2010-08-14 17:26:09 +000012099 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
12100 if (!Op1 && !Op2 && !Op3 && Op4)
12101 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012102
Chris Lattner132929a2010-08-14 17:26:09 +000012103 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
12104 if (Op1 && !Op2 && !Op3 && !Op4)
12105 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012106
12107 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000012108 // (MFENCE)>;
12109 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000012110}
12111
Craig Topper55b24052012-09-11 06:15:32 +000012112static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12113 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000012114 DebugLoc dl = Op.getDebugLoc();
12115 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12116 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12117 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12118 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12119
12120 // The only fence that needs an instruction is a sequentially-consistent
12121 // cross-thread fence.
12122 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12123 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12124 // no-sse2). There isn't any reason to disable it if the target processor
12125 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012126 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012127 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12128
12129 SDValue Chain = Op.getOperand(0);
12130 SDValue Zero = DAG.getConstant(0, MVT::i32);
12131 SDValue Ops[] = {
12132 DAG.getRegister(X86::ESP, MVT::i32), // Base
12133 DAG.getTargetConstant(1, MVT::i8), // Scale
12134 DAG.getRegister(0, MVT::i32), // Index
12135 DAG.getTargetConstant(0, MVT::i32), // Disp
12136 DAG.getRegister(0, MVT::i32), // Segment.
12137 Zero,
12138 Chain
12139 };
12140 SDNode *Res =
12141 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12142 array_lengthof(Ops));
12143 return SDValue(Res, 0);
12144 }
12145
12146 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12147 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12148}
12149
Craig Topper55b24052012-09-11 06:15:32 +000012150static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12151 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012152 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012153 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012154 unsigned Reg = 0;
12155 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012156 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012157 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012158 case MVT::i8: Reg = X86::AL; size = 1; break;
12159 case MVT::i16: Reg = X86::AX; size = 2; break;
12160 case MVT::i32: Reg = X86::EAX; size = 4; break;
12161 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012162 assert(Subtarget->is64Bit() && "Node not type legal!");
12163 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012164 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012165 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012166 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012167 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012168 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012169 Op.getOperand(1),
12170 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012171 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012172 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012173 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012174 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12175 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
12176 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012177 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012178 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012179 return cpOut;
12180}
12181
Craig Topper55b24052012-09-11 06:15:32 +000012182static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12183 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012184 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012185 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012186 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000012187 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012188 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012189 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12190 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012191 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012192 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12193 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012194 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012195 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012196 rdx.getValue(1)
12197 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000012198 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012199}
12200
Craig Topper55b24052012-09-11 06:15:32 +000012201SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012202 EVT SrcVT = Op.getOperand(0).getValueType();
12203 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012204 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012205 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012206 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012207 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012208 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012209 // i64 <=> MMX conversions are Legal.
12210 if (SrcVT==MVT::i64 && DstVT.isVector())
12211 return Op;
12212 if (DstVT==MVT::i64 && SrcVT.isVector())
12213 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012214 // MMX <=> MMX conversions are Legal.
12215 if (SrcVT.isVector() && DstVT.isVector())
12216 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012217 // All other conversions need to be expanded.
12218 return SDValue();
12219}
Chris Lattner5b856542010-12-20 00:59:46 +000012220
Craig Topper55b24052012-09-11 06:15:32 +000012221static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012222 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012223 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012224 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012225 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012226 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012227 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012228 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012229 Node->getOperand(0),
12230 Node->getOperand(1), negOp,
12231 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012232 cast<AtomicSDNode>(Node)->getAlignment(),
12233 cast<AtomicSDNode>(Node)->getOrdering(),
12234 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012235}
12236
Eli Friedman327236c2011-08-24 20:50:09 +000012237static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12238 SDNode *Node = Op.getNode();
12239 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012240 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012241
12242 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012243 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12244 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12245 // (The only way to get a 16-byte store is cmpxchg16b)
12246 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12247 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12248 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012249 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12250 cast<AtomicSDNode>(Node)->getMemoryVT(),
12251 Node->getOperand(0),
12252 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012253 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012254 cast<AtomicSDNode>(Node)->getOrdering(),
12255 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012256 return Swap.getValue(1);
12257 }
12258 // Other atomic stores have a simple pattern.
12259 return Op;
12260}
12261
Chris Lattner5b856542010-12-20 00:59:46 +000012262static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12263 EVT VT = Op.getNode()->getValueType(0);
12264
12265 // Let legalize expand this if it isn't a legal type yet.
12266 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12267 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012268
Chris Lattner5b856542010-12-20 00:59:46 +000012269 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012270
Chris Lattner5b856542010-12-20 00:59:46 +000012271 unsigned Opc;
12272 bool ExtraOp = false;
12273 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012274 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012275 case ISD::ADDC: Opc = X86ISD::ADD; break;
12276 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12277 case ISD::SUBC: Opc = X86ISD::SUB; break;
12278 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12279 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012280
Chris Lattner5b856542010-12-20 00:59:46 +000012281 if (!ExtraOp)
12282 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12283 Op.getOperand(1));
12284 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12285 Op.getOperand(1), Op.getOperand(2));
12286}
12287
Evan Cheng8688a582013-01-29 02:32:37 +000012288SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012289 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012290
Evan Cheng8688a582013-01-29 02:32:37 +000012291 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12292 // which returns the values in two XMM registers.
12293 DebugLoc dl = Op.getDebugLoc();
12294 SDValue Arg = Op.getOperand(0);
12295 EVT ArgVT = Arg.getValueType();
12296 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012297
Evan Cheng8688a582013-01-29 02:32:37 +000012298 ArgListTy Args;
12299 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012300
Evan Cheng8688a582013-01-29 02:32:37 +000012301 Entry.Node = Arg;
12302 Entry.Ty = ArgTy;
12303 Entry.isSExt = false;
12304 Entry.isZExt = false;
12305 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012306
12307 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12308 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12309 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012310 const char *LibcallName = (ArgVT == MVT::f64)
12311 ? "__sincos_stret" : "__sincosf_stret";
12312 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012313
Evan Cheng8688a582013-01-29 02:32:37 +000012314 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12315 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012316 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12317 false, false, false, false, 0,
12318 CallingConv::C, /*isTaillCall=*/false,
12319 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12320 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012321 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012322 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012323}
12324
Evan Cheng0db9fe62006-04-25 20:13:52 +000012325/// LowerOperation - Provide custom lowering hooks for some operations.
12326///
Dan Gohmand858e902010-04-17 15:26:15 +000012327SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012328 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012329 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012330 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012331 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12332 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12333 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012334 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012335 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012336 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012337 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012338 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12339 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12340 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012341 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12342 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012343 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12344 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12345 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012346 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012347 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012348 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012349 case ISD::SHL_PARTS:
12350 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012351 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012352 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012353 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012354 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012355 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12356 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12357 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012358 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012359 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012360 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012361 case ISD::FABS: return LowerFABS(Op, DAG);
12362 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012363 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012364 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012365 case ISD::SETCC: return LowerSETCC(Op, DAG);
12366 case ISD::SELECT: return LowerSELECT(Op, DAG);
12367 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012368 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012369 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012370 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012371 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012372 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012373 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012374 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12375 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012376 case ISD::FRAME_TO_ARGS_OFFSET:
12377 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012378 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012379 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012380 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12381 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012382 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12383 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012384 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012385 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012386 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012387 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012388 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012389 case ISD::SRA:
12390 case ISD::SRL:
12391 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012392 case ISD::SADDO:
12393 case ISD::UADDO:
12394 case ISD::SSUBO:
12395 case ISD::USUBO:
12396 case ISD::SMULO:
12397 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012398 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012399 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012400 case ISD::ADDC:
12401 case ISD::ADDE:
12402 case ISD::SUBC:
12403 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012404 case ISD::ADD: return LowerADD(Op, DAG);
12405 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012406 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012407 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012408 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012409}
12410
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012411static void ReplaceATOMIC_LOAD(SDNode *Node,
12412 SmallVectorImpl<SDValue> &Results,
12413 SelectionDAG &DAG) {
12414 DebugLoc dl = Node->getDebugLoc();
12415 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12416
12417 // Convert wide load -> cmpxchg8b/cmpxchg16b
12418 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12419 // (The only way to get a 16-byte load is cmpxchg16b)
12420 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012421 SDValue Zero = DAG.getConstant(0, VT);
12422 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012423 Node->getOperand(0),
12424 Node->getOperand(1), Zero, Zero,
12425 cast<AtomicSDNode>(Node)->getMemOperand(),
12426 cast<AtomicSDNode>(Node)->getOrdering(),
12427 cast<AtomicSDNode>(Node)->getSynchScope());
12428 Results.push_back(Swap.getValue(0));
12429 Results.push_back(Swap.getValue(1));
12430}
12431
Craig Topperc0878702012-08-17 06:55:11 +000012432static void
Duncan Sands1607f052008-12-01 11:39:25 +000012433ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012434 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012435 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012436 assert (Node->getValueType(0) == MVT::i64 &&
12437 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012438
12439 SDValue Chain = Node->getOperand(0);
12440 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012441 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012442 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012443 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012444 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012445 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012446 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012447 SDValue Result =
12448 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12449 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012450 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012451 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012452 Results.push_back(Result.getValue(2));
12453}
12454
Duncan Sands126d9072008-07-04 11:47:58 +000012455/// ReplaceNodeResults - Replace a node with an illegal result type
12456/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012457void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12458 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012459 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012460 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012461 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012462 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012463 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012464 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012465 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012466 case ISD::ADDC:
12467 case ISD::ADDE:
12468 case ISD::SUBC:
12469 case ISD::SUBE:
12470 // We don't want to expand or promote these.
12471 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012472 case ISD::FP_TO_SINT:
12473 case ISD::FP_TO_UINT: {
12474 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12475
12476 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12477 return;
12478
Eli Friedman948e95a2009-05-23 09:59:16 +000012479 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012480 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012481 SDValue FIST = Vals.first, StackSlot = Vals.second;
12482 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012483 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012484 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012485 if (StackSlot.getNode() != 0)
12486 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12487 MachinePointerInfo(),
12488 false, false, false, 0));
12489 else
12490 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012491 }
12492 return;
12493 }
Michael Liao991b6a22012-10-24 04:09:32 +000012494 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012495 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12496 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012497 N->getValueType(0) != MVT::v2f32)
12498 return;
12499 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12500 N->getOperand(0));
12501 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12502 MVT::f64);
12503 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12504 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12505 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12506 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12507 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12508 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12509 return;
12510 }
Michael Liao44c2d612012-10-10 16:53:28 +000012511 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012512 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12513 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012514 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12515 Results.push_back(V);
12516 return;
12517 }
Duncan Sands1607f052008-12-01 11:39:25 +000012518 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012519 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012520 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012521 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012522 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012523 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012524 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012525 eax.getValue(2));
12526 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12527 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012528 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012529 Results.push_back(edx.getValue(1));
12530 return;
12531 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012532 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012533 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012534 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012535 bool Regs64bit = T == MVT::i128;
12536 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012537 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012538 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12539 DAG.getConstant(0, HalfT));
12540 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12541 DAG.getConstant(1, HalfT));
12542 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12543 Regs64bit ? X86::RAX : X86::EAX,
12544 cpInL, SDValue());
12545 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12546 Regs64bit ? X86::RDX : X86::EDX,
12547 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012548 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012549 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12550 DAG.getConstant(0, HalfT));
12551 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12552 DAG.getConstant(1, HalfT));
12553 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12554 Regs64bit ? X86::RBX : X86::EBX,
12555 swapInL, cpInH.getValue(1));
12556 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012557 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012558 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012559 SDValue Ops[] = { swapInH.getValue(0),
12560 N->getOperand(1),
12561 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012562 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012563 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012564 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12565 X86ISD::LCMPXCHG8_DAG;
12566 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012567 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012568 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12569 Regs64bit ? X86::RAX : X86::EAX,
12570 HalfT, Result.getValue(1));
12571 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12572 Regs64bit ? X86::RDX : X86::EDX,
12573 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012574 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012575 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012576 Results.push_back(cpOutH.getValue(1));
12577 return;
12578 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012579 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012580 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012581 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012582 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012583 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012584 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012585 case ISD::ATOMIC_LOAD_MAX:
12586 case ISD::ATOMIC_LOAD_MIN:
12587 case ISD::ATOMIC_LOAD_UMAX:
12588 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012589 case ISD::ATOMIC_SWAP: {
12590 unsigned Opc;
12591 switch (N->getOpcode()) {
12592 default: llvm_unreachable("Unexpected opcode");
12593 case ISD::ATOMIC_LOAD_ADD:
12594 Opc = X86ISD::ATOMADD64_DAG;
12595 break;
12596 case ISD::ATOMIC_LOAD_AND:
12597 Opc = X86ISD::ATOMAND64_DAG;
12598 break;
12599 case ISD::ATOMIC_LOAD_NAND:
12600 Opc = X86ISD::ATOMNAND64_DAG;
12601 break;
12602 case ISD::ATOMIC_LOAD_OR:
12603 Opc = X86ISD::ATOMOR64_DAG;
12604 break;
12605 case ISD::ATOMIC_LOAD_SUB:
12606 Opc = X86ISD::ATOMSUB64_DAG;
12607 break;
12608 case ISD::ATOMIC_LOAD_XOR:
12609 Opc = X86ISD::ATOMXOR64_DAG;
12610 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012611 case ISD::ATOMIC_LOAD_MAX:
12612 Opc = X86ISD::ATOMMAX64_DAG;
12613 break;
12614 case ISD::ATOMIC_LOAD_MIN:
12615 Opc = X86ISD::ATOMMIN64_DAG;
12616 break;
12617 case ISD::ATOMIC_LOAD_UMAX:
12618 Opc = X86ISD::ATOMUMAX64_DAG;
12619 break;
12620 case ISD::ATOMIC_LOAD_UMIN:
12621 Opc = X86ISD::ATOMUMIN64_DAG;
12622 break;
Craig Topperc0878702012-08-17 06:55:11 +000012623 case ISD::ATOMIC_SWAP:
12624 Opc = X86ISD::ATOMSWAP64_DAG;
12625 break;
12626 }
12627 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012628 return;
Craig Topperc0878702012-08-17 06:55:11 +000012629 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012630 case ISD::ATOMIC_LOAD:
12631 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012632 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012633}
12634
Evan Cheng72261582005-12-20 06:22:03 +000012635const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12636 switch (Opcode) {
12637 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012638 case X86ISD::BSF: return "X86ISD::BSF";
12639 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012640 case X86ISD::SHLD: return "X86ISD::SHLD";
12641 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012642 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012643 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012644 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012645 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012646 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012647 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012648 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12649 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12650 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012651 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012652 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012653 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012654 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012655 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012656 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012657 case X86ISD::COMI: return "X86ISD::COMI";
12658 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012659 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012660 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012661 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12662 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012663 case X86ISD::CMOV: return "X86ISD::CMOV";
12664 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012665 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012666 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12667 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012668 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012669 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012670 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012671 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012672 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012673 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12674 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012675 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012676 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012677 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012678 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012679 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012680 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012681 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012682 case X86ISD::HADD: return "X86ISD::HADD";
12683 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012684 case X86ISD::FHADD: return "X86ISD::FHADD";
12685 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012686 case X86ISD::UMAX: return "X86ISD::UMAX";
12687 case X86ISD::UMIN: return "X86ISD::UMIN";
12688 case X86ISD::SMAX: return "X86ISD::SMAX";
12689 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012690 case X86ISD::FMAX: return "X86ISD::FMAX";
12691 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012692 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12693 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012694 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12695 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012696 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012697 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012698 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012699 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12700 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012701 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012702 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012703 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012704 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012705 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12706 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012707 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12708 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12709 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12710 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12711 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12712 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012713 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012714 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012715 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012716 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12717 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012718 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012719 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012720 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12721 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012722 case X86ISD::VSHL: return "X86ISD::VSHL";
12723 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012724 case X86ISD::VSRA: return "X86ISD::VSRA";
12725 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12726 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12727 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012728 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012729 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12730 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012731 case X86ISD::ADD: return "X86ISD::ADD";
12732 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012733 case X86ISD::ADC: return "X86ISD::ADC";
12734 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012735 case X86ISD::SMUL: return "X86ISD::SMUL";
12736 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012737 case X86ISD::INC: return "X86ISD::INC";
12738 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012739 case X86ISD::OR: return "X86ISD::OR";
12740 case X86ISD::XOR: return "X86ISD::XOR";
12741 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012742 case X86ISD::BLSI: return "X86ISD::BLSI";
12743 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12744 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012745 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012746 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012747 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012748 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012749 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12750 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012751 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012752 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012753 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012754 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012755 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012756 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12757 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012758 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12759 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12760 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012761 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12762 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012763 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12764 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012765 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012766 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012767 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012768 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12769 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012770 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012771 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012772 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012773 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012774 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012775 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012776 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012777 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012778 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012779 case X86ISD::FMADD: return "X86ISD::FMADD";
12780 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12781 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12782 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12783 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12784 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012785 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12786 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000012787 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000012788 }
12789}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012790
Chris Lattnerc9addb72007-03-30 23:15:24 +000012791// isLegalAddressingMode - Return true if the addressing mode represented
12792// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012793bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012794 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012795 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012796 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012797 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012798
Chris Lattnerc9addb72007-03-30 23:15:24 +000012799 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012800 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012801 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012802
Chris Lattnerc9addb72007-03-30 23:15:24 +000012803 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012804 unsigned GVFlags =
12805 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012806
Chris Lattnerdfed4132009-07-10 07:38:24 +000012807 // If a reference to this global requires an extra load, we can't fold it.
12808 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012809 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012810
Chris Lattnerdfed4132009-07-10 07:38:24 +000012811 // If BaseGV requires a register for the PIC base, we cannot also have a
12812 // BaseReg specified.
12813 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012814 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012815
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012816 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012817 if ((M != CodeModel::Small || R != Reloc::Static) &&
12818 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012819 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012820 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012821
Chris Lattnerc9addb72007-03-30 23:15:24 +000012822 switch (AM.Scale) {
12823 case 0:
12824 case 1:
12825 case 2:
12826 case 4:
12827 case 8:
12828 // These scales always work.
12829 break;
12830 case 3:
12831 case 5:
12832 case 9:
12833 // These scales are formed with basereg+scalereg. Only accept if there is
12834 // no basereg yet.
12835 if (AM.HasBaseReg)
12836 return false;
12837 break;
12838 default: // Other stuff never works.
12839 return false;
12840 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012841
Chris Lattnerc9addb72007-03-30 23:15:24 +000012842 return true;
12843}
12844
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012845bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012846 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012847 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012848 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12849 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012850 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012851}
12852
Evan Cheng70e10d32012-07-17 06:53:39 +000012853bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012854 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012855}
12856
12857bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012858 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012859 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012860}
12861
Owen Andersone50ed302009-08-10 22:56:29 +000012862bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012863 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012864 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012865 unsigned NumBits1 = VT1.getSizeInBits();
12866 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012867 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012868}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012869
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012870bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012871 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012872 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012873}
12874
Owen Andersone50ed302009-08-10 22:56:29 +000012875bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012876 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012877 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012878}
12879
Evan Cheng2766a472012-12-06 19:13:27 +000012880bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12881 EVT VT1 = Val.getValueType();
12882 if (isZExtFree(VT1, VT2))
12883 return true;
12884
12885 if (Val.getOpcode() != ISD::LOAD)
12886 return false;
12887
12888 if (!VT1.isSimple() || !VT1.isInteger() ||
12889 !VT2.isSimple() || !VT2.isInteger())
12890 return false;
12891
12892 switch (VT1.getSimpleVT().SimpleTy) {
12893 default: break;
12894 case MVT::i8:
12895 case MVT::i16:
12896 case MVT::i32:
12897 // X86 has 8, 16, and 32-bit zero-extending loads.
12898 return true;
12899 }
12900
12901 return false;
12902}
12903
Owen Andersone50ed302009-08-10 22:56:29 +000012904bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012905 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012906 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012907}
12908
Evan Cheng60c07e12006-07-05 22:17:51 +000012909/// isShuffleMaskLegal - Targets can use this to indicate that they only
12910/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12911/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12912/// are assumed to be legal.
12913bool
Eric Christopherfd179292009-08-27 18:07:15 +000012914X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012915 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012916 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012917 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012918 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012919
Nate Begemana09008b2009-10-19 02:17:23 +000012920 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012921 return (VT.getVectorNumElements() == 2 ||
12922 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12923 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012924 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012925 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012926 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12927 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012928 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012929 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12930 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12931 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12932 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012933}
12934
Dan Gohman7d8143f2008-04-09 20:09:42 +000012935bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012936X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012937 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012938 unsigned NumElts = VT.getVectorNumElements();
12939 // FIXME: This collection of masks seems suspect.
12940 if (NumElts == 2)
12941 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012942 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012943 return (isMOVLMask(Mask, VT) ||
12944 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012945 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12946 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012947 }
12948 return false;
12949}
12950
12951//===----------------------------------------------------------------------===//
12952// X86 Scheduler Hooks
12953//===----------------------------------------------------------------------===//
12954
Michael Liaobe02a902012-11-08 07:28:54 +000012955/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012956static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12957 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012958 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012959
12960 const BasicBlock *BB = MBB->getBasicBlock();
12961 MachineFunction::iterator I = MBB;
12962 ++I;
12963
12964 // For the v = xbegin(), we generate
12965 //
12966 // thisMBB:
12967 // xbegin sinkMBB
12968 //
12969 // mainMBB:
12970 // eax = -1
12971 //
12972 // sinkMBB:
12973 // v = eax
12974
12975 MachineBasicBlock *thisMBB = MBB;
12976 MachineFunction *MF = MBB->getParent();
12977 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12978 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12979 MF->insert(I, mainMBB);
12980 MF->insert(I, sinkMBB);
12981
12982 // Transfer the remainder of BB and its successor edges to sinkMBB.
12983 sinkMBB->splice(sinkMBB->begin(), MBB,
12984 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12985 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12986
12987 // thisMBB:
12988 // xbegin sinkMBB
12989 // # fallthrough to mainMBB
12990 // # abortion to sinkMBB
12991 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12992 thisMBB->addSuccessor(mainMBB);
12993 thisMBB->addSuccessor(sinkMBB);
12994
12995 // mainMBB:
12996 // EAX = -1
12997 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12998 mainMBB->addSuccessor(sinkMBB);
12999
13000 // sinkMBB:
13001 // EAX is live into the sinkMBB
13002 sinkMBB->addLiveIn(X86::EAX);
13003 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13004 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13005 .addReg(X86::EAX);
13006
13007 MI->eraseFromParent();
13008 return sinkMBB;
13009}
13010
Michael Liaob118a072012-09-20 03:06:15 +000013011// Get CMPXCHG opcode for the specified data type.
13012static unsigned getCmpXChgOpcode(EVT VT) {
13013 switch (VT.getSimpleVT().SimpleTy) {
13014 case MVT::i8: return X86::LCMPXCHG8;
13015 case MVT::i16: return X86::LCMPXCHG16;
13016 case MVT::i32: return X86::LCMPXCHG32;
13017 case MVT::i64: return X86::LCMPXCHG64;
13018 default:
13019 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013020 }
Michael Liaob118a072012-09-20 03:06:15 +000013021 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013022}
13023
Michael Liaob118a072012-09-20 03:06:15 +000013024// Get LOAD opcode for the specified data type.
13025static unsigned getLoadOpcode(EVT VT) {
13026 switch (VT.getSimpleVT().SimpleTy) {
13027 case MVT::i8: return X86::MOV8rm;
13028 case MVT::i16: return X86::MOV16rm;
13029 case MVT::i32: return X86::MOV32rm;
13030 case MVT::i64: return X86::MOV64rm;
13031 default:
13032 break;
13033 }
13034 llvm_unreachable("Invalid operand size!");
13035}
13036
13037// Get opcode of the non-atomic one from the specified atomic instruction.
13038static unsigned getNonAtomicOpcode(unsigned Opc) {
13039 switch (Opc) {
13040 case X86::ATOMAND8: return X86::AND8rr;
13041 case X86::ATOMAND16: return X86::AND16rr;
13042 case X86::ATOMAND32: return X86::AND32rr;
13043 case X86::ATOMAND64: return X86::AND64rr;
13044 case X86::ATOMOR8: return X86::OR8rr;
13045 case X86::ATOMOR16: return X86::OR16rr;
13046 case X86::ATOMOR32: return X86::OR32rr;
13047 case X86::ATOMOR64: return X86::OR64rr;
13048 case X86::ATOMXOR8: return X86::XOR8rr;
13049 case X86::ATOMXOR16: return X86::XOR16rr;
13050 case X86::ATOMXOR32: return X86::XOR32rr;
13051 case X86::ATOMXOR64: return X86::XOR64rr;
13052 }
13053 llvm_unreachable("Unhandled atomic-load-op opcode!");
13054}
13055
13056// Get opcode of the non-atomic one from the specified atomic instruction with
13057// extra opcode.
13058static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13059 unsigned &ExtraOpc) {
13060 switch (Opc) {
13061 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13062 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13063 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13064 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013065 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013066 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13067 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13068 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013069 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013070 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13071 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13072 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013073 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013074 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13075 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13076 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013077 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013078 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13079 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13080 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13081 }
13082 llvm_unreachable("Unhandled atomic-load-op opcode!");
13083}
13084
13085// Get opcode of the non-atomic one from the specified atomic instruction for
13086// 64-bit data type on 32-bit target.
13087static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13088 switch (Opc) {
13089 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13090 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13091 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13092 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13093 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13094 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013095 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13096 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13097 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13098 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013099 }
13100 llvm_unreachable("Unhandled atomic-load-op opcode!");
13101}
13102
13103// Get opcode of the non-atomic one from the specified atomic instruction for
13104// 64-bit data type on 32-bit target with extra opcode.
13105static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13106 unsigned &HiOpc,
13107 unsigned &ExtraOpc) {
13108 switch (Opc) {
13109 case X86::ATOMNAND6432:
13110 ExtraOpc = X86::NOT32r;
13111 HiOpc = X86::AND32rr;
13112 return X86::AND32rr;
13113 }
13114 llvm_unreachable("Unhandled atomic-load-op opcode!");
13115}
13116
13117// Get pseudo CMOV opcode from the specified data type.
13118static unsigned getPseudoCMOVOpc(EVT VT) {
13119 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013120 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013121 case MVT::i16: return X86::CMOV_GR16;
13122 case MVT::i32: return X86::CMOV_GR32;
13123 default:
13124 break;
13125 }
13126 llvm_unreachable("Unknown CMOV opcode!");
13127}
13128
13129// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13130// They will be translated into a spin-loop or compare-exchange loop from
13131//
13132// ...
13133// dst = atomic-fetch-op MI.addr, MI.val
13134// ...
13135//
13136// to
13137//
13138// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013139// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013140// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013141// t4 = phi(t1, t3 / loop)
13142// t2 = OP MI.val, t4
13143// EAX = t4
13144// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13145// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013146// JNE loop
13147// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013148// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013149// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013150MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013151X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13152 MachineBasicBlock *MBB) const {
13153 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13154 DebugLoc DL = MI->getDebugLoc();
13155
13156 MachineFunction *MF = MBB->getParent();
13157 MachineRegisterInfo &MRI = MF->getRegInfo();
13158
13159 const BasicBlock *BB = MBB->getBasicBlock();
13160 MachineFunction::iterator I = MBB;
13161 ++I;
13162
Michael Liao13d08bf2013-01-22 21:47:38 +000013163 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013164 "Unexpected number of operands");
13165
13166 assert(MI->hasOneMemOperand() &&
13167 "Expected atomic-load-op to have one memoperand");
13168
13169 // Memory Reference
13170 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13171 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13172
13173 unsigned DstReg, SrcReg;
13174 unsigned MemOpndSlot;
13175
13176 unsigned CurOp = 0;
13177
13178 DstReg = MI->getOperand(CurOp++).getReg();
13179 MemOpndSlot = CurOp;
13180 CurOp += X86::AddrNumOperands;
13181 SrcReg = MI->getOperand(CurOp++).getReg();
13182
13183 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013184 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013185 unsigned t1 = MRI.createVirtualRegister(RC);
13186 unsigned t2 = MRI.createVirtualRegister(RC);
13187 unsigned t3 = MRI.createVirtualRegister(RC);
13188 unsigned t4 = MRI.createVirtualRegister(RC);
13189 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013190
13191 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13192 unsigned LOADOpc = getLoadOpcode(VT);
13193
13194 // For the atomic load-arith operator, we generate
13195 //
13196 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013197 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013198 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013199 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013200 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013201 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013202 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013203 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013204 // JNE mainMBB
13205 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013206 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013207
13208 MachineBasicBlock *thisMBB = MBB;
13209 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13210 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13211 MF->insert(I, mainMBB);
13212 MF->insert(I, sinkMBB);
13213
13214 MachineInstrBuilder MIB;
13215
13216 // Transfer the remainder of BB and its successor edges to sinkMBB.
13217 sinkMBB->splice(sinkMBB->begin(), MBB,
13218 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13219 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13220
13221 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013222 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13223 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13224 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13225 if (NewMO.isReg())
13226 NewMO.setIsKill(false);
13227 MIB.addOperand(NewMO);
13228 }
13229 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13230 unsigned flags = (*MMOI)->getFlags();
13231 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13232 MachineMemOperand *MMO =
13233 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13234 (*MMOI)->getSize(),
13235 (*MMOI)->getBaseAlignment(),
13236 (*MMOI)->getTBAAInfo(),
13237 (*MMOI)->getRanges());
13238 MIB.addMemOperand(MMO);
13239 }
Michael Liaob118a072012-09-20 03:06:15 +000013240
13241 thisMBB->addSuccessor(mainMBB);
13242
13243 // mainMBB:
13244 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013245
Michael Liaoc537f792013-03-06 00:17:04 +000013246 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013247 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13248 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013249
Michael Liaob118a072012-09-20 03:06:15 +000013250 unsigned Opc = MI->getOpcode();
13251 switch (Opc) {
13252 default:
13253 llvm_unreachable("Unhandled atomic-load-op opcode!");
13254 case X86::ATOMAND8:
13255 case X86::ATOMAND16:
13256 case X86::ATOMAND32:
13257 case X86::ATOMAND64:
13258 case X86::ATOMOR8:
13259 case X86::ATOMOR16:
13260 case X86::ATOMOR32:
13261 case X86::ATOMOR64:
13262 case X86::ATOMXOR8:
13263 case X86::ATOMXOR16:
13264 case X86::ATOMXOR32:
13265 case X86::ATOMXOR64: {
13266 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013267 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13268 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013269 break;
13270 }
13271 case X86::ATOMNAND8:
13272 case X86::ATOMNAND16:
13273 case X86::ATOMNAND32:
13274 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013275 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013276 unsigned NOTOpc;
13277 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013278 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13279 .addReg(t4);
13280 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013281 break;
13282 }
Michael Liao08382492012-09-21 03:00:17 +000013283 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013284 case X86::ATOMMAX16:
13285 case X86::ATOMMAX32:
13286 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013287 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013288 case X86::ATOMMIN16:
13289 case X86::ATOMMIN32:
13290 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013291 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013292 case X86::ATOMUMAX16:
13293 case X86::ATOMUMAX32:
13294 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013295 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013296 case X86::ATOMUMIN16:
13297 case X86::ATOMUMIN32:
13298 case X86::ATOMUMIN64: {
13299 unsigned CMPOpc;
13300 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13301
13302 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13303 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013304 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013305
13306 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013307 if (VT != MVT::i8) {
13308 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013309 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013310 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013311 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013312 } else {
13313 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013314 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13315 const TargetRegisterClass *RC32 =
13316 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013317 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13318 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013319 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013320
13321 unsigned Undef = MRI.createVirtualRegister(RC32);
13322 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13323
13324 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13325 .addReg(Undef)
13326 .addReg(SrcReg)
13327 .addImm(X86::sub_8bit);
13328 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13329 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013330 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013331 .addImm(X86::sub_8bit);
13332
Michael Liaoc537f792013-03-06 00:17:04 +000013333 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013334 .addReg(SrcReg32)
13335 .addReg(AccReg32);
13336
Michael Liaoc537f792013-03-06 00:17:04 +000013337 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13338 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013339 }
Michael Liaob118a072012-09-20 03:06:15 +000013340 } else {
13341 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013342 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013343 "Invalid atomic-load-op transformation!");
13344 unsigned SelOpc = getPseudoCMOVOpc(VT);
13345 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13346 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013347 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13348 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013349 .addImm(CC);
13350 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013351 // Replace the original PHI node as mainMBB is changed after CMOV
13352 // lowering.
13353 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13354 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13355 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013356 }
13357 break;
13358 }
13359 }
13360
Michael Liaoc537f792013-03-06 00:17:04 +000013361 // Copy PhyReg back from virtual register.
13362 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13363 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013364
13365 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013366 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13367 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13368 if (NewMO.isReg())
13369 NewMO.setIsKill(false);
13370 MIB.addOperand(NewMO);
13371 }
13372 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013373 MIB.setMemRefs(MMOBegin, MMOEnd);
13374
Michael Liaoc537f792013-03-06 00:17:04 +000013375 // Copy PhyReg back to virtual register.
13376 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13377 .addReg(PhyReg);
13378
Michael Liaob118a072012-09-20 03:06:15 +000013379 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13380
13381 mainMBB->addSuccessor(origMainMBB);
13382 mainMBB->addSuccessor(sinkMBB);
13383
13384 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013385 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13386 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013387 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013388
13389 MI->eraseFromParent();
13390 return sinkMBB;
13391}
13392
13393// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13394// instructions. They will be translated into a spin-loop or compare-exchange
13395// loop from
13396//
13397// ...
13398// dst = atomic-fetch-op MI.addr, MI.val
13399// ...
13400//
13401// to
13402//
13403// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013404// t1L = LOAD [MI.addr + 0]
13405// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013406// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013407// t4L = phi(t1L, t3L / loop)
13408// t4H = phi(t1H, t3H / loop)
13409// t2L = OP MI.val.lo, t4L
13410// t2H = OP MI.val.hi, t4H
13411// EAX = t4L
13412// EDX = t4H
13413// EBX = t2L
13414// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013415// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013416// t3L = EAX
13417// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013418// JNE loop
13419// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013420// dstL = t3L
13421// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013422// ...
13423MachineBasicBlock *
13424X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13425 MachineBasicBlock *MBB) const {
13426 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13427 DebugLoc DL = MI->getDebugLoc();
13428
13429 MachineFunction *MF = MBB->getParent();
13430 MachineRegisterInfo &MRI = MF->getRegInfo();
13431
13432 const BasicBlock *BB = MBB->getBasicBlock();
13433 MachineFunction::iterator I = MBB;
13434 ++I;
13435
Michael Liao13d08bf2013-01-22 21:47:38 +000013436 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013437 "Unexpected number of operands");
13438
13439 assert(MI->hasOneMemOperand() &&
13440 "Expected atomic-load-op32 to have one memoperand");
13441
13442 // Memory Reference
13443 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13444 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13445
13446 unsigned DstLoReg, DstHiReg;
13447 unsigned SrcLoReg, SrcHiReg;
13448 unsigned MemOpndSlot;
13449
13450 unsigned CurOp = 0;
13451
13452 DstLoReg = MI->getOperand(CurOp++).getReg();
13453 DstHiReg = MI->getOperand(CurOp++).getReg();
13454 MemOpndSlot = CurOp;
13455 CurOp += X86::AddrNumOperands;
13456 SrcLoReg = MI->getOperand(CurOp++).getReg();
13457 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013458
Craig Topperc9099502012-04-20 06:31:50 +000013459 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013460 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013461
Michael Liaoc537f792013-03-06 00:17:04 +000013462 unsigned t1L = MRI.createVirtualRegister(RC);
13463 unsigned t1H = MRI.createVirtualRegister(RC);
13464 unsigned t2L = MRI.createVirtualRegister(RC);
13465 unsigned t2H = MRI.createVirtualRegister(RC);
13466 unsigned t3L = MRI.createVirtualRegister(RC);
13467 unsigned t3H = MRI.createVirtualRegister(RC);
13468 unsigned t4L = MRI.createVirtualRegister(RC);
13469 unsigned t4H = MRI.createVirtualRegister(RC);
13470
Michael Liaob118a072012-09-20 03:06:15 +000013471 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13472 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013473
Michael Liaob118a072012-09-20 03:06:15 +000013474 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013475 //
Michael Liaob118a072012-09-20 03:06:15 +000013476 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013477 // t1L = LOAD [MI.addr + 0]
13478 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013479 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013480 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13481 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13482 // t2L = OP MI.val.lo, t4L
13483 // t2H = OP MI.val.hi, t4H
13484 // EBX = t2L
13485 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013486 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013487 // t3L = EAX
13488 // t3H = EDX
13489 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013490 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013491 // dstL = t3L
13492 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013493
Mon P Wang63307c32008-05-05 19:05:59 +000013494 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013495 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13496 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13497 MF->insert(I, mainMBB);
13498 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013499
Michael Liaob118a072012-09-20 03:06:15 +000013500 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013501
Michael Liaob118a072012-09-20 03:06:15 +000013502 // Transfer the remainder of BB and its successor edges to sinkMBB.
13503 sinkMBB->splice(sinkMBB->begin(), MBB,
13504 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13505 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013506
Michael Liaob118a072012-09-20 03:06:15 +000013507 // thisMBB:
13508 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013509 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013510 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013511 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13512 if (NewMO.isReg())
13513 NewMO.setIsKill(false);
13514 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013515 }
Michael Liaoc537f792013-03-06 00:17:04 +000013516 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13517 unsigned flags = (*MMOI)->getFlags();
13518 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13519 MachineMemOperand *MMO =
13520 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13521 (*MMOI)->getSize(),
13522 (*MMOI)->getBaseAlignment(),
13523 (*MMOI)->getTBAAInfo(),
13524 (*MMOI)->getRanges());
13525 MIB.addMemOperand(MMO);
13526 };
13527 MachineInstr *LowMI = MIB;
13528
13529 // Hi
13530 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13531 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13532 if (i == X86::AddrDisp) {
13533 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13534 } else {
13535 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13536 if (NewMO.isReg())
13537 NewMO.setIsKill(false);
13538 MIB.addOperand(NewMO);
13539 }
13540 }
13541 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013542
Michael Liaob118a072012-09-20 03:06:15 +000013543 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013544
Michael Liaob118a072012-09-20 03:06:15 +000013545 // mainMBB:
13546 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013547
Michael Liaoc537f792013-03-06 00:17:04 +000013548 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013549 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13550 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13551 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13552 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013553
Michael Liaob118a072012-09-20 03:06:15 +000013554 unsigned Opc = MI->getOpcode();
13555 switch (Opc) {
13556 default:
13557 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13558 case X86::ATOMAND6432:
13559 case X86::ATOMOR6432:
13560 case X86::ATOMXOR6432:
13561 case X86::ATOMADD6432:
13562 case X86::ATOMSUB6432: {
13563 unsigned HiOpc;
13564 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013565 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13566 .addReg(SrcLoReg);
13567 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13568 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013569 break;
13570 }
13571 case X86::ATOMNAND6432: {
13572 unsigned HiOpc, NOTOpc;
13573 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013574 unsigned TmpL = MRI.createVirtualRegister(RC);
13575 unsigned TmpH = MRI.createVirtualRegister(RC);
13576 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13577 .addReg(t4L);
13578 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13579 .addReg(t4H);
13580 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13581 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013582 break;
13583 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013584 case X86::ATOMMAX6432:
13585 case X86::ATOMMIN6432:
13586 case X86::ATOMUMAX6432:
13587 case X86::ATOMUMIN6432: {
13588 unsigned HiOpc;
13589 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13590 unsigned cL = MRI.createVirtualRegister(RC8);
13591 unsigned cH = MRI.createVirtualRegister(RC8);
13592 unsigned cL32 = MRI.createVirtualRegister(RC);
13593 unsigned cH32 = MRI.createVirtualRegister(RC);
13594 unsigned cc = MRI.createVirtualRegister(RC);
13595 // cl := cmp src_lo, lo
13596 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013597 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013598 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13599 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13600 // ch := cmp src_hi, hi
13601 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013602 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013603 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13604 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13605 // cc := if (src_hi == hi) ? cl : ch;
13606 if (Subtarget->hasCMov()) {
13607 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13608 .addReg(cH32).addReg(cL32);
13609 } else {
13610 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13611 .addReg(cH32).addReg(cL32)
13612 .addImm(X86::COND_E);
13613 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13614 }
13615 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13616 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013617 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13618 .addReg(SrcLoReg).addReg(t4L);
13619 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13620 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013621 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013622 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13623 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013624 .addImm(X86::COND_NE);
13625 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013626 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13627 // 2nd CMOV lowering.
13628 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013629 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13630 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013631 .addImm(X86::COND_NE);
13632 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013633 // Replace the original PHI node as mainMBB is changed after CMOV
13634 // lowering.
13635 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13636 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13637 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13638 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13639 PhiL->eraseFromParent();
13640 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013641 }
13642 break;
13643 }
Michael Liaob118a072012-09-20 03:06:15 +000013644 case X86::ATOMSWAP6432: {
13645 unsigned HiOpc;
13646 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013647 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13648 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013649 break;
13650 }
13651 }
Mon P Wang63307c32008-05-05 19:05:59 +000013652
Michael Liaob118a072012-09-20 03:06:15 +000013653 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013654 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13655 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013656 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013657 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13658 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013659
Michael Liaob118a072012-09-20 03:06:15 +000013660 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013661 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13662 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13663 if (NewMO.isReg())
13664 NewMO.setIsKill(false);
13665 MIB.addOperand(NewMO);
13666 }
Michael Liaob118a072012-09-20 03:06:15 +000013667 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013668
Michael Liaoc537f792013-03-06 00:17:04 +000013669 // Copy EDX:EAX back to t3H:t3L
13670 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13671 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13672
Michael Liaob118a072012-09-20 03:06:15 +000013673 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013674
Michael Liaob118a072012-09-20 03:06:15 +000013675 mainMBB->addSuccessor(origMainMBB);
13676 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013677
Michael Liaob118a072012-09-20 03:06:15 +000013678 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013679 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13680 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013681 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013682 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13683 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013684 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013685
Michael Liaob118a072012-09-20 03:06:15 +000013686 MI->eraseFromParent();
13687 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013688}
13689
Eric Christopherf83a5de2009-08-27 18:08:16 +000013690// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013691// or XMM0_V32I8 in AVX all of this code can be replaced with that
13692// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013693static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13694 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013695 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013696 switch (MI->getOpcode()) {
13697 default: llvm_unreachable("illegal opcode!");
13698 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13699 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13700 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13701 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13702 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13703 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13704 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13705 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013706 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013707
Craig Topper8aae8dd2012-11-10 08:57:41 +000013708 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013709 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013710
Craig Topper52ea2452012-11-10 09:25:36 +000013711 unsigned NumArgs = MI->getNumOperands();
13712 for (unsigned i = 1; i < NumArgs; ++i) {
13713 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013714 if (!(Op.isReg() && Op.isImplicit()))
13715 MIB.addOperand(Op);
13716 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013717 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013718 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13719
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013720 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013721 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013722 .addReg(X86::XMM0);
13723
Dan Gohman14152b42010-07-06 20:24:04 +000013724 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013725 return BB;
13726}
13727
Craig Topper9c7ae012012-11-10 01:23:36 +000013728// FIXME: Custom handling because TableGen doesn't support multiple implicit
13729// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013730static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13731 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013732 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013733 switch (MI->getOpcode()) {
13734 default: llvm_unreachable("illegal opcode!");
13735 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13736 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13737 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13738 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13739 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13740 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13741 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13742 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013743 }
13744
Craig Topper8aae8dd2012-11-10 08:57:41 +000013745 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013746 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013747
Craig Topper52ea2452012-11-10 09:25:36 +000013748 unsigned NumArgs = MI->getNumOperands(); // remove the results
13749 for (unsigned i = 1; i < NumArgs; ++i) {
13750 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013751 if (!(Op.isReg() && Op.isImplicit()))
13752 MIB.addOperand(Op);
13753 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013754 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013755 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13756
13757 BuildMI(*BB, MI, dl,
13758 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13759 .addReg(X86::ECX);
13760
13761 MI->eraseFromParent();
13762 return BB;
13763}
13764
Craig Topper2da36912012-11-11 22:45:02 +000013765static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13766 const TargetInstrInfo *TII,
13767 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013768 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013769
Eric Christopher228232b2010-11-30 07:20:12 +000013770 // Address into RAX/EAX, other two args into ECX, EDX.
13771 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13772 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13773 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13774 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013775 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013776
Eric Christopher228232b2010-11-30 07:20:12 +000013777 unsigned ValOps = X86::AddrNumOperands;
13778 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13779 .addReg(MI->getOperand(ValOps).getReg());
13780 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13781 .addReg(MI->getOperand(ValOps+1).getReg());
13782
13783 // The instruction doesn't actually take any operands though.
13784 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013785
Eric Christopher228232b2010-11-30 07:20:12 +000013786 MI->eraseFromParent(); // The pseudo is gone now.
13787 return BB;
13788}
13789
13790MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013791X86TargetLowering::EmitVAARG64WithCustomInserter(
13792 MachineInstr *MI,
13793 MachineBasicBlock *MBB) const {
13794 // Emit va_arg instruction on X86-64.
13795
13796 // Operands to this pseudo-instruction:
13797 // 0 ) Output : destination address (reg)
13798 // 1-5) Input : va_list address (addr, i64mem)
13799 // 6 ) ArgSize : Size (in bytes) of vararg type
13800 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13801 // 8 ) Align : Alignment of type
13802 // 9 ) EFLAGS (implicit-def)
13803
13804 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13805 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13806
13807 unsigned DestReg = MI->getOperand(0).getReg();
13808 MachineOperand &Base = MI->getOperand(1);
13809 MachineOperand &Scale = MI->getOperand(2);
13810 MachineOperand &Index = MI->getOperand(3);
13811 MachineOperand &Disp = MI->getOperand(4);
13812 MachineOperand &Segment = MI->getOperand(5);
13813 unsigned ArgSize = MI->getOperand(6).getImm();
13814 unsigned ArgMode = MI->getOperand(7).getImm();
13815 unsigned Align = MI->getOperand(8).getImm();
13816
13817 // Memory Reference
13818 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13819 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13820 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13821
13822 // Machine Information
13823 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13824 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13825 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13826 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13827 DebugLoc DL = MI->getDebugLoc();
13828
13829 // struct va_list {
13830 // i32 gp_offset
13831 // i32 fp_offset
13832 // i64 overflow_area (address)
13833 // i64 reg_save_area (address)
13834 // }
13835 // sizeof(va_list) = 24
13836 // alignment(va_list) = 8
13837
13838 unsigned TotalNumIntRegs = 6;
13839 unsigned TotalNumXMMRegs = 8;
13840 bool UseGPOffset = (ArgMode == 1);
13841 bool UseFPOffset = (ArgMode == 2);
13842 unsigned MaxOffset = TotalNumIntRegs * 8 +
13843 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13844
13845 /* Align ArgSize to a multiple of 8 */
13846 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13847 bool NeedsAlign = (Align > 8);
13848
13849 MachineBasicBlock *thisMBB = MBB;
13850 MachineBasicBlock *overflowMBB;
13851 MachineBasicBlock *offsetMBB;
13852 MachineBasicBlock *endMBB;
13853
13854 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13855 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13856 unsigned OffsetReg = 0;
13857
13858 if (!UseGPOffset && !UseFPOffset) {
13859 // If we only pull from the overflow region, we don't create a branch.
13860 // We don't need to alter control flow.
13861 OffsetDestReg = 0; // unused
13862 OverflowDestReg = DestReg;
13863
13864 offsetMBB = NULL;
13865 overflowMBB = thisMBB;
13866 endMBB = thisMBB;
13867 } else {
13868 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13869 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13870 // If not, pull from overflow_area. (branch to overflowMBB)
13871 //
13872 // thisMBB
13873 // | .
13874 // | .
13875 // offsetMBB overflowMBB
13876 // | .
13877 // | .
13878 // endMBB
13879
13880 // Registers for the PHI in endMBB
13881 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13882 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13883
13884 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13885 MachineFunction *MF = MBB->getParent();
13886 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13887 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13888 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13889
13890 MachineFunction::iterator MBBIter = MBB;
13891 ++MBBIter;
13892
13893 // Insert the new basic blocks
13894 MF->insert(MBBIter, offsetMBB);
13895 MF->insert(MBBIter, overflowMBB);
13896 MF->insert(MBBIter, endMBB);
13897
13898 // Transfer the remainder of MBB and its successor edges to endMBB.
13899 endMBB->splice(endMBB->begin(), thisMBB,
13900 llvm::next(MachineBasicBlock::iterator(MI)),
13901 thisMBB->end());
13902 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13903
13904 // Make offsetMBB and overflowMBB successors of thisMBB
13905 thisMBB->addSuccessor(offsetMBB);
13906 thisMBB->addSuccessor(overflowMBB);
13907
13908 // endMBB is a successor of both offsetMBB and overflowMBB
13909 offsetMBB->addSuccessor(endMBB);
13910 overflowMBB->addSuccessor(endMBB);
13911
13912 // Load the offset value into a register
13913 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13914 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13915 .addOperand(Base)
13916 .addOperand(Scale)
13917 .addOperand(Index)
13918 .addDisp(Disp, UseFPOffset ? 4 : 0)
13919 .addOperand(Segment)
13920 .setMemRefs(MMOBegin, MMOEnd);
13921
13922 // Check if there is enough room left to pull this argument.
13923 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13924 .addReg(OffsetReg)
13925 .addImm(MaxOffset + 8 - ArgSizeA8);
13926
13927 // Branch to "overflowMBB" if offset >= max
13928 // Fall through to "offsetMBB" otherwise
13929 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13930 .addMBB(overflowMBB);
13931 }
13932
13933 // In offsetMBB, emit code to use the reg_save_area.
13934 if (offsetMBB) {
13935 assert(OffsetReg != 0);
13936
13937 // Read the reg_save_area address.
13938 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13939 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13940 .addOperand(Base)
13941 .addOperand(Scale)
13942 .addOperand(Index)
13943 .addDisp(Disp, 16)
13944 .addOperand(Segment)
13945 .setMemRefs(MMOBegin, MMOEnd);
13946
13947 // Zero-extend the offset
13948 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13949 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13950 .addImm(0)
13951 .addReg(OffsetReg)
13952 .addImm(X86::sub_32bit);
13953
13954 // Add the offset to the reg_save_area to get the final address.
13955 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13956 .addReg(OffsetReg64)
13957 .addReg(RegSaveReg);
13958
13959 // Compute the offset for the next argument
13960 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13961 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13962 .addReg(OffsetReg)
13963 .addImm(UseFPOffset ? 16 : 8);
13964
13965 // Store it back into the va_list.
13966 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13967 .addOperand(Base)
13968 .addOperand(Scale)
13969 .addOperand(Index)
13970 .addDisp(Disp, UseFPOffset ? 4 : 0)
13971 .addOperand(Segment)
13972 .addReg(NextOffsetReg)
13973 .setMemRefs(MMOBegin, MMOEnd);
13974
13975 // Jump to endMBB
13976 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13977 .addMBB(endMBB);
13978 }
13979
13980 //
13981 // Emit code to use overflow area
13982 //
13983
13984 // Load the overflow_area address into a register.
13985 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13986 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13987 .addOperand(Base)
13988 .addOperand(Scale)
13989 .addOperand(Index)
13990 .addDisp(Disp, 8)
13991 .addOperand(Segment)
13992 .setMemRefs(MMOBegin, MMOEnd);
13993
13994 // If we need to align it, do so. Otherwise, just copy the address
13995 // to OverflowDestReg.
13996 if (NeedsAlign) {
13997 // Align the overflow address
13998 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13999 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14000
14001 // aligned_addr = (addr + (align-1)) & ~(align-1)
14002 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14003 .addReg(OverflowAddrReg)
14004 .addImm(Align-1);
14005
14006 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14007 .addReg(TmpReg)
14008 .addImm(~(uint64_t)(Align-1));
14009 } else {
14010 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14011 .addReg(OverflowAddrReg);
14012 }
14013
14014 // Compute the next overflow address after this argument.
14015 // (the overflow address should be kept 8-byte aligned)
14016 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14017 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14018 .addReg(OverflowDestReg)
14019 .addImm(ArgSizeA8);
14020
14021 // Store the new overflow address.
14022 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14023 .addOperand(Base)
14024 .addOperand(Scale)
14025 .addOperand(Index)
14026 .addDisp(Disp, 8)
14027 .addOperand(Segment)
14028 .addReg(NextAddrReg)
14029 .setMemRefs(MMOBegin, MMOEnd);
14030
14031 // If we branched, emit the PHI to the front of endMBB.
14032 if (offsetMBB) {
14033 BuildMI(*endMBB, endMBB->begin(), DL,
14034 TII->get(X86::PHI), DestReg)
14035 .addReg(OffsetDestReg).addMBB(offsetMBB)
14036 .addReg(OverflowDestReg).addMBB(overflowMBB);
14037 }
14038
14039 // Erase the pseudo instruction
14040 MI->eraseFromParent();
14041
14042 return endMBB;
14043}
14044
14045MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014046X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14047 MachineInstr *MI,
14048 MachineBasicBlock *MBB) const {
14049 // Emit code to save XMM registers to the stack. The ABI says that the
14050 // number of registers to save is given in %al, so it's theoretically
14051 // possible to do an indirect jump trick to avoid saving all of them,
14052 // however this code takes a simpler approach and just executes all
14053 // of the stores if %al is non-zero. It's less code, and it's probably
14054 // easier on the hardware branch predictor, and stores aren't all that
14055 // expensive anyway.
14056
14057 // Create the new basic blocks. One block contains all the XMM stores,
14058 // and one block is the final destination regardless of whether any
14059 // stores were performed.
14060 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14061 MachineFunction *F = MBB->getParent();
14062 MachineFunction::iterator MBBIter = MBB;
14063 ++MBBIter;
14064 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14065 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14066 F->insert(MBBIter, XMMSaveMBB);
14067 F->insert(MBBIter, EndMBB);
14068
Dan Gohman14152b42010-07-06 20:24:04 +000014069 // Transfer the remainder of MBB and its successor edges to EndMBB.
14070 EndMBB->splice(EndMBB->begin(), MBB,
14071 llvm::next(MachineBasicBlock::iterator(MI)),
14072 MBB->end());
14073 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14074
Dan Gohmand6708ea2009-08-15 01:38:56 +000014075 // The original block will now fall through to the XMM save block.
14076 MBB->addSuccessor(XMMSaveMBB);
14077 // The XMMSaveMBB will fall through to the end block.
14078 XMMSaveMBB->addSuccessor(EndMBB);
14079
14080 // Now add the instructions.
14081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14082 DebugLoc DL = MI->getDebugLoc();
14083
14084 unsigned CountReg = MI->getOperand(0).getReg();
14085 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14086 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14087
14088 if (!Subtarget->isTargetWin64()) {
14089 // If %al is 0, branch around the XMM save block.
14090 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014091 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014092 MBB->addSuccessor(EndMBB);
14093 }
14094
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014095 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014096 // In the XMM save block, save all the XMM argument registers.
14097 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14098 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014099 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014100 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014101 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014102 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014103 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014104 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014105 .addFrameIndex(RegSaveFrameIndex)
14106 .addImm(/*Scale=*/1)
14107 .addReg(/*IndexReg=*/0)
14108 .addImm(/*Disp=*/Offset)
14109 .addReg(/*Segment=*/0)
14110 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014111 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014112 }
14113
Dan Gohman14152b42010-07-06 20:24:04 +000014114 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014115
14116 return EndMBB;
14117}
Mon P Wang63307c32008-05-05 19:05:59 +000014118
Lang Hames6e3f7e42012-02-03 01:13:49 +000014119// The EFLAGS operand of SelectItr might be missing a kill marker
14120// because there were multiple uses of EFLAGS, and ISel didn't know
14121// which to mark. Figure out whether SelectItr should have had a
14122// kill marker, and set it if it should. Returns the correct kill
14123// marker value.
14124static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14125 MachineBasicBlock* BB,
14126 const TargetRegisterInfo* TRI) {
14127 // Scan forward through BB for a use/def of EFLAGS.
14128 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14129 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014130 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014131 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014132 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014133 if (mi.definesRegister(X86::EFLAGS))
14134 break; // Should have kill-flag - update below.
14135 }
14136
14137 // If we hit the end of the block, check whether EFLAGS is live into a
14138 // successor.
14139 if (miI == BB->end()) {
14140 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14141 sEnd = BB->succ_end();
14142 sItr != sEnd; ++sItr) {
14143 MachineBasicBlock* succ = *sItr;
14144 if (succ->isLiveIn(X86::EFLAGS))
14145 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014146 }
14147 }
14148
Lang Hames6e3f7e42012-02-03 01:13:49 +000014149 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14150 // out. SelectMI should have a kill flag on EFLAGS.
14151 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014152 return true;
14153}
14154
Evan Cheng60c07e12006-07-05 22:17:51 +000014155MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014156X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014157 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14159 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014160
Chris Lattner52600972009-09-02 05:57:00 +000014161 // To "insert" a SELECT_CC instruction, we actually have to insert the
14162 // diamond control-flow pattern. The incoming instruction knows the
14163 // destination vreg to set, the condition code register to branch on, the
14164 // true/false values to select between, and a branch opcode to use.
14165 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14166 MachineFunction::iterator It = BB;
14167 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014168
Chris Lattner52600972009-09-02 05:57:00 +000014169 // thisMBB:
14170 // ...
14171 // TrueVal = ...
14172 // cmpTY ccX, r1, r2
14173 // bCC copy1MBB
14174 // fallthrough --> copy0MBB
14175 MachineBasicBlock *thisMBB = BB;
14176 MachineFunction *F = BB->getParent();
14177 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14178 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014179 F->insert(It, copy0MBB);
14180 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014181
Bill Wendling730c07e2010-06-25 20:48:10 +000014182 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14183 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014184 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14185 if (!MI->killsRegister(X86::EFLAGS) &&
14186 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14187 copy0MBB->addLiveIn(X86::EFLAGS);
14188 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014189 }
14190
Dan Gohman14152b42010-07-06 20:24:04 +000014191 // Transfer the remainder of BB and its successor edges to sinkMBB.
14192 sinkMBB->splice(sinkMBB->begin(), BB,
14193 llvm::next(MachineBasicBlock::iterator(MI)),
14194 BB->end());
14195 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14196
14197 // Add the true and fallthrough blocks as its successors.
14198 BB->addSuccessor(copy0MBB);
14199 BB->addSuccessor(sinkMBB);
14200
14201 // Create the conditional branch instruction.
14202 unsigned Opc =
14203 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14204 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14205
Chris Lattner52600972009-09-02 05:57:00 +000014206 // copy0MBB:
14207 // %FalseValue = ...
14208 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014209 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014210
Chris Lattner52600972009-09-02 05:57:00 +000014211 // sinkMBB:
14212 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14213 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014214 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14215 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014216 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14217 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14218
Dan Gohman14152b42010-07-06 20:24:04 +000014219 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014220 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014221}
14222
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014223MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014224X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14225 bool Is64Bit) const {
14226 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14227 DebugLoc DL = MI->getDebugLoc();
14228 MachineFunction *MF = BB->getParent();
14229 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14230
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014231 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014232
14233 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14234 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14235
14236 // BB:
14237 // ... [Till the alloca]
14238 // If stacklet is not large enough, jump to mallocMBB
14239 //
14240 // bumpMBB:
14241 // Allocate by subtracting from RSP
14242 // Jump to continueMBB
14243 //
14244 // mallocMBB:
14245 // Allocate by call to runtime
14246 //
14247 // continueMBB:
14248 // ...
14249 // [rest of original BB]
14250 //
14251
14252 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14253 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14254 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14255
14256 MachineRegisterInfo &MRI = MF->getRegInfo();
14257 const TargetRegisterClass *AddrRegClass =
14258 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14259
14260 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14261 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14262 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014263 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014264 sizeVReg = MI->getOperand(1).getReg(),
14265 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14266
14267 MachineFunction::iterator MBBIter = BB;
14268 ++MBBIter;
14269
14270 MF->insert(MBBIter, bumpMBB);
14271 MF->insert(MBBIter, mallocMBB);
14272 MF->insert(MBBIter, continueMBB);
14273
14274 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14275 (MachineBasicBlock::iterator(MI)), BB->end());
14276 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14277
14278 // Add code to the main basic block to check if the stack limit has been hit,
14279 // and if so, jump to mallocMBB otherwise to bumpMBB.
14280 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014281 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014282 .addReg(tmpSPVReg).addReg(sizeVReg);
14283 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014284 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014285 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014286 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14287
14288 // bumpMBB simply decreases the stack pointer, since we know the current
14289 // stacklet has enough space.
14290 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014291 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014292 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014293 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014294 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14295
14296 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014297 const uint32_t *RegMask =
14298 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014299 if (Is64Bit) {
14300 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14301 .addReg(sizeVReg);
14302 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014303 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014304 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014305 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014306 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014307 } else {
14308 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14309 .addImm(12);
14310 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14311 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014312 .addExternalSymbol("__morestack_allocate_stack_space")
14313 .addRegMask(RegMask)
14314 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014315 }
14316
14317 if (!Is64Bit)
14318 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14319 .addImm(16);
14320
14321 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14322 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14323 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14324
14325 // Set up the CFG correctly.
14326 BB->addSuccessor(bumpMBB);
14327 BB->addSuccessor(mallocMBB);
14328 mallocMBB->addSuccessor(continueMBB);
14329 bumpMBB->addSuccessor(continueMBB);
14330
14331 // Take care of the PHI nodes.
14332 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14333 MI->getOperand(0).getReg())
14334 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14335 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14336
14337 // Delete the original pseudo instruction.
14338 MI->eraseFromParent();
14339
14340 // And we're done.
14341 return continueMBB;
14342}
14343
14344MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014345X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014346 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14348 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014349
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014350 assert(!Subtarget->isTargetEnvMacho());
14351
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014352 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14353 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014354
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014355 if (Subtarget->isTargetWin64()) {
14356 if (Subtarget->isTargetCygMing()) {
14357 // ___chkstk(Mingw64):
14358 // Clobbers R10, R11, RAX and EFLAGS.
14359 // Updates RSP.
14360 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14361 .addExternalSymbol("___chkstk")
14362 .addReg(X86::RAX, RegState::Implicit)
14363 .addReg(X86::RSP, RegState::Implicit)
14364 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14365 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14366 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14367 } else {
14368 // __chkstk(MSVCRT): does not update stack pointer.
14369 // Clobbers R10, R11 and EFLAGS.
14370 // FIXME: RAX(allocated size) might be reused and not killed.
14371 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14372 .addExternalSymbol("__chkstk")
14373 .addReg(X86::RAX, RegState::Implicit)
14374 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14375 // RAX has the offset to subtracted from RSP.
14376 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14377 .addReg(X86::RSP)
14378 .addReg(X86::RAX);
14379 }
14380 } else {
14381 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014382 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14383
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014384 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14385 .addExternalSymbol(StackProbeSymbol)
14386 .addReg(X86::EAX, RegState::Implicit)
14387 .addReg(X86::ESP, RegState::Implicit)
14388 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14389 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14390 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14391 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014392
Dan Gohman14152b42010-07-06 20:24:04 +000014393 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014394 return BB;
14395}
Chris Lattner52600972009-09-02 05:57:00 +000014396
14397MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014398X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14399 MachineBasicBlock *BB) const {
14400 // This is pretty easy. We're taking the value that we received from
14401 // our load from the relocation, sticking it in either RDI (x86-64)
14402 // or EAX and doing an indirect call. The return value will then
14403 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014404 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014405 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014406 DebugLoc DL = MI->getDebugLoc();
14407 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014408
14409 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014410 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014411
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014412 // Get a register mask for the lowered call.
14413 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14414 // proper register mask.
14415 const uint32_t *RegMask =
14416 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014417 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014418 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14419 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014420 .addReg(X86::RIP)
14421 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014422 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014423 MI->getOperand(3).getTargetFlags())
14424 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014425 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014426 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014427 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014428 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014429 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14430 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014431 .addReg(0)
14432 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014433 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014434 MI->getOperand(3).getTargetFlags())
14435 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014436 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014437 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014438 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014439 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014440 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14441 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014442 .addReg(TII->getGlobalBaseReg(F))
14443 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014444 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014445 MI->getOperand(3).getTargetFlags())
14446 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014447 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014448 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014449 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014450 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014451
Dan Gohman14152b42010-07-06 20:24:04 +000014452 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014453 return BB;
14454}
14455
14456MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014457X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14458 MachineBasicBlock *MBB) const {
14459 DebugLoc DL = MI->getDebugLoc();
14460 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14461
14462 MachineFunction *MF = MBB->getParent();
14463 MachineRegisterInfo &MRI = MF->getRegInfo();
14464
14465 const BasicBlock *BB = MBB->getBasicBlock();
14466 MachineFunction::iterator I = MBB;
14467 ++I;
14468
14469 // Memory Reference
14470 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14471 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14472
14473 unsigned DstReg;
14474 unsigned MemOpndSlot = 0;
14475
14476 unsigned CurOp = 0;
14477
14478 DstReg = MI->getOperand(CurOp++).getReg();
14479 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14480 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14481 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14482 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14483
14484 MemOpndSlot = CurOp;
14485
14486 MVT PVT = getPointerTy();
14487 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14488 "Invalid Pointer Size!");
14489
14490 // For v = setjmp(buf), we generate
14491 //
14492 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014493 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014494 // SjLjSetup restoreMBB
14495 //
14496 // mainMBB:
14497 // v_main = 0
14498 //
14499 // sinkMBB:
14500 // v = phi(main, restore)
14501 //
14502 // restoreMBB:
14503 // v_restore = 1
14504
14505 MachineBasicBlock *thisMBB = MBB;
14506 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14507 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14508 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14509 MF->insert(I, mainMBB);
14510 MF->insert(I, sinkMBB);
14511 MF->push_back(restoreMBB);
14512
14513 MachineInstrBuilder MIB;
14514
14515 // Transfer the remainder of BB and its successor edges to sinkMBB.
14516 sinkMBB->splice(sinkMBB->begin(), MBB,
14517 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14518 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14519
14520 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014521 unsigned PtrStoreOpc = 0;
14522 unsigned LabelReg = 0;
14523 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14524 Reloc::Model RM = getTargetMachine().getRelocationModel();
14525 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14526 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014527
Michael Liao281ae5a2012-10-17 02:22:27 +000014528 // Prepare IP either in reg or imm.
14529 if (!UseImmLabel) {
14530 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14531 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14532 LabelReg = MRI.createVirtualRegister(PtrRC);
14533 if (Subtarget->is64Bit()) {
14534 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14535 .addReg(X86::RIP)
14536 .addImm(0)
14537 .addReg(0)
14538 .addMBB(restoreMBB)
14539 .addReg(0);
14540 } else {
14541 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14542 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14543 .addReg(XII->getGlobalBaseReg(MF))
14544 .addImm(0)
14545 .addReg(0)
14546 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14547 .addReg(0);
14548 }
14549 } else
14550 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014551 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014552 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014553 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14554 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014555 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014556 else
14557 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14558 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014559 if (!UseImmLabel)
14560 MIB.addReg(LabelReg);
14561 else
14562 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014563 MIB.setMemRefs(MMOBegin, MMOEnd);
14564 // Setup
14565 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14566 .addMBB(restoreMBB);
14567 MIB.addRegMask(RegInfo->getNoPreservedMask());
14568 thisMBB->addSuccessor(mainMBB);
14569 thisMBB->addSuccessor(restoreMBB);
14570
14571 // mainMBB:
14572 // EAX = 0
14573 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14574 mainMBB->addSuccessor(sinkMBB);
14575
14576 // sinkMBB:
14577 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14578 TII->get(X86::PHI), DstReg)
14579 .addReg(mainDstReg).addMBB(mainMBB)
14580 .addReg(restoreDstReg).addMBB(restoreMBB);
14581
14582 // restoreMBB:
14583 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14584 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14585 restoreMBB->addSuccessor(sinkMBB);
14586
14587 MI->eraseFromParent();
14588 return sinkMBB;
14589}
14590
14591MachineBasicBlock *
14592X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14593 MachineBasicBlock *MBB) const {
14594 DebugLoc DL = MI->getDebugLoc();
14595 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14596
14597 MachineFunction *MF = MBB->getParent();
14598 MachineRegisterInfo &MRI = MF->getRegInfo();
14599
14600 // Memory Reference
14601 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14602 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14603
14604 MVT PVT = getPointerTy();
14605 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14606 "Invalid Pointer Size!");
14607
14608 const TargetRegisterClass *RC =
14609 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14610 unsigned Tmp = MRI.createVirtualRegister(RC);
14611 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14612 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14613 unsigned SP = RegInfo->getStackRegister();
14614
14615 MachineInstrBuilder MIB;
14616
Michael Liao281ae5a2012-10-17 02:22:27 +000014617 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14618 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014619
14620 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14621 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14622
14623 // Reload FP
14624 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14625 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14626 MIB.addOperand(MI->getOperand(i));
14627 MIB.setMemRefs(MMOBegin, MMOEnd);
14628 // Reload IP
14629 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14630 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14631 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014632 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014633 else
14634 MIB.addOperand(MI->getOperand(i));
14635 }
14636 MIB.setMemRefs(MMOBegin, MMOEnd);
14637 // Reload SP
14638 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14639 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14640 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014641 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014642 else
14643 MIB.addOperand(MI->getOperand(i));
14644 }
14645 MIB.setMemRefs(MMOBegin, MMOEnd);
14646 // Jump
14647 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14648
14649 MI->eraseFromParent();
14650 return MBB;
14651}
14652
14653MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014654X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014655 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014656 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014657 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014658 case X86::TAILJMPd64:
14659 case X86::TAILJMPr64:
14660 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014661 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014662 case X86::TCRETURNdi64:
14663 case X86::TCRETURNri64:
14664 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014665 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014666 case X86::WIN_ALLOCA:
14667 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014668 case X86::SEG_ALLOCA_32:
14669 return EmitLoweredSegAlloca(MI, BB, false);
14670 case X86::SEG_ALLOCA_64:
14671 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014672 case X86::TLSCall_32:
14673 case X86::TLSCall_64:
14674 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014675 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014676 case X86::CMOV_FR32:
14677 case X86::CMOV_FR64:
14678 case X86::CMOV_V4F32:
14679 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014680 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014681 case X86::CMOV_V8F32:
14682 case X86::CMOV_V4F64:
14683 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014684 case X86::CMOV_GR16:
14685 case X86::CMOV_GR32:
14686 case X86::CMOV_RFP32:
14687 case X86::CMOV_RFP64:
14688 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014689 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014690
Dale Johannesen849f2142007-07-03 00:53:03 +000014691 case X86::FP32_TO_INT16_IN_MEM:
14692 case X86::FP32_TO_INT32_IN_MEM:
14693 case X86::FP32_TO_INT64_IN_MEM:
14694 case X86::FP64_TO_INT16_IN_MEM:
14695 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014696 case X86::FP64_TO_INT64_IN_MEM:
14697 case X86::FP80_TO_INT16_IN_MEM:
14698 case X86::FP80_TO_INT32_IN_MEM:
14699 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014700 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14701 DebugLoc DL = MI->getDebugLoc();
14702
Evan Cheng60c07e12006-07-05 22:17:51 +000014703 // Change the floating point control register to use "round towards zero"
14704 // mode when truncating to an integer value.
14705 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014706 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014707 addFrameReference(BuildMI(*BB, MI, DL,
14708 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014709
14710 // Load the old value of the high byte of the control word...
14711 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014712 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014713 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014714 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014715
14716 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014717 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014718 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014719
14720 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014721 addFrameReference(BuildMI(*BB, MI, DL,
14722 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014723
14724 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014725 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014726 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014727
14728 // Get the X86 opcode to use.
14729 unsigned Opc;
14730 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014731 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014732 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14733 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14734 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14735 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14736 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14737 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014738 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14739 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14740 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014741 }
14742
14743 X86AddressMode AM;
14744 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014745 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014746 AM.BaseType = X86AddressMode::RegBase;
14747 AM.Base.Reg = Op.getReg();
14748 } else {
14749 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014750 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014751 }
14752 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014753 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014754 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014755 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014756 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014757 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014758 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014759 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014760 AM.GV = Op.getGlobal();
14761 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014762 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014763 }
Dan Gohman14152b42010-07-06 20:24:04 +000014764 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014765 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014766
14767 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014768 addFrameReference(BuildMI(*BB, MI, DL,
14769 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014770
Dan Gohman14152b42010-07-06 20:24:04 +000014771 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014772 return BB;
14773 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014774 // String/text processing lowering.
14775 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014776 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014777 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014778 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014779 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014780 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014781 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014782 case X86::VPCMPESTRM128MEM:
14783 assert(Subtarget->hasSSE42() &&
14784 "Target must have SSE4.2 or AVX features enabled");
14785 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014786
14787 // String/text processing lowering.
14788 case X86::PCMPISTRIREG:
14789 case X86::VPCMPISTRIREG:
14790 case X86::PCMPISTRIMEM:
14791 case X86::VPCMPISTRIMEM:
14792 case X86::PCMPESTRIREG:
14793 case X86::VPCMPESTRIREG:
14794 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014795 case X86::VPCMPESTRIMEM:
14796 assert(Subtarget->hasSSE42() &&
14797 "Target must have SSE4.2 or AVX features enabled");
14798 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014799
Craig Topper8aae8dd2012-11-10 08:57:41 +000014800 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014801 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014802 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014803
Michael Liaobe02a902012-11-08 07:28:54 +000014804 // xbegin
14805 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014806 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014807
Craig Topper8aae8dd2012-11-10 08:57:41 +000014808 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014809 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014810 case X86::ATOMAND16:
14811 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014812 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014813 // Fall through
14814 case X86::ATOMOR8:
14815 case X86::ATOMOR16:
14816 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014817 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014818 // Fall through
14819 case X86::ATOMXOR16:
14820 case X86::ATOMXOR8:
14821 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014822 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014823 // Fall through
14824 case X86::ATOMNAND8:
14825 case X86::ATOMNAND16:
14826 case X86::ATOMNAND32:
14827 case X86::ATOMNAND64:
14828 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014829 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014830 case X86::ATOMMAX16:
14831 case X86::ATOMMAX32:
14832 case X86::ATOMMAX64:
14833 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014834 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014835 case X86::ATOMMIN16:
14836 case X86::ATOMMIN32:
14837 case X86::ATOMMIN64:
14838 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014839 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014840 case X86::ATOMUMAX16:
14841 case X86::ATOMUMAX32:
14842 case X86::ATOMUMAX64:
14843 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014844 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014845 case X86::ATOMUMIN16:
14846 case X86::ATOMUMIN32:
14847 case X86::ATOMUMIN64:
14848 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014849
14850 // This group does 64-bit operations on a 32-bit host.
14851 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014852 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014853 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014854 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014855 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014856 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014857 case X86::ATOMMAX6432:
14858 case X86::ATOMMIN6432:
14859 case X86::ATOMUMAX6432:
14860 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014861 case X86::ATOMSWAP6432:
14862 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014863
Dan Gohmand6708ea2009-08-15 01:38:56 +000014864 case X86::VASTART_SAVE_XMM_REGS:
14865 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014866
14867 case X86::VAARG_64:
14868 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014869
14870 case X86::EH_SjLj_SetJmp32:
14871 case X86::EH_SjLj_SetJmp64:
14872 return emitEHSjLjSetJmp(MI, BB);
14873
14874 case X86::EH_SjLj_LongJmp32:
14875 case X86::EH_SjLj_LongJmp64:
14876 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014877 }
14878}
14879
14880//===----------------------------------------------------------------------===//
14881// X86 Optimization Hooks
14882//===----------------------------------------------------------------------===//
14883
Dan Gohman475871a2008-07-27 21:46:04 +000014884void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014885 APInt &KnownZero,
14886 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014887 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014888 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014889 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014890 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014891 assert((Opc >= ISD::BUILTIN_OP_END ||
14892 Opc == ISD::INTRINSIC_WO_CHAIN ||
14893 Opc == ISD::INTRINSIC_W_CHAIN ||
14894 Opc == ISD::INTRINSIC_VOID) &&
14895 "Should use MaskedValueIsZero if you don't know whether Op"
14896 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014897
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014898 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014899 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014900 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014901 case X86ISD::ADD:
14902 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014903 case X86ISD::ADC:
14904 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014905 case X86ISD::SMUL:
14906 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014907 case X86ISD::INC:
14908 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014909 case X86ISD::OR:
14910 case X86ISD::XOR:
14911 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014912 // These nodes' second result is a boolean.
14913 if (Op.getResNo() == 0)
14914 break;
14915 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014916 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014917 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014918 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014919 case ISD::INTRINSIC_WO_CHAIN: {
14920 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14921 unsigned NumLoBits = 0;
14922 switch (IntId) {
14923 default: break;
14924 case Intrinsic::x86_sse_movmsk_ps:
14925 case Intrinsic::x86_avx_movmsk_ps_256:
14926 case Intrinsic::x86_sse2_movmsk_pd:
14927 case Intrinsic::x86_avx_movmsk_pd_256:
14928 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014929 case Intrinsic::x86_sse2_pmovmskb_128:
14930 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014931 // High bits of movmskp{s|d}, pmovmskb are known zero.
14932 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014933 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014934 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14935 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14936 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14937 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14938 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14939 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014940 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014941 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014942 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014943 break;
14944 }
14945 }
14946 break;
14947 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014948 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014949}
Chris Lattner259e97c2006-01-31 19:43:35 +000014950
Owen Andersonbc146b02010-09-21 20:42:50 +000014951unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14952 unsigned Depth) const {
14953 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14954 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14955 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014956
Owen Andersonbc146b02010-09-21 20:42:50 +000014957 // Fallback case.
14958 return 1;
14959}
14960
Evan Cheng206ee9d2006-07-07 08:33:52 +000014961/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014962/// node is a GlobalAddress + offset.
14963bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014964 const GlobalValue* &GA,
14965 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014966 if (N->getOpcode() == X86ISD::Wrapper) {
14967 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014968 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014969 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014970 return true;
14971 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014972 }
Evan Chengad4196b2008-05-12 19:56:52 +000014973 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014974}
14975
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014976/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14977/// same as extracting the high 128-bit part of 256-bit vector and then
14978/// inserting the result into the low part of a new 256-bit vector
14979static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14980 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014981 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014982
14983 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014984 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014985 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14986 SVOp->getMaskElt(j) >= 0)
14987 return false;
14988
14989 return true;
14990}
14991
14992/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14993/// same as extracting the low 128-bit part of 256-bit vector and then
14994/// inserting the result into the high part of a new 256-bit vector
14995static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14996 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014997 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014998
14999 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015000 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015001 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15002 SVOp->getMaskElt(j) >= 0)
15003 return false;
15004
15005 return true;
15006}
15007
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015008/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15009static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015010 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015011 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015012 DebugLoc dl = N->getDebugLoc();
15013 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15014 SDValue V1 = SVOp->getOperand(0);
15015 SDValue V2 = SVOp->getOperand(1);
15016 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015017 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015018
15019 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15020 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15021 //
15022 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015023 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015024 // V UNDEF BUILD_VECTOR UNDEF
15025 // \ / \ /
15026 // CONCAT_VECTOR CONCAT_VECTOR
15027 // \ /
15028 // \ /
15029 // RESULT: V + zero extended
15030 //
15031 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15032 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15033 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15034 return SDValue();
15035
15036 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15037 return SDValue();
15038
15039 // To match the shuffle mask, the first half of the mask should
15040 // be exactly the first vector, and all the rest a splat with the
15041 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015042 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015043 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15044 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15045 return SDValue();
15046
Chad Rosier3d1161e2012-01-03 21:05:52 +000015047 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15048 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015049 if (Ld->hasNUsesOfValue(1, 0)) {
15050 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15051 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15052 SDValue ResNode =
15053 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
15054 Ld->getMemoryVT(),
15055 Ld->getPointerInfo(),
15056 Ld->getAlignment(),
15057 false/*isVolatile*/, true/*ReadMem*/,
15058 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015059
15060 // Make sure the newly-created LOAD is in the same position as Ld in
15061 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15062 // and update uses of Ld's output chain to use the TokenFactor.
15063 if (Ld->hasAnyUseOfValue(1)) {
15064 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15065 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15066 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15067 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15068 SDValue(ResNode.getNode(), 1));
15069 }
15070
Chad Rosier42726832012-05-07 18:47:44 +000015071 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15072 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015073 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015074
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015075 // Emit a zeroed vector and insert the desired subvector on its
15076 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015077 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015078 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015079 return DCI.CombineTo(N, InsV);
15080 }
15081
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015082 //===--------------------------------------------------------------------===//
15083 // Combine some shuffles into subvector extracts and inserts:
15084 //
15085
15086 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15087 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015088 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15089 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015090 return DCI.CombineTo(N, InsV);
15091 }
15092
15093 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15094 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015095 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15096 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015097 return DCI.CombineTo(N, InsV);
15098 }
15099
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015100 return SDValue();
15101}
15102
15103/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015104static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015105 TargetLowering::DAGCombinerInfo &DCI,
15106 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000015107 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000015108 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015109
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015110 // Don't create instructions with illegal types after legalize types has run.
15111 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15112 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15113 return SDValue();
15114
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015115 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015116 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015117 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015118 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015119
15120 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015121 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015122 return SDValue();
15123
15124 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15125 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15126 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015127 SmallVector<SDValue, 16> Elts;
15128 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015129 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015130
Nate Begemanfdea31a2010-03-24 20:49:50 +000015131 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015132}
Evan Chengd880b972008-05-09 21:53:03 +000015133
Nadav Roteme12bf182013-01-04 17:35:21 +000015134/// PerformTruncateCombine - Converts truncate operation to
15135/// a sequence of vector shuffle operations.
15136/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015137static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15138 TargetLowering::DAGCombinerInfo &DCI,
15139 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015140 return SDValue();
15141}
15142
Craig Topper89f4e662012-03-20 07:17:59 +000015143/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15144/// specific shuffle of a load can be folded into a single element load.
15145/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15146/// shuffles have been customed lowered so we need to handle those here.
15147static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15148 TargetLowering::DAGCombinerInfo &DCI) {
15149 if (DCI.isBeforeLegalizeOps())
15150 return SDValue();
15151
15152 SDValue InVec = N->getOperand(0);
15153 SDValue EltNo = N->getOperand(1);
15154
15155 if (!isa<ConstantSDNode>(EltNo))
15156 return SDValue();
15157
15158 EVT VT = InVec.getValueType();
15159
15160 bool HasShuffleIntoBitcast = false;
15161 if (InVec.getOpcode() == ISD::BITCAST) {
15162 // Don't duplicate a load with other uses.
15163 if (!InVec.hasOneUse())
15164 return SDValue();
15165 EVT BCVT = InVec.getOperand(0).getValueType();
15166 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15167 return SDValue();
15168 InVec = InVec.getOperand(0);
15169 HasShuffleIntoBitcast = true;
15170 }
15171
15172 if (!isTargetShuffle(InVec.getOpcode()))
15173 return SDValue();
15174
15175 // Don't duplicate a load with other uses.
15176 if (!InVec.hasOneUse())
15177 return SDValue();
15178
15179 SmallVector<int, 16> ShuffleMask;
15180 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015181 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15182 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015183 return SDValue();
15184
15185 // Select the input vector, guarding against out of range extract vector.
15186 unsigned NumElems = VT.getVectorNumElements();
15187 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15188 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15189 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15190 : InVec.getOperand(1);
15191
15192 // If inputs to shuffle are the same for both ops, then allow 2 uses
15193 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15194
15195 if (LdNode.getOpcode() == ISD::BITCAST) {
15196 // Don't duplicate a load with other uses.
15197 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15198 return SDValue();
15199
15200 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15201 LdNode = LdNode.getOperand(0);
15202 }
15203
15204 if (!ISD::isNormalLoad(LdNode.getNode()))
15205 return SDValue();
15206
15207 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15208
15209 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15210 return SDValue();
15211
15212 if (HasShuffleIntoBitcast) {
15213 // If there's a bitcast before the shuffle, check if the load type and
15214 // alignment is valid.
15215 unsigned Align = LN0->getAlignment();
15216 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015217 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015218 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15219
15220 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15221 return SDValue();
15222 }
15223
15224 // All checks match so transform back to vector_shuffle so that DAG combiner
15225 // can finish the job
15226 DebugLoc dl = N->getDebugLoc();
15227
15228 // Create shuffle node taking into account the case that its a unary shuffle
15229 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15230 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15231 InVec.getOperand(0), Shuffle,
15232 &ShuffleMask[0]);
15233 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15234 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15235 EltNo);
15236}
15237
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015238/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15239/// generation and convert it from being a bunch of shuffles and extracts
15240/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015241static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015242 TargetLowering::DAGCombinerInfo &DCI) {
15243 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15244 if (NewOp.getNode())
15245 return NewOp;
15246
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015247 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015248 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15249 // from mmx to v2i32 has a single usage.
15250 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15251 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15252 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
15253 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
15254 N->getValueType(0),
15255 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015256
15257 // Only operate on vectors of 4 elements, where the alternative shuffling
15258 // gets to be more expensive.
15259 if (InputVector.getValueType() != MVT::v4i32)
15260 return SDValue();
15261
15262 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15263 // single use which is a sign-extend or zero-extend, and all elements are
15264 // used.
15265 SmallVector<SDNode *, 4> Uses;
15266 unsigned ExtractedElements = 0;
15267 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15268 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15269 if (UI.getUse().getResNo() != InputVector.getResNo())
15270 return SDValue();
15271
15272 SDNode *Extract = *UI;
15273 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15274 return SDValue();
15275
15276 if (Extract->getValueType(0) != MVT::i32)
15277 return SDValue();
15278 if (!Extract->hasOneUse())
15279 return SDValue();
15280 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15281 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15282 return SDValue();
15283 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15284 return SDValue();
15285
15286 // Record which element was extracted.
15287 ExtractedElements |=
15288 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15289
15290 Uses.push_back(Extract);
15291 }
15292
15293 // If not all the elements were used, this may not be worthwhile.
15294 if (ExtractedElements != 15)
15295 return SDValue();
15296
15297 // Ok, we've now decided to do the transformation.
15298 DebugLoc dl = InputVector.getDebugLoc();
15299
15300 // Store the value to a temporary stack slot.
15301 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015302 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15303 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015304
15305 // Replace each use (extract) with a load of the appropriate element.
15306 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15307 UE = Uses.end(); UI != UE; ++UI) {
15308 SDNode *Extract = *UI;
15309
Nadav Rotem86694292011-05-17 08:31:57 +000015310 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015311 SDValue Idx = Extract->getOperand(1);
15312 unsigned EltSize =
15313 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15314 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015315 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015316 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15317
Nadav Rotem86694292011-05-17 08:31:57 +000015318 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015319 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015320
15321 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015322 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015323 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015324 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015325
15326 // Replace the exact with the load.
15327 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15328 }
15329
15330 // The replacement was made in place; don't return anything.
15331 return SDValue();
15332}
15333
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015334/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15335static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15336 SDValue RHS, SelectionDAG &DAG,
15337 const X86Subtarget *Subtarget) {
15338 if (!VT.isVector())
15339 return 0;
15340
15341 switch (VT.getSimpleVT().SimpleTy) {
15342 default: return 0;
15343 case MVT::v32i8:
15344 case MVT::v16i16:
15345 case MVT::v8i32:
15346 if (!Subtarget->hasAVX2())
15347 return 0;
15348 case MVT::v16i8:
15349 case MVT::v8i16:
15350 case MVT::v4i32:
15351 if (!Subtarget->hasSSE2())
15352 return 0;
15353 }
15354
15355 // SSE2 has only a small subset of the operations.
15356 bool hasUnsigned = Subtarget->hasSSE41() ||
15357 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15358 bool hasSigned = Subtarget->hasSSE41() ||
15359 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15360
15361 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15362
15363 // Check for x CC y ? x : y.
15364 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15365 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15366 switch (CC) {
15367 default: break;
15368 case ISD::SETULT:
15369 case ISD::SETULE:
15370 return hasUnsigned ? X86ISD::UMIN : 0;
15371 case ISD::SETUGT:
15372 case ISD::SETUGE:
15373 return hasUnsigned ? X86ISD::UMAX : 0;
15374 case ISD::SETLT:
15375 case ISD::SETLE:
15376 return hasSigned ? X86ISD::SMIN : 0;
15377 case ISD::SETGT:
15378 case ISD::SETGE:
15379 return hasSigned ? X86ISD::SMAX : 0;
15380 }
15381 // Check for x CC y ? y : x -- a min/max with reversed arms.
15382 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15383 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15384 switch (CC) {
15385 default: break;
15386 case ISD::SETULT:
15387 case ISD::SETULE:
15388 return hasUnsigned ? X86ISD::UMAX : 0;
15389 case ISD::SETUGT:
15390 case ISD::SETUGE:
15391 return hasUnsigned ? X86ISD::UMIN : 0;
15392 case ISD::SETLT:
15393 case ISD::SETLE:
15394 return hasSigned ? X86ISD::SMAX : 0;
15395 case ISD::SETGT:
15396 case ISD::SETGE:
15397 return hasSigned ? X86ISD::SMIN : 0;
15398 }
15399 }
15400
15401 return 0;
15402}
15403
Duncan Sands6bcd2192011-09-17 16:49:39 +000015404/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15405/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015406static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015407 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015408 const X86Subtarget *Subtarget) {
15409 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015410 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015411 // Get the LHS/RHS of the select.
15412 SDValue LHS = N->getOperand(1);
15413 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015414 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015415
Dan Gohman670e5392009-09-21 18:03:22 +000015416 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015417 // instructions match the semantics of the common C idiom x<y?x:y but not
15418 // x<=y?x:y, because of how they handle negative zero (which can be
15419 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015420 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15421 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015422 (Subtarget->hasSSE2() ||
15423 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015424 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015425
Chris Lattner47b4ce82009-03-11 05:48:52 +000015426 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015427 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015428 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15429 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015430 switch (CC) {
15431 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015432 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015433 // Converting this to a min would handle NaNs incorrectly, and swapping
15434 // the operands would cause it to handle comparisons between positive
15435 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015436 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015437 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015438 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15439 break;
15440 std::swap(LHS, RHS);
15441 }
Dan Gohman670e5392009-09-21 18:03:22 +000015442 Opcode = X86ISD::FMIN;
15443 break;
15444 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015445 // Converting this to a min would handle comparisons between positive
15446 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015447 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015448 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15449 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015450 Opcode = X86ISD::FMIN;
15451 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015452 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015453 // Converting this to a min would handle both negative zeros and NaNs
15454 // incorrectly, but we can swap the operands to fix both.
15455 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015456 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015457 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015458 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015459 Opcode = X86ISD::FMIN;
15460 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015461
Dan Gohman670e5392009-09-21 18:03:22 +000015462 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015463 // Converting this to a max would handle comparisons between positive
15464 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015465 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015466 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015467 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015468 Opcode = X86ISD::FMAX;
15469 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015470 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015471 // Converting this to a max would handle NaNs incorrectly, and swapping
15472 // the operands would cause it to handle comparisons between positive
15473 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015474 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015475 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015476 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15477 break;
15478 std::swap(LHS, RHS);
15479 }
Dan Gohman670e5392009-09-21 18:03:22 +000015480 Opcode = X86ISD::FMAX;
15481 break;
15482 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015483 // Converting this to a max would handle both negative zeros and NaNs
15484 // incorrectly, but we can swap the operands to fix both.
15485 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015486 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015487 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015488 case ISD::SETGE:
15489 Opcode = X86ISD::FMAX;
15490 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015491 }
Dan Gohman670e5392009-09-21 18:03:22 +000015492 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015493 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15494 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015495 switch (CC) {
15496 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015497 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015498 // Converting this to a min would handle comparisons between positive
15499 // and negative zero incorrectly, and swapping the operands would
15500 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015501 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015502 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015503 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015504 break;
15505 std::swap(LHS, RHS);
15506 }
Dan Gohman670e5392009-09-21 18:03:22 +000015507 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015508 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015509 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015510 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015511 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015512 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15513 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015514 Opcode = X86ISD::FMIN;
15515 break;
15516 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015517 // Converting this to a min would handle both negative zeros and NaNs
15518 // incorrectly, but we can swap the operands to fix both.
15519 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015520 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015521 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015522 case ISD::SETGE:
15523 Opcode = X86ISD::FMIN;
15524 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015525
Dan Gohman670e5392009-09-21 18:03:22 +000015526 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015527 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015528 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015529 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015530 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015531 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015532 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015533 // Converting this to a max would handle comparisons between positive
15534 // and negative zero incorrectly, and swapping the operands would
15535 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015536 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015537 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015538 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015539 break;
15540 std::swap(LHS, RHS);
15541 }
Dan Gohman670e5392009-09-21 18:03:22 +000015542 Opcode = X86ISD::FMAX;
15543 break;
15544 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015545 // Converting this to a max would handle both negative zeros and NaNs
15546 // incorrectly, but we can swap the operands to fix both.
15547 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015548 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015549 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015550 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015551 Opcode = X86ISD::FMAX;
15552 break;
15553 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015554 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015555
Chris Lattner47b4ce82009-03-11 05:48:52 +000015556 if (Opcode)
15557 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015558 }
Eric Christopherfd179292009-08-27 18:07:15 +000015559
Chris Lattnerd1980a52009-03-12 06:52:53 +000015560 // If this is a select between two integer constants, try to do some
15561 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015562 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15563 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015564 // Don't do this for crazy integer types.
15565 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15566 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015567 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015568 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015569
Chris Lattnercee56e72009-03-13 05:53:31 +000015570 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015571 // Efficiently invertible.
15572 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15573 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15574 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15575 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015576 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015577 }
Eric Christopherfd179292009-08-27 18:07:15 +000015578
Chris Lattnerd1980a52009-03-12 06:52:53 +000015579 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015580 if (FalseC->getAPIntValue() == 0 &&
15581 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015582 if (NeedsCondInvert) // Invert the condition if needed.
15583 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15584 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015585
Chris Lattnerd1980a52009-03-12 06:52:53 +000015586 // Zero extend the condition if needed.
15587 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015588
Chris Lattnercee56e72009-03-13 05:53:31 +000015589 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015590 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015591 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015592 }
Eric Christopherfd179292009-08-27 18:07:15 +000015593
Chris Lattner97a29a52009-03-13 05:22:11 +000015594 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015595 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015596 if (NeedsCondInvert) // Invert the condition if needed.
15597 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15598 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015599
Chris Lattner97a29a52009-03-13 05:22:11 +000015600 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015601 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15602 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015603 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015604 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015605 }
Eric Christopherfd179292009-08-27 18:07:15 +000015606
Chris Lattnercee56e72009-03-13 05:53:31 +000015607 // Optimize cases that will turn into an LEA instruction. This requires
15608 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015609 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015610 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015611 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015612
Chris Lattnercee56e72009-03-13 05:53:31 +000015613 bool isFastMultiplier = false;
15614 if (Diff < 10) {
15615 switch ((unsigned char)Diff) {
15616 default: break;
15617 case 1: // result = add base, cond
15618 case 2: // result = lea base( , cond*2)
15619 case 3: // result = lea base(cond, cond*2)
15620 case 4: // result = lea base( , cond*4)
15621 case 5: // result = lea base(cond, cond*4)
15622 case 8: // result = lea base( , cond*8)
15623 case 9: // result = lea base(cond, cond*8)
15624 isFastMultiplier = true;
15625 break;
15626 }
15627 }
Eric Christopherfd179292009-08-27 18:07:15 +000015628
Chris Lattnercee56e72009-03-13 05:53:31 +000015629 if (isFastMultiplier) {
15630 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15631 if (NeedsCondInvert) // Invert the condition if needed.
15632 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15633 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015634
Chris Lattnercee56e72009-03-13 05:53:31 +000015635 // Zero extend the condition if needed.
15636 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15637 Cond);
15638 // Scale the condition by the difference.
15639 if (Diff != 1)
15640 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15641 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015642
Chris Lattnercee56e72009-03-13 05:53:31 +000015643 // Add the base if non-zero.
15644 if (FalseC->getAPIntValue() != 0)
15645 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15646 SDValue(FalseC, 0));
15647 return Cond;
15648 }
Eric Christopherfd179292009-08-27 18:07:15 +000015649 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015650 }
15651 }
Eric Christopherfd179292009-08-27 18:07:15 +000015652
Evan Cheng56f582d2012-01-04 01:41:39 +000015653 // Canonicalize max and min:
15654 // (x > y) ? x : y -> (x >= y) ? x : y
15655 // (x < y) ? x : y -> (x <= y) ? x : y
15656 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15657 // the need for an extra compare
15658 // against zero. e.g.
15659 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15660 // subl %esi, %edi
15661 // testl %edi, %edi
15662 // movl $0, %eax
15663 // cmovgl %edi, %eax
15664 // =>
15665 // xorl %eax, %eax
15666 // subl %esi, $edi
15667 // cmovsl %eax, %edi
15668 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15669 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15670 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15671 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15672 switch (CC) {
15673 default: break;
15674 case ISD::SETLT:
15675 case ISD::SETGT: {
15676 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15677 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15678 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15679 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15680 }
15681 }
15682 }
15683
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015684 // Match VSELECTs into subs with unsigned saturation.
15685 if (!DCI.isBeforeLegalize() &&
15686 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15687 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15688 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15689 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15690 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15691
15692 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15693 // left side invert the predicate to simplify logic below.
15694 SDValue Other;
15695 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15696 Other = RHS;
15697 CC = ISD::getSetCCInverse(CC, true);
15698 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15699 Other = LHS;
15700 }
15701
15702 if (Other.getNode() && Other->getNumOperands() == 2 &&
15703 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15704 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15705 SDValue CondRHS = Cond->getOperand(1);
15706
15707 // Look for a general sub with unsigned saturation first.
15708 // x >= y ? x-y : 0 --> subus x, y
15709 // x > y ? x-y : 0 --> subus x, y
15710 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15711 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15712 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15713
15714 // If the RHS is a constant we have to reverse the const canonicalization.
15715 // x > C-1 ? x+-C : 0 --> subus x, C
15716 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15717 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15718 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015719 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015720 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015721 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015722 }
15723
15724 // Another special case: If C was a sign bit, the sub has been
15725 // canonicalized into a xor.
15726 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15727 // it's safe to decanonicalize the xor?
15728 // x s< 0 ? x^C : 0 --> subus x, C
15729 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15730 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15731 isSplatVector(OpRHS.getNode())) {
15732 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15733 if (A.isSignBit())
15734 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15735 }
15736 }
15737 }
15738
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015739 // Try to match a min/max vector operation.
15740 if (!DCI.isBeforeLegalize() &&
15741 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15742 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15743 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15744
Nadav Rotemcc616562012-01-15 19:27:55 +000015745 // If we know that this node is legal then we know that it is going to be
15746 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15747 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15748 // to simplify previous instructions.
15749 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15750 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015751 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015752 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015753
15754 // Don't optimize vector selects that map to mask-registers.
15755 if (BitWidth == 1)
15756 return SDValue();
15757
Nadav Rotemcc616562012-01-15 19:27:55 +000015758 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15759 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15760
15761 APInt KnownZero, KnownOne;
15762 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15763 DCI.isBeforeLegalizeOps());
15764 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15765 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15766 DCI.CommitTargetLoweringOpt(TLO);
15767 }
15768
Dan Gohman475871a2008-07-27 21:46:04 +000015769 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015770}
15771
Michael Liao2a33cec2012-08-10 19:58:13 +000015772// Check whether a boolean test is testing a boolean value generated by
15773// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15774// code.
15775//
15776// Simplify the following patterns:
15777// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15778// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15779// to (Op EFLAGS Cond)
15780//
15781// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15782// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15783// to (Op EFLAGS !Cond)
15784//
15785// where Op could be BRCOND or CMOV.
15786//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015787static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015788 // Quit if not CMP and SUB with its value result used.
15789 if (Cmp.getOpcode() != X86ISD::CMP &&
15790 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15791 return SDValue();
15792
15793 // Quit if not used as a boolean value.
15794 if (CC != X86::COND_E && CC != X86::COND_NE)
15795 return SDValue();
15796
15797 // Check CMP operands. One of them should be 0 or 1 and the other should be
15798 // an SetCC or extended from it.
15799 SDValue Op1 = Cmp.getOperand(0);
15800 SDValue Op2 = Cmp.getOperand(1);
15801
15802 SDValue SetCC;
15803 const ConstantSDNode* C = 0;
15804 bool needOppositeCond = (CC == X86::COND_E);
15805
15806 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15807 SetCC = Op2;
15808 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15809 SetCC = Op1;
15810 else // Quit if all operands are not constants.
15811 return SDValue();
15812
15813 if (C->getZExtValue() == 1)
15814 needOppositeCond = !needOppositeCond;
15815 else if (C->getZExtValue() != 0)
15816 // Quit if the constant is neither 0 or 1.
15817 return SDValue();
15818
15819 // Skip 'zext' node.
15820 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15821 SetCC = SetCC.getOperand(0);
15822
Michael Liao7fdc66b2012-09-10 16:36:16 +000015823 switch (SetCC.getOpcode()) {
15824 case X86ISD::SETCC:
15825 // Set the condition code or opposite one if necessary.
15826 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15827 if (needOppositeCond)
15828 CC = X86::GetOppositeBranchCondition(CC);
15829 return SetCC.getOperand(1);
15830 case X86ISD::CMOV: {
15831 // Check whether false/true value has canonical one, i.e. 0 or 1.
15832 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15833 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15834 // Quit if true value is not a constant.
15835 if (!TVal)
15836 return SDValue();
15837 // Quit if false value is not a constant.
15838 if (!FVal) {
15839 // A special case for rdrand, where 0 is set if false cond is found.
15840 SDValue Op = SetCC.getOperand(0);
15841 if (Op.getOpcode() != X86ISD::RDRAND)
15842 return SDValue();
15843 }
15844 // Quit if false value is not the constant 0 or 1.
15845 bool FValIsFalse = true;
15846 if (FVal && FVal->getZExtValue() != 0) {
15847 if (FVal->getZExtValue() != 1)
15848 return SDValue();
15849 // If FVal is 1, opposite cond is needed.
15850 needOppositeCond = !needOppositeCond;
15851 FValIsFalse = false;
15852 }
15853 // Quit if TVal is not the constant opposite of FVal.
15854 if (FValIsFalse && TVal->getZExtValue() != 1)
15855 return SDValue();
15856 if (!FValIsFalse && TVal->getZExtValue() != 0)
15857 return SDValue();
15858 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15859 if (needOppositeCond)
15860 CC = X86::GetOppositeBranchCondition(CC);
15861 return SetCC.getOperand(3);
15862 }
15863 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015864
Michael Liao7fdc66b2012-09-10 16:36:16 +000015865 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015866}
15867
Chris Lattnerd1980a52009-03-12 06:52:53 +000015868/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15869static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015870 TargetLowering::DAGCombinerInfo &DCI,
15871 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015872 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015873
Chris Lattnerd1980a52009-03-12 06:52:53 +000015874 // If the flag operand isn't dead, don't touch this CMOV.
15875 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15876 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015877
Evan Chengb5a55d92011-05-24 01:48:22 +000015878 SDValue FalseOp = N->getOperand(0);
15879 SDValue TrueOp = N->getOperand(1);
15880 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15881 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015882
Evan Chengb5a55d92011-05-24 01:48:22 +000015883 if (CC == X86::COND_E || CC == X86::COND_NE) {
15884 switch (Cond.getOpcode()) {
15885 default: break;
15886 case X86ISD::BSR:
15887 case X86ISD::BSF:
15888 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15889 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15890 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15891 }
15892 }
15893
Michael Liao2a33cec2012-08-10 19:58:13 +000015894 SDValue Flags;
15895
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015896 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015897 if (Flags.getNode() &&
15898 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015899 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015900 SDValue Ops[] = { FalseOp, TrueOp,
15901 DAG.getConstant(CC, MVT::i8), Flags };
15902 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15903 Ops, array_lengthof(Ops));
15904 }
15905
Chris Lattnerd1980a52009-03-12 06:52:53 +000015906 // If this is a select between two integer constants, try to do some
15907 // optimizations. Note that the operands are ordered the opposite of SELECT
15908 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015909 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15910 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015911 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15912 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015913 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15914 CC = X86::GetOppositeBranchCondition(CC);
15915 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015916 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015917 }
Eric Christopherfd179292009-08-27 18:07:15 +000015918
Chris Lattnerd1980a52009-03-12 06:52:53 +000015919 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015920 // This is efficient for any integer data type (including i8/i16) and
15921 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015922 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015923 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15924 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015925
Chris Lattnerd1980a52009-03-12 06:52:53 +000015926 // Zero extend the condition if needed.
15927 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015928
Chris Lattnerd1980a52009-03-12 06:52:53 +000015929 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15930 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015931 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015932 if (N->getNumValues() == 2) // Dead flag value?
15933 return DCI.CombineTo(N, Cond, SDValue());
15934 return Cond;
15935 }
Eric Christopherfd179292009-08-27 18:07:15 +000015936
Chris Lattnercee56e72009-03-13 05:53:31 +000015937 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15938 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015939 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015940 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15941 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015942
Chris Lattner97a29a52009-03-13 05:22:11 +000015943 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015944 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15945 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015946 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15947 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015948
Chris Lattner97a29a52009-03-13 05:22:11 +000015949 if (N->getNumValues() == 2) // Dead flag value?
15950 return DCI.CombineTo(N, Cond, SDValue());
15951 return Cond;
15952 }
Eric Christopherfd179292009-08-27 18:07:15 +000015953
Chris Lattnercee56e72009-03-13 05:53:31 +000015954 // Optimize cases that will turn into an LEA instruction. This requires
15955 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015956 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015957 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015958 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015959
Chris Lattnercee56e72009-03-13 05:53:31 +000015960 bool isFastMultiplier = false;
15961 if (Diff < 10) {
15962 switch ((unsigned char)Diff) {
15963 default: break;
15964 case 1: // result = add base, cond
15965 case 2: // result = lea base( , cond*2)
15966 case 3: // result = lea base(cond, cond*2)
15967 case 4: // result = lea base( , cond*4)
15968 case 5: // result = lea base(cond, cond*4)
15969 case 8: // result = lea base( , cond*8)
15970 case 9: // result = lea base(cond, cond*8)
15971 isFastMultiplier = true;
15972 break;
15973 }
15974 }
Eric Christopherfd179292009-08-27 18:07:15 +000015975
Chris Lattnercee56e72009-03-13 05:53:31 +000015976 if (isFastMultiplier) {
15977 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015978 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15979 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015980 // Zero extend the condition if needed.
15981 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15982 Cond);
15983 // Scale the condition by the difference.
15984 if (Diff != 1)
15985 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15986 DAG.getConstant(Diff, Cond.getValueType()));
15987
15988 // Add the base if non-zero.
15989 if (FalseC->getAPIntValue() != 0)
15990 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15991 SDValue(FalseC, 0));
15992 if (N->getNumValues() == 2) // Dead flag value?
15993 return DCI.CombineTo(N, Cond, SDValue());
15994 return Cond;
15995 }
Eric Christopherfd179292009-08-27 18:07:15 +000015996 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015997 }
15998 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015999
16000 // Handle these cases:
16001 // (select (x != c), e, c) -> select (x != c), e, x),
16002 // (select (x == c), c, e) -> select (x == c), x, e)
16003 // where the c is an integer constant, and the "select" is the combination
16004 // of CMOV and CMP.
16005 //
16006 // The rationale for this change is that the conditional-move from a constant
16007 // needs two instructions, however, conditional-move from a register needs
16008 // only one instruction.
16009 //
16010 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16011 // some instruction-combining opportunities. This opt needs to be
16012 // postponed as late as possible.
16013 //
16014 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16015 // the DCI.xxxx conditions are provided to postpone the optimization as
16016 // late as possible.
16017
16018 ConstantSDNode *CmpAgainst = 0;
16019 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16020 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016021 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016022
16023 if (CC == X86::COND_NE &&
16024 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16025 CC = X86::GetOppositeBranchCondition(CC);
16026 std::swap(TrueOp, FalseOp);
16027 }
16028
16029 if (CC == X86::COND_E &&
16030 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16031 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16032 DAG.getConstant(CC, MVT::i8), Cond };
16033 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16034 array_lengthof(Ops));
16035 }
16036 }
16037 }
16038
Chris Lattnerd1980a52009-03-12 06:52:53 +000016039 return SDValue();
16040}
16041
Evan Cheng0b0cd912009-03-28 05:57:29 +000016042/// PerformMulCombine - Optimize a single multiply with constant into two
16043/// in order to implement it with two cheaper instructions, e.g.
16044/// LEA + SHL, LEA + LEA.
16045static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16046 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016047 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16048 return SDValue();
16049
Owen Andersone50ed302009-08-10 22:56:29 +000016050 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016051 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016052 return SDValue();
16053
16054 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16055 if (!C)
16056 return SDValue();
16057 uint64_t MulAmt = C->getZExtValue();
16058 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16059 return SDValue();
16060
16061 uint64_t MulAmt1 = 0;
16062 uint64_t MulAmt2 = 0;
16063 if ((MulAmt % 9) == 0) {
16064 MulAmt1 = 9;
16065 MulAmt2 = MulAmt / 9;
16066 } else if ((MulAmt % 5) == 0) {
16067 MulAmt1 = 5;
16068 MulAmt2 = MulAmt / 5;
16069 } else if ((MulAmt % 3) == 0) {
16070 MulAmt1 = 3;
16071 MulAmt2 = MulAmt / 3;
16072 }
16073 if (MulAmt2 &&
16074 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
16075 DebugLoc DL = N->getDebugLoc();
16076
16077 if (isPowerOf2_64(MulAmt2) &&
16078 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16079 // If second multiplifer is pow2, issue it first. We want the multiply by
16080 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16081 // is an add.
16082 std::swap(MulAmt1, MulAmt2);
16083
16084 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016085 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016086 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016087 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016088 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016089 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016090 DAG.getConstant(MulAmt1, VT));
16091
Eric Christopherfd179292009-08-27 18:07:15 +000016092 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016093 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016094 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016095 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016096 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016097 DAG.getConstant(MulAmt2, VT));
16098
16099 // Do not add new nodes to DAG combiner worklist.
16100 DCI.CombineTo(N, NewMul, false);
16101 }
16102 return SDValue();
16103}
16104
Evan Chengad9c0a32009-12-15 00:53:42 +000016105static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16106 SDValue N0 = N->getOperand(0);
16107 SDValue N1 = N->getOperand(1);
16108 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16109 EVT VT = N0.getValueType();
16110
16111 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16112 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016113 if (VT.isInteger() && !VT.isVector() &&
16114 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016115 N0.getOperand(1).getOpcode() == ISD::Constant) {
16116 SDValue N00 = N0.getOperand(0);
16117 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16118 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16119 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16120 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16121 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16122 APInt ShAmt = N1C->getAPIntValue();
16123 Mask = Mask.shl(ShAmt);
16124 if (Mask != 0)
16125 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
16126 N00, DAG.getConstant(Mask, VT));
16127 }
16128 }
16129
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016130 // Hardware support for vector shifts is sparse which makes us scalarize the
16131 // vector operations in many cases. Also, on sandybridge ADD is faster than
16132 // shl.
16133 // (shl V, 1) -> add V,V
16134 if (isSplatVector(N1.getNode())) {
16135 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16136 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16137 // We shift all of the values by one. In many cases we do not have
16138 // hardware support for this operation. This is better expressed as an ADD
16139 // of two values.
16140 if (N1C && (1 == N1C->getZExtValue())) {
16141 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
16142 }
16143 }
16144
Evan Chengad9c0a32009-12-15 00:53:42 +000016145 return SDValue();
16146}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016147
Nate Begeman740ab032009-01-26 00:52:55 +000016148/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
16149/// when possible.
16150static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016151 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016152 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016153 if (N->getOpcode() == ISD::SHL) {
16154 SDValue V = PerformSHLCombine(N, DAG);
16155 if (V.getNode()) return V;
16156 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016157
Michael Liao42317cc2013-03-20 02:33:21 +000016158 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016159}
16160
Stuart Hastings865f0932011-06-03 23:53:54 +000016161// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16162// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16163// and friends. Likewise for OR -> CMPNEQSS.
16164static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16165 TargetLowering::DAGCombinerInfo &DCI,
16166 const X86Subtarget *Subtarget) {
16167 unsigned opcode;
16168
16169 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16170 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016171 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016172 SDValue N0 = N->getOperand(0);
16173 SDValue N1 = N->getOperand(1);
16174 SDValue CMP0 = N0->getOperand(1);
16175 SDValue CMP1 = N1->getOperand(1);
16176 DebugLoc DL = N->getDebugLoc();
16177
16178 // The SETCCs should both refer to the same CMP.
16179 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16180 return SDValue();
16181
16182 SDValue CMP00 = CMP0->getOperand(0);
16183 SDValue CMP01 = CMP0->getOperand(1);
16184 EVT VT = CMP00.getValueType();
16185
16186 if (VT == MVT::f32 || VT == MVT::f64) {
16187 bool ExpectingFlags = false;
16188 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016189 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016190 !ExpectingFlags && UI != UE; ++UI)
16191 switch (UI->getOpcode()) {
16192 default:
16193 case ISD::BR_CC:
16194 case ISD::BRCOND:
16195 case ISD::SELECT:
16196 ExpectingFlags = true;
16197 break;
16198 case ISD::CopyToReg:
16199 case ISD::SIGN_EXTEND:
16200 case ISD::ZERO_EXTEND:
16201 case ISD::ANY_EXTEND:
16202 break;
16203 }
16204
16205 if (!ExpectingFlags) {
16206 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16207 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16208
16209 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16210 X86::CondCode tmp = cc0;
16211 cc0 = cc1;
16212 cc1 = tmp;
16213 }
16214
16215 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16216 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16217 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16218 X86ISD::NodeType NTOperator = is64BitFP ?
16219 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16220 // FIXME: need symbolic constants for these magic numbers.
16221 // See X86ATTInstPrinter.cpp:printSSECC().
16222 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16223 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16224 DAG.getConstant(x86cc, MVT::i8));
16225 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16226 OnesOrZeroesF);
16227 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16228 DAG.getConstant(1, MVT::i32));
16229 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16230 return OneBitOfTruth;
16231 }
16232 }
16233 }
16234 }
16235 return SDValue();
16236}
16237
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016238/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16239/// so it can be folded inside ANDNP.
16240static bool CanFoldXORWithAllOnes(const SDNode *N) {
16241 EVT VT = N->getValueType(0);
16242
16243 // Match direct AllOnes for 128 and 256-bit vectors
16244 if (ISD::isBuildVectorAllOnes(N))
16245 return true;
16246
16247 // Look through a bit convert.
16248 if (N->getOpcode() == ISD::BITCAST)
16249 N = N->getOperand(0).getNode();
16250
16251 // Sometimes the operand may come from a insert_subvector building a 256-bit
16252 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016253 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016254 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16255 SDValue V1 = N->getOperand(0);
16256 SDValue V2 = N->getOperand(1);
16257
16258 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16259 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16260 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16261 ISD::isBuildVectorAllOnes(V2.getNode()))
16262 return true;
16263 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016264
16265 return false;
16266}
16267
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016268// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16269// register. In most cases we actually compare or select YMM-sized registers
16270// and mixing the two types creates horrible code. This method optimizes
16271// some of the transition sequences.
16272static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16273 TargetLowering::DAGCombinerInfo &DCI,
16274 const X86Subtarget *Subtarget) {
16275 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016276 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016277 return SDValue();
16278
16279 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16280 N->getOpcode() == ISD::ZERO_EXTEND ||
16281 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16282
16283 SDValue Narrow = N->getOperand(0);
16284 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016285 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016286 return SDValue();
16287
16288 if (Narrow->getOpcode() != ISD::XOR &&
16289 Narrow->getOpcode() != ISD::AND &&
16290 Narrow->getOpcode() != ISD::OR)
16291 return SDValue();
16292
16293 SDValue N0 = Narrow->getOperand(0);
16294 SDValue N1 = Narrow->getOperand(1);
16295 DebugLoc DL = Narrow->getDebugLoc();
16296
16297 // The Left side has to be a trunc.
16298 if (N0.getOpcode() != ISD::TRUNCATE)
16299 return SDValue();
16300
16301 // The type of the truncated inputs.
16302 EVT WideVT = N0->getOperand(0)->getValueType(0);
16303 if (WideVT != VT)
16304 return SDValue();
16305
16306 // The right side has to be a 'trunc' or a constant vector.
16307 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16308 bool RHSConst = (isSplatVector(N1.getNode()) &&
16309 isa<ConstantSDNode>(N1->getOperand(0)));
16310 if (!RHSTrunc && !RHSConst)
16311 return SDValue();
16312
16313 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16314
16315 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16316 return SDValue();
16317
16318 // Set N0 and N1 to hold the inputs to the new wide operation.
16319 N0 = N0->getOperand(0);
16320 if (RHSConst) {
16321 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16322 N1->getOperand(0));
16323 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16324 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16325 } else if (RHSTrunc) {
16326 N1 = N1->getOperand(0);
16327 }
16328
16329 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016330 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016331 unsigned Opcode = N->getOpcode();
16332 switch (Opcode) {
16333 case ISD::ANY_EXTEND:
16334 return Op;
16335 case ISD::ZERO_EXTEND: {
16336 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16337 APInt Mask = APInt::getAllOnesValue(InBits);
16338 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16339 return DAG.getNode(ISD::AND, DL, VT,
16340 Op, DAG.getConstant(Mask, VT));
16341 }
16342 case ISD::SIGN_EXTEND:
16343 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16344 Op, DAG.getValueType(NarrowVT));
16345 default:
16346 llvm_unreachable("Unexpected opcode");
16347 }
16348}
16349
Nate Begemanb65c1752010-12-17 22:55:37 +000016350static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16351 TargetLowering::DAGCombinerInfo &DCI,
16352 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016353 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016354 if (DCI.isBeforeLegalizeOps())
16355 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016356
Stuart Hastings865f0932011-06-03 23:53:54 +000016357 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16358 if (R.getNode())
16359 return R;
16360
Craig Topperb926afc2012-12-17 05:12:30 +000016361 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016362 // BLSI is X & (-X)
16363 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016364 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16365 SDValue N0 = N->getOperand(0);
16366 SDValue N1 = N->getOperand(1);
16367 DebugLoc DL = N->getDebugLoc();
16368
Craig Topperb4c94572011-10-21 06:55:01 +000016369 // Check LHS for neg
16370 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16371 isZero(N0.getOperand(0)))
16372 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16373
16374 // Check RHS for neg
16375 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16376 isZero(N1.getOperand(0)))
16377 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16378
16379 // Check LHS for X-1
16380 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16381 isAllOnes(N0.getOperand(1)))
16382 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16383
16384 // Check RHS for X-1
16385 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16386 isAllOnes(N1.getOperand(1)))
16387 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16388
Craig Topper54a11172011-10-14 07:06:56 +000016389 return SDValue();
16390 }
16391
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016392 // Want to form ANDNP nodes:
16393 // 1) In the hopes of then easily combining them with OR and AND nodes
16394 // to form PBLEND/PSIGN.
16395 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016396 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016397 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016398
Nate Begemanb65c1752010-12-17 22:55:37 +000016399 SDValue N0 = N->getOperand(0);
16400 SDValue N1 = N->getOperand(1);
16401 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016402
Nate Begemanb65c1752010-12-17 22:55:37 +000016403 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016404 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016405 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16406 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016407 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016408
16409 // Check RHS for vnot
16410 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016411 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16412 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016413 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016414
Nate Begemanb65c1752010-12-17 22:55:37 +000016415 return SDValue();
16416}
16417
Evan Cheng760d1942010-01-04 21:22:48 +000016418static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016419 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016420 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016421 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016422 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016423 return SDValue();
16424
Stuart Hastings865f0932011-06-03 23:53:54 +000016425 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16426 if (R.getNode())
16427 return R;
16428
Evan Cheng760d1942010-01-04 21:22:48 +000016429 SDValue N0 = N->getOperand(0);
16430 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016431
Nate Begemanb65c1752010-12-17 22:55:37 +000016432 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016433 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016434 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016435 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016436 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016437
Craig Topper1666cb62011-11-19 07:07:26 +000016438 // Canonicalize pandn to RHS
16439 if (N0.getOpcode() == X86ISD::ANDNP)
16440 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016441 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016442 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16443 SDValue Mask = N1.getOperand(0);
16444 SDValue X = N1.getOperand(1);
16445 SDValue Y;
16446 if (N0.getOperand(0) == Mask)
16447 Y = N0.getOperand(1);
16448 if (N0.getOperand(1) == Mask)
16449 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016450
Craig Topper1666cb62011-11-19 07:07:26 +000016451 // Check to see if the mask appeared in both the AND and ANDNP and
16452 if (!Y.getNode())
16453 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016454
Craig Topper1666cb62011-11-19 07:07:26 +000016455 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016456 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016457 if (Mask.getOpcode() == ISD::BITCAST)
16458 Mask = Mask.getOperand(0);
16459 if (X.getOpcode() == ISD::BITCAST)
16460 X = X.getOperand(0);
16461 if (Y.getOpcode() == ISD::BITCAST)
16462 Y = Y.getOperand(0);
16463
Craig Topper1666cb62011-11-19 07:07:26 +000016464 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016465
Craig Toppered2e13d2012-01-22 19:15:14 +000016466 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016467 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16468 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016469 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016470 unsigned SraAmt = ~0;
16471 if (Mask.getOpcode() == ISD::SRA) {
16472 SDValue Amt = Mask.getOperand(1);
16473 if (isSplatVector(Amt.getNode())) {
16474 SDValue SclrAmt = Amt->getOperand(0);
16475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16476 SraAmt = C->getZExtValue();
16477 }
16478 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16479 SDValue SraC = Mask.getOperand(1);
16480 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16481 }
Craig Topper1666cb62011-11-19 07:07:26 +000016482 if ((SraAmt + 1) != EltBits)
16483 return SDValue();
16484
16485 DebugLoc DL = N->getDebugLoc();
16486
16487 // Now we know we at least have a plendvb with the mask val. See if
16488 // we can form a psignb/w/d.
16489 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016490 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16491 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016492 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16493 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16494 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016495 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016496 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016497 }
16498 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016499 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016500 return SDValue();
16501
16502 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16503
16504 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16505 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16506 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016507 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016508 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016509 }
16510 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016511
Craig Topper1666cb62011-11-19 07:07:26 +000016512 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16513 return SDValue();
16514
Nate Begemanb65c1752010-12-17 22:55:37 +000016515 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016516 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16517 std::swap(N0, N1);
16518 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16519 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016520 if (!N0.hasOneUse() || !N1.hasOneUse())
16521 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016522
16523 SDValue ShAmt0 = N0.getOperand(1);
16524 if (ShAmt0.getValueType() != MVT::i8)
16525 return SDValue();
16526 SDValue ShAmt1 = N1.getOperand(1);
16527 if (ShAmt1.getValueType() != MVT::i8)
16528 return SDValue();
16529 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16530 ShAmt0 = ShAmt0.getOperand(0);
16531 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16532 ShAmt1 = ShAmt1.getOperand(0);
16533
16534 DebugLoc DL = N->getDebugLoc();
16535 unsigned Opc = X86ISD::SHLD;
16536 SDValue Op0 = N0.getOperand(0);
16537 SDValue Op1 = N1.getOperand(0);
16538 if (ShAmt0.getOpcode() == ISD::SUB) {
16539 Opc = X86ISD::SHRD;
16540 std::swap(Op0, Op1);
16541 std::swap(ShAmt0, ShAmt1);
16542 }
16543
Evan Cheng8b1190a2010-04-28 01:18:01 +000016544 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016545 if (ShAmt1.getOpcode() == ISD::SUB) {
16546 SDValue Sum = ShAmt1.getOperand(0);
16547 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016548 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16549 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16550 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16551 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016552 return DAG.getNode(Opc, DL, VT,
16553 Op0, Op1,
16554 DAG.getNode(ISD::TRUNCATE, DL,
16555 MVT::i8, ShAmt0));
16556 }
16557 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16558 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16559 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016560 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016561 return DAG.getNode(Opc, DL, VT,
16562 N0.getOperand(0), N1.getOperand(0),
16563 DAG.getNode(ISD::TRUNCATE, DL,
16564 MVT::i8, ShAmt0));
16565 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016566
Evan Cheng760d1942010-01-04 21:22:48 +000016567 return SDValue();
16568}
16569
Manman Ren92363622012-06-07 22:39:10 +000016570// Generate NEG and CMOV for integer abs.
16571static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16572 EVT VT = N->getValueType(0);
16573
16574 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16575 // 8-bit integer abs to NEG and CMOV.
16576 if (VT.isInteger() && VT.getSizeInBits() == 8)
16577 return SDValue();
16578
16579 SDValue N0 = N->getOperand(0);
16580 SDValue N1 = N->getOperand(1);
16581 DebugLoc DL = N->getDebugLoc();
16582
16583 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16584 // and change it to SUB and CMOV.
16585 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16586 N0.getOpcode() == ISD::ADD &&
16587 N0.getOperand(1) == N1 &&
16588 N1.getOpcode() == ISD::SRA &&
16589 N1.getOperand(0) == N0.getOperand(0))
16590 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16591 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16592 // Generate SUB & CMOV.
16593 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16594 DAG.getConstant(0, VT), N0.getOperand(0));
16595
16596 SDValue Ops[] = { N0.getOperand(0), Neg,
16597 DAG.getConstant(X86::COND_GE, MVT::i8),
16598 SDValue(Neg.getNode(), 1) };
16599 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16600 Ops, array_lengthof(Ops));
16601 }
16602 return SDValue();
16603}
16604
Craig Topper3738ccd2011-12-27 06:27:23 +000016605// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016606static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16607 TargetLowering::DAGCombinerInfo &DCI,
16608 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016609 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016610 if (DCI.isBeforeLegalizeOps())
16611 return SDValue();
16612
Manman Ren45d53b82012-06-08 18:58:26 +000016613 if (Subtarget->hasCMov()) {
16614 SDValue RV = performIntegerAbsCombine(N, DAG);
16615 if (RV.getNode())
16616 return RV;
16617 }
Manman Ren92363622012-06-07 22:39:10 +000016618
16619 // Try forming BMI if it is available.
16620 if (!Subtarget->hasBMI())
16621 return SDValue();
16622
Craig Topperb4c94572011-10-21 06:55:01 +000016623 if (VT != MVT::i32 && VT != MVT::i64)
16624 return SDValue();
16625
Craig Topper3738ccd2011-12-27 06:27:23 +000016626 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16627
Craig Topperb4c94572011-10-21 06:55:01 +000016628 // Create BLSMSK instructions by finding X ^ (X-1)
16629 SDValue N0 = N->getOperand(0);
16630 SDValue N1 = N->getOperand(1);
16631 DebugLoc DL = N->getDebugLoc();
16632
16633 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16634 isAllOnes(N0.getOperand(1)))
16635 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16636
16637 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16638 isAllOnes(N1.getOperand(1)))
16639 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16640
16641 return SDValue();
16642}
16643
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016644/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16645static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016646 TargetLowering::DAGCombinerInfo &DCI,
16647 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016648 LoadSDNode *Ld = cast<LoadSDNode>(N);
16649 EVT RegVT = Ld->getValueType(0);
16650 EVT MemVT = Ld->getMemoryVT();
16651 DebugLoc dl = Ld->getDebugLoc();
16652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016653 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016654
Michael Liaod4584c92013-03-25 23:50:10 +000016655 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016656 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016657 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000016658 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016659 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016660 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016661 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016662 if (NumElems < 2)
16663 return SDValue();
16664
Nadav Rotem48177ac2013-01-18 23:10:30 +000016665 SDValue Ptr = Ld->getBasePtr();
16666 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16667
16668 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16669 NumElems/2);
16670 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16671 Ld->getPointerInfo(), Ld->isVolatile(),
16672 Ld->isNonTemporal(), Ld->isInvariant(),
16673 Alignment);
16674 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16675 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16676 Ld->getPointerInfo(), Ld->isVolatile(),
16677 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000016678 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016679 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16680 Load1.getValue(1),
16681 Load2.getValue(1));
16682
16683 SDValue NewVec = DAG.getUNDEF(RegVT);
16684 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16685 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16686 return DCI.CombineTo(N, NewVec, TF, true);
16687 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016688
Nadav Rotemca6f2962011-09-18 19:00:23 +000016689 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016690 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16691 // expansion is still better than scalar code.
16692 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16693 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016694 // TODO: It is possible to support ZExt by zeroing the undef values
16695 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016696 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16697 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016698 assert(MemVT != RegVT && "Cannot extend to the same type");
16699 assert(MemVT.isVector() && "Must load a vector from memory");
16700
16701 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016702 unsigned MemSz = MemVT.getSizeInBits();
16703 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016704
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016705 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16706 return SDValue();
16707
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016708 // All sizes must be a power of two.
16709 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16710 return SDValue();
16711
16712 // Attempt to load the original value using scalar loads.
16713 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016714 MVT SclrLoadTy = MVT::i8;
16715 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16716 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16717 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016718 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016719 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016720 }
16721 }
16722
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016723 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16724 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16725 (64 <= MemSz))
16726 SclrLoadTy = MVT::f64;
16727
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016728 // Calculate the number of scalar loads that we need to perform
16729 // in order to load our vector from memory.
16730 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016731 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16732 return SDValue();
16733
16734 unsigned loadRegZize = RegSz;
16735 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16736 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016737
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016738 // Represent our vector as a sequence of elements which are the
16739 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016740 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016741 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016742
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016743 // Represent the data using the same element type that is stored in
16744 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016745 EVT WideVecVT =
16746 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016747 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016748
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016749 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16750 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016751
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016752 // We can't shuffle using an illegal type.
16753 if (!TLI.isTypeLegal(WideVecVT))
16754 return SDValue();
16755
16756 SmallVector<SDValue, 8> Chains;
16757 SDValue Ptr = Ld->getBasePtr();
16758 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16759 TLI.getPointerTy());
16760 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16761
16762 for (unsigned i = 0; i < NumLoads; ++i) {
16763 // Perform a single load.
16764 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16765 Ptr, Ld->getPointerInfo(),
16766 Ld->isVolatile(), Ld->isNonTemporal(),
16767 Ld->isInvariant(), Ld->getAlignment());
16768 Chains.push_back(ScalarLoad.getValue(1));
16769 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16770 // another round of DAGCombining.
16771 if (i == 0)
16772 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16773 else
16774 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16775 ScalarLoad, DAG.getIntPtrConstant(i));
16776
16777 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16778 }
16779
16780 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16781 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016782
16783 // Bitcast the loaded value to a vector of the original element type, in
16784 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016785 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016786 unsigned SizeRatio = RegSz/MemSz;
16787
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016788 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016789 // If we have SSE4.1 we can directly emit a VSEXT node.
16790 if (Subtarget->hasSSE41()) {
16791 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16792 return DCI.CombineTo(N, Sext, TF, true);
16793 }
16794
16795 // Otherwise we'll shuffle the small elements in the high bits of the
16796 // larger type and perform an arithmetic shift. If the shift is not legal
16797 // it's better to scalarize.
16798 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16799 return SDValue();
16800
16801 // Redistribute the loaded elements into the different locations.
16802 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16803 for (unsigned i = 0; i != NumElems; ++i)
16804 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16805
16806 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16807 DAG.getUNDEF(WideVecVT),
16808 &ShuffleVec[0]);
16809
16810 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16811
16812 // Build the arithmetic shift.
16813 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16814 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016815 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16816 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016817
16818 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016819 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016820
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016821 // Redistribute the loaded elements into the different locations.
16822 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016823 for (unsigned i = 0; i != NumElems; ++i)
16824 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016825
16826 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016827 DAG.getUNDEF(WideVecVT),
16828 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016829
16830 // Bitcast to the requested type.
16831 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16832 // Replace the original load with the new sequence
16833 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016834 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016835 }
16836
16837 return SDValue();
16838}
16839
Chris Lattner149a4e52008-02-22 02:09:43 +000016840/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016841static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016842 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016843 StoreSDNode *St = cast<StoreSDNode>(N);
16844 EVT VT = St->getValue().getValueType();
16845 EVT StVT = St->getMemoryVT();
16846 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016847 SDValue StoredVal = St->getOperand(1);
16848 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16849
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016850 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016851 // On Sandy Bridge, 256-bit memory operations are executed by two
16852 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16853 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000016854 unsigned Alignment = St->getAlignment();
16855 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016856 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016857 StVT == VT && !IsAligned) {
16858 unsigned NumElems = VT.getVectorNumElements();
16859 if (NumElems < 2)
16860 return SDValue();
16861
16862 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16863 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016864
16865 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16866 SDValue Ptr0 = St->getBasePtr();
16867 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16868
16869 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16870 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016871 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016872 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16873 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016874 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000016875 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016876 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16877 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016878
16879 // Optimize trunc store (of multiple scalars) to shuffle and store.
16880 // First, pack all of the elements in one place. Next, store to memory
16881 // in fewer chunks.
16882 if (St->isTruncatingStore() && VT.isVector()) {
16883 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16884 unsigned NumElems = VT.getVectorNumElements();
16885 assert(StVT != VT && "Cannot truncate to the same type");
16886 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16887 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16888
16889 // From, To sizes and ElemCount must be pow of two
16890 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016891 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016892 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016893 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016894
Nadav Rotem614061b2011-08-10 19:30:14 +000016895 unsigned SizeRatio = FromSz / ToSz;
16896
16897 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16898
16899 // Create a type on which we perform the shuffle
16900 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16901 StVT.getScalarType(), NumElems*SizeRatio);
16902
16903 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16904
16905 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16906 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016907 for (unsigned i = 0; i != NumElems; ++i)
16908 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016909
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016910 // Can't shuffle using an illegal type.
16911 if (!TLI.isTypeLegal(WideVecVT))
16912 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016913
16914 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016915 DAG.getUNDEF(WideVecVT),
16916 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016917 // At this point all of the data is stored at the bottom of the
16918 // register. We now need to save it to mem.
16919
16920 // Find the largest store unit
16921 MVT StoreType = MVT::i8;
16922 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16923 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16924 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016925 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016926 StoreType = Tp;
16927 }
16928
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016929 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16930 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16931 (64 <= NumElems * ToSz))
16932 StoreType = MVT::f64;
16933
Nadav Rotem614061b2011-08-10 19:30:14 +000016934 // Bitcast the original vector into a vector of store-size units
16935 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016936 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016937 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16938 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16939 SmallVector<SDValue, 8> Chains;
16940 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16941 TLI.getPointerTy());
16942 SDValue Ptr = St->getBasePtr();
16943
16944 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016945 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016946 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16947 StoreType, ShuffWide,
16948 DAG.getIntPtrConstant(i));
16949 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16950 St->getPointerInfo(), St->isVolatile(),
16951 St->isNonTemporal(), St->getAlignment());
16952 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16953 Chains.push_back(Ch);
16954 }
16955
16956 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16957 Chains.size());
16958 }
16959
Chris Lattner149a4e52008-02-22 02:09:43 +000016960 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16961 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016962 // A preferable solution to the general problem is to figure out the right
16963 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016964
16965 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016966 if (VT.getSizeInBits() != 64)
16967 return SDValue();
16968
Devang Patel578efa92009-06-05 21:57:13 +000016969 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016970 bool NoImplicitFloatOps = F->getAttributes().
16971 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016972 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016973 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016974 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016975 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016976 isa<LoadSDNode>(St->getValue()) &&
16977 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16978 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016979 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016980 LoadSDNode *Ld = 0;
16981 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016982 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016983 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016984 // Must be a store of a load. We currently handle two cases: the load
16985 // is a direct child, and it's under an intervening TokenFactor. It is
16986 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016987 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016988 Ld = cast<LoadSDNode>(St->getChain());
16989 else if (St->getValue().hasOneUse() &&
16990 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016991 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016992 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016993 TokenFactorIndex = i;
16994 Ld = cast<LoadSDNode>(St->getValue());
16995 } else
16996 Ops.push_back(ChainVal->getOperand(i));
16997 }
16998 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016999
Evan Cheng536e6672009-03-12 05:59:15 +000017000 if (!Ld || !ISD::isNormalLoad(Ld))
17001 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017002
Evan Cheng536e6672009-03-12 05:59:15 +000017003 // If this is not the MMX case, i.e. we are just turning i64 load/store
17004 // into f64 load/store, avoid the transformation if there are multiple
17005 // uses of the loaded value.
17006 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17007 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017008
Evan Cheng536e6672009-03-12 05:59:15 +000017009 DebugLoc LdDL = Ld->getDebugLoc();
17010 DebugLoc StDL = N->getDebugLoc();
17011 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17012 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17013 // pair instead.
17014 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017015 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017016 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17017 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017018 Ld->isNonTemporal(), Ld->isInvariant(),
17019 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017020 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017021 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017022 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017023 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017024 Ops.size());
17025 }
Evan Cheng536e6672009-03-12 05:59:15 +000017026 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017027 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017028 St->isVolatile(), St->isNonTemporal(),
17029 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017030 }
Evan Cheng536e6672009-03-12 05:59:15 +000017031
17032 // Otherwise, lower to two pairs of 32-bit loads / stores.
17033 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017034 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17035 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017036
Owen Anderson825b72b2009-08-11 20:47:22 +000017037 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017038 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017039 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017040 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017041 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017042 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017043 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017044 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017045 MinAlign(Ld->getAlignment(), 4));
17046
17047 SDValue NewChain = LoLd.getValue(1);
17048 if (TokenFactorIndex != -1) {
17049 Ops.push_back(LoLd);
17050 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017051 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017052 Ops.size());
17053 }
17054
17055 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017056 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17057 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017058
17059 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017060 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017061 St->isVolatile(), St->isNonTemporal(),
17062 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017063 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017064 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017065 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017066 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017067 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017068 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017069 }
Dan Gohman475871a2008-07-27 21:46:04 +000017070 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017071}
17072
Duncan Sands17470be2011-09-22 20:15:48 +000017073/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17074/// and return the operands for the horizontal operation in LHS and RHS. A
17075/// horizontal operation performs the binary operation on successive elements
17076/// of its first operand, then on successive elements of its second operand,
17077/// returning the resulting values in a vector. For example, if
17078/// A = < float a0, float a1, float a2, float a3 >
17079/// and
17080/// B = < float b0, float b1, float b2, float b3 >
17081/// then the result of doing a horizontal operation on A and B is
17082/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17083/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17084/// A horizontal-op B, for some already available A and B, and if so then LHS is
17085/// set to A, RHS to B, and the routine returns 'true'.
17086/// Note that the binary operation should have the property that if one of the
17087/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017088static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017089 // Look for the following pattern: if
17090 // A = < float a0, float a1, float a2, float a3 >
17091 // B = < float b0, float b1, float b2, float b3 >
17092 // and
17093 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17094 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17095 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17096 // which is A horizontal-op B.
17097
17098 // At least one of the operands should be a vector shuffle.
17099 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17100 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17101 return false;
17102
17103 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017104
17105 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17106 "Unsupported vector type for horizontal add/sub");
17107
17108 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17109 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017110 unsigned NumElts = VT.getVectorNumElements();
17111 unsigned NumLanes = VT.getSizeInBits()/128;
17112 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017113 assert((NumLaneElts % 2 == 0) &&
17114 "Vector type should have an even number of elements in each lane");
17115 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017116
17117 // View LHS in the form
17118 // LHS = VECTOR_SHUFFLE A, B, LMask
17119 // If LHS is not a shuffle then pretend it is the shuffle
17120 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17121 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17122 // type VT.
17123 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017124 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017125 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17126 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17127 A = LHS.getOperand(0);
17128 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17129 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017130 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17131 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017132 } else {
17133 if (LHS.getOpcode() != ISD::UNDEF)
17134 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017135 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017136 LMask[i] = i;
17137 }
17138
17139 // Likewise, view RHS in the form
17140 // RHS = VECTOR_SHUFFLE C, D, RMask
17141 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017142 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017143 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17144 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17145 C = RHS.getOperand(0);
17146 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17147 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017148 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17149 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017150 } else {
17151 if (RHS.getOpcode() != ISD::UNDEF)
17152 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017153 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017154 RMask[i] = i;
17155 }
17156
17157 // Check that the shuffles are both shuffling the same vectors.
17158 if (!(A == C && B == D) && !(A == D && B == C))
17159 return false;
17160
17161 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17162 if (!A.getNode() && !B.getNode())
17163 return false;
17164
17165 // If A and B occur in reverse order in RHS, then "swap" them (which means
17166 // rewriting the mask).
17167 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017168 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017169
17170 // At this point LHS and RHS are equivalent to
17171 // LHS = VECTOR_SHUFFLE A, B, LMask
17172 // RHS = VECTOR_SHUFFLE A, B, RMask
17173 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017174 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017175 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017176
Craig Topperf8363302011-12-02 08:18:41 +000017177 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017178 if (LIdx < 0 || RIdx < 0 ||
17179 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17180 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017181 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017182
Craig Topperf8363302011-12-02 08:18:41 +000017183 // Check that successive elements are being operated on. If not, this is
17184 // not a horizontal operation.
17185 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17186 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017187 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017188 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017189 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017190 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017191 }
17192
17193 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17194 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17195 return true;
17196}
17197
17198/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17199static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17200 const X86Subtarget *Subtarget) {
17201 EVT VT = N->getValueType(0);
17202 SDValue LHS = N->getOperand(0);
17203 SDValue RHS = N->getOperand(1);
17204
17205 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017206 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017207 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017208 isHorizontalBinOp(LHS, RHS, true))
17209 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17210 return SDValue();
17211}
17212
17213/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17214static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17215 const X86Subtarget *Subtarget) {
17216 EVT VT = N->getValueType(0);
17217 SDValue LHS = N->getOperand(0);
17218 SDValue RHS = N->getOperand(1);
17219
17220 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017221 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017222 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017223 isHorizontalBinOp(LHS, RHS, false))
17224 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17225 return SDValue();
17226}
17227
Chris Lattner6cf73262008-01-25 06:14:17 +000017228/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17229/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017230static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017231 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17232 // F[X]OR(0.0, x) -> x
17233 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017234 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17235 if (C->getValueAPF().isPosZero())
17236 return N->getOperand(1);
17237 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17238 if (C->getValueAPF().isPosZero())
17239 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017240 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017241}
17242
Nadav Rotemd60cb112012-08-19 13:06:16 +000017243/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17244/// X86ISD::FMAX nodes.
17245static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17246 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17247
17248 // Only perform optimizations if UnsafeMath is used.
17249 if (!DAG.getTarget().Options.UnsafeFPMath)
17250 return SDValue();
17251
17252 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017253 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017254 unsigned NewOp = 0;
17255 switch (N->getOpcode()) {
17256 default: llvm_unreachable("unknown opcode");
17257 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17258 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17259 }
17260
17261 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17262 N->getOperand(0), N->getOperand(1));
17263}
17264
Chris Lattneraf723b92008-01-25 05:46:26 +000017265/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017266static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017267 // FAND(0.0, x) -> 0.0
17268 // FAND(x, 0.0) -> 0.0
17269 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17270 if (C->getValueAPF().isPosZero())
17271 return N->getOperand(0);
17272 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17273 if (C->getValueAPF().isPosZero())
17274 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017275 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017276}
17277
Dan Gohmane5af2d32009-01-29 01:59:02 +000017278static SDValue PerformBTCombine(SDNode *N,
17279 SelectionDAG &DAG,
17280 TargetLowering::DAGCombinerInfo &DCI) {
17281 // BT ignores high bits in the bit index operand.
17282 SDValue Op1 = N->getOperand(1);
17283 if (Op1.hasOneUse()) {
17284 unsigned BitWidth = Op1.getValueSizeInBits();
17285 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17286 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017287 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17288 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017290 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17291 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17292 DCI.CommitTargetLoweringOpt(TLO);
17293 }
17294 return SDValue();
17295}
Chris Lattner83e6c992006-10-04 06:57:07 +000017296
Eli Friedman7a5e5552009-06-07 06:52:44 +000017297static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17298 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017299 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017300 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017301 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017302 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017303 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017304 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017305 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017306 }
17307 return SDValue();
17308}
17309
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017310static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17311 const X86Subtarget *Subtarget) {
17312 EVT VT = N->getValueType(0);
17313 if (!VT.isVector())
17314 return SDValue();
17315
17316 SDValue N0 = N->getOperand(0);
17317 SDValue N1 = N->getOperand(1);
17318 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17319 DebugLoc dl = N->getDebugLoc();
17320
17321 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17322 // both SSE and AVX2 since there is no sign-extended shift right
17323 // operation on a vector with 64-bit elements.
17324 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17325 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17326 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17327 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17328 SDValue N00 = N0.getOperand(0);
17329
17330 // EXTLOAD has a better solution on AVX2,
17331 // it may be replaced with X86ISD::VSEXT node.
17332 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17333 if (!ISD::isNormalLoad(N00.getNode()))
17334 return SDValue();
17335
17336 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17337 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17338 N00, N1);
17339 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17340 }
17341 }
17342 return SDValue();
17343}
17344
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017345static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17346 TargetLowering::DAGCombinerInfo &DCI,
17347 const X86Subtarget *Subtarget) {
17348 if (!DCI.isBeforeLegalizeOps())
17349 return SDValue();
17350
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017351 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017352 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017353
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017354 EVT VT = N->getValueType(0);
17355 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017356 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17357 if (R.getNode())
17358 return R;
17359 }
17360
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017361 return SDValue();
17362}
17363
Michael Liaof6c24ee2012-08-10 14:39:24 +000017364static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017365 const X86Subtarget* Subtarget) {
17366 DebugLoc dl = N->getDebugLoc();
17367 EVT VT = N->getValueType(0);
17368
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017369 // Let legalize expand this if it isn't a legal type yet.
17370 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17371 return SDValue();
17372
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017373 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017374 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17375 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017376 return SDValue();
17377
17378 SDValue A = N->getOperand(0);
17379 SDValue B = N->getOperand(1);
17380 SDValue C = N->getOperand(2);
17381
17382 bool NegA = (A.getOpcode() == ISD::FNEG);
17383 bool NegB = (B.getOpcode() == ISD::FNEG);
17384 bool NegC = (C.getOpcode() == ISD::FNEG);
17385
Michael Liaof6c24ee2012-08-10 14:39:24 +000017386 // Negative multiplication when NegA xor NegB
17387 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017388 if (NegA)
17389 A = A.getOperand(0);
17390 if (NegB)
17391 B = B.getOperand(0);
17392 if (NegC)
17393 C = C.getOperand(0);
17394
17395 unsigned Opcode;
17396 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017397 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017398 else
Craig Topperbf404372012-08-31 15:40:30 +000017399 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17400
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017401 return DAG.getNode(Opcode, dl, VT, A, B, C);
17402}
17403
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017404static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017405 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017406 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017407 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17408 // (and (i32 x86isd::setcc_carry), 1)
17409 // This eliminates the zext. This transformation is necessary because
17410 // ISD::SETCC is always legalized to i8.
17411 DebugLoc dl = N->getDebugLoc();
17412 SDValue N0 = N->getOperand(0);
17413 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017414
Evan Cheng2e489c42009-12-16 00:53:11 +000017415 if (N0.getOpcode() == ISD::AND &&
17416 N0.hasOneUse() &&
17417 N0.getOperand(0).hasOneUse()) {
17418 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017419 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17420 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17421 if (!C || C->getZExtValue() != 1)
17422 return SDValue();
17423 return DAG.getNode(ISD::AND, dl, VT,
17424 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17425 N00.getOperand(0), N00.getOperand(1)),
17426 DAG.getConstant(1, VT));
17427 }
17428 }
17429
Craig Topper5a529e42013-01-18 06:44:29 +000017430 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017431 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17432 if (R.getNode())
17433 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017434 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017435
Evan Cheng2e489c42009-12-16 00:53:11 +000017436 return SDValue();
17437}
17438
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017439// Optimize x == -y --> x+y == 0
17440// x != -y --> x+y != 0
17441static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17442 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17443 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017444 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017445
17446 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17448 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17449 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17450 LHS.getValueType(), RHS, LHS.getOperand(1));
17451 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17452 addV, DAG.getConstant(0, addV.getValueType()), CC);
17453 }
17454 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17456 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17457 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17458 RHS.getValueType(), LHS, RHS.getOperand(1));
17459 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17460 addV, DAG.getConstant(0, addV.getValueType()), CC);
17461 }
17462 return SDValue();
17463}
17464
Eric Christophere187e252013-01-31 00:50:48 +000017465// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17466// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017467// an all-ones bit which is more useful than 0/1 in some cases.
17468static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17469 return DAG.getNode(ISD::AND, DL, MVT::i8,
17470 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17471 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17472 DAG.getConstant(1, MVT::i8));
17473}
17474
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017475// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017476static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17477 TargetLowering::DAGCombinerInfo &DCI,
17478 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017479 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017480 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17481 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017482
Shuxin Yanga5526a92012-10-31 23:11:48 +000017483 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017484 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017485 // materializing "setb reg".
17486 //
17487 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17488 // cannot take an immediate as its first operand.
17489 //
Eric Christophere187e252013-01-31 00:50:48 +000017490 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017491 EFLAGS.getValueType().isInteger() &&
17492 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17493 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17494 EFLAGS.getNode()->getVTList(),
17495 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17496 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17497 return MaterializeSETB(DL, NewEFLAGS, DAG);
17498 }
17499 }
17500
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017501 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17502 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17503 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017504 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017505 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017506
Michael Liao2a33cec2012-08-10 19:58:13 +000017507 SDValue Flags;
17508
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017509 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17510 if (Flags.getNode()) {
17511 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17512 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17513 }
17514
Michael Liao2a33cec2012-08-10 19:58:13 +000017515 return SDValue();
17516}
17517
17518// Optimize branch condition evaluation.
17519//
17520static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17521 TargetLowering::DAGCombinerInfo &DCI,
17522 const X86Subtarget *Subtarget) {
17523 DebugLoc DL = N->getDebugLoc();
17524 SDValue Chain = N->getOperand(0);
17525 SDValue Dest = N->getOperand(1);
17526 SDValue EFLAGS = N->getOperand(3);
17527 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17528
17529 SDValue Flags;
17530
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017531 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17532 if (Flags.getNode()) {
17533 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17534 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17535 Flags);
17536 }
17537
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017538 return SDValue();
17539}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017540
Benjamin Kramer1396c402011-06-18 11:09:41 +000017541static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17542 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017543 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017544 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017545
17546 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017547 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017548 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017549 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017550 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17551 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17552 }
17553
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017554 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17555 // a 32-bit target where SSE doesn't support i64->FP operations.
17556 if (Op0.getOpcode() == ISD::LOAD) {
17557 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17558 EVT VT = Ld->getValueType(0);
17559 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17560 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17561 !XTLI->getSubtarget()->is64Bit() &&
17562 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017563 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17564 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017565 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17566 return FILDChain;
17567 }
17568 }
17569 return SDValue();
17570}
17571
Chris Lattner23a01992010-12-20 01:37:09 +000017572// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17573static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17574 X86TargetLowering::DAGCombinerInfo &DCI) {
17575 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17576 // the result is either zero or one (depending on the input carry bit).
17577 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17578 if (X86::isZeroNode(N->getOperand(0)) &&
17579 X86::isZeroNode(N->getOperand(1)) &&
17580 // We don't have a good way to replace an EFLAGS use, so only do this when
17581 // dead right now.
17582 SDValue(N, 1).use_empty()) {
17583 DebugLoc DL = N->getDebugLoc();
17584 EVT VT = N->getValueType(0);
17585 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17586 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17587 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17588 DAG.getConstant(X86::COND_B,MVT::i8),
17589 N->getOperand(2)),
17590 DAG.getConstant(1, VT));
17591 return DCI.CombineTo(N, Res1, CarryOut);
17592 }
17593
17594 return SDValue();
17595}
17596
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017597// fold (add Y, (sete X, 0)) -> adc 0, Y
17598// (add Y, (setne X, 0)) -> sbb -1, Y
17599// (sub (sete X, 0), Y) -> sbb 0, Y
17600// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017601static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017602 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017603
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017604 // Look through ZExts.
17605 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17606 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17607 return SDValue();
17608
17609 SDValue SetCC = Ext.getOperand(0);
17610 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17611 return SDValue();
17612
17613 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17614 if (CC != X86::COND_E && CC != X86::COND_NE)
17615 return SDValue();
17616
17617 SDValue Cmp = SetCC.getOperand(1);
17618 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017619 !X86::isZeroNode(Cmp.getOperand(1)) ||
17620 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017621 return SDValue();
17622
17623 SDValue CmpOp0 = Cmp.getOperand(0);
17624 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17625 DAG.getConstant(1, CmpOp0.getValueType()));
17626
17627 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17628 if (CC == X86::COND_NE)
17629 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17630 DL, OtherVal.getValueType(), OtherVal,
17631 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17632 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17633 DL, OtherVal.getValueType(), OtherVal,
17634 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17635}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017636
Craig Topper54f952a2011-11-19 09:02:40 +000017637/// PerformADDCombine - Do target-specific dag combines on integer adds.
17638static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17639 const X86Subtarget *Subtarget) {
17640 EVT VT = N->getValueType(0);
17641 SDValue Op0 = N->getOperand(0);
17642 SDValue Op1 = N->getOperand(1);
17643
17644 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017645 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017646 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017647 isHorizontalBinOp(Op0, Op1, true))
17648 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17649
17650 return OptimizeConditionalInDecrement(N, DAG);
17651}
17652
17653static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17654 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017655 SDValue Op0 = N->getOperand(0);
17656 SDValue Op1 = N->getOperand(1);
17657
17658 // X86 can't encode an immediate LHS of a sub. See if we can push the
17659 // negation into a preceding instruction.
17660 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017661 // If the RHS of the sub is a XOR with one use and a constant, invert the
17662 // immediate. Then add one to the LHS of the sub so we can turn
17663 // X-Y -> X+~Y+1, saving one register.
17664 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17665 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017666 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017667 EVT VT = Op0.getValueType();
17668 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17669 Op1.getOperand(0),
17670 DAG.getConstant(~XorC, VT));
17671 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017672 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017673 }
17674 }
17675
Craig Topper54f952a2011-11-19 09:02:40 +000017676 // Try to synthesize horizontal adds from adds of shuffles.
17677 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017678 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017679 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017680 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017681 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17682
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017683 return OptimizeConditionalInDecrement(N, DAG);
17684}
17685
Michael Liaod9d09602012-10-23 17:34:00 +000017686/// performVZEXTCombine - Performs build vector combines
17687static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17688 TargetLowering::DAGCombinerInfo &DCI,
17689 const X86Subtarget *Subtarget) {
17690 // (vzext (bitcast (vzext (x)) -> (vzext x)
17691 SDValue In = N->getOperand(0);
17692 while (In.getOpcode() == ISD::BITCAST)
17693 In = In.getOperand(0);
17694
17695 if (In.getOpcode() != X86ISD::VZEXT)
17696 return SDValue();
17697
Nadav Rotemb39a5522013-02-14 18:20:48 +000017698 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17699 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017700}
17701
Dan Gohman475871a2008-07-27 21:46:04 +000017702SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017703 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017704 SelectionDAG &DAG = DCI.DAG;
17705 switch (N->getOpcode()) {
17706 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017707 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017708 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017709 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017710 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017711 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017712 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17713 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017714 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017715 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017716 case ISD::SHL:
17717 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017718 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017719 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017720 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017721 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017722 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017723 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017724 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017725 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17726 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017727 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017728 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017729 case X86ISD::FMIN:
17730 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017731 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017732 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017733 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017734 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017735 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017736 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017737 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017738 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017739 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017740 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017741 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017742 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017743 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017744 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017745 case X86ISD::UNPCKH:
17746 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017747 case X86ISD::MOVHLPS:
17748 case X86ISD::MOVLHPS:
17749 case X86ISD::PSHUFD:
17750 case X86ISD::PSHUFHW:
17751 case X86ISD::PSHUFLW:
17752 case X86ISD::MOVSS:
17753 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017754 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017755 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017756 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017757 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017758 }
17759
Dan Gohman475871a2008-07-27 21:46:04 +000017760 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017761}
17762
Evan Chenge5b51ac2010-04-17 06:13:15 +000017763/// isTypeDesirableForOp - Return true if the target has native support for
17764/// the specified value type and it is 'desirable' to use the type for the
17765/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17766/// instruction encodings are longer and some i16 instructions are slow.
17767bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17768 if (!isTypeLegal(VT))
17769 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017770 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017771 return true;
17772
17773 switch (Opc) {
17774 default:
17775 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017776 case ISD::LOAD:
17777 case ISD::SIGN_EXTEND:
17778 case ISD::ZERO_EXTEND:
17779 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017780 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017781 case ISD::SRL:
17782 case ISD::SUB:
17783 case ISD::ADD:
17784 case ISD::MUL:
17785 case ISD::AND:
17786 case ISD::OR:
17787 case ISD::XOR:
17788 return false;
17789 }
17790}
17791
17792/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017793/// beneficial for dag combiner to promote the specified node. If true, it
17794/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017795bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017796 EVT VT = Op.getValueType();
17797 if (VT != MVT::i16)
17798 return false;
17799
Evan Cheng4c26e932010-04-19 19:29:22 +000017800 bool Promote = false;
17801 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017802 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017803 default: break;
17804 case ISD::LOAD: {
17805 LoadSDNode *LD = cast<LoadSDNode>(Op);
17806 // If the non-extending load has a single use and it's not live out, then it
17807 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017808 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17809 Op.hasOneUse()*/) {
17810 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17811 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17812 // The only case where we'd want to promote LOAD (rather then it being
17813 // promoted as an operand is when it's only use is liveout.
17814 if (UI->getOpcode() != ISD::CopyToReg)
17815 return false;
17816 }
17817 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017818 Promote = true;
17819 break;
17820 }
17821 case ISD::SIGN_EXTEND:
17822 case ISD::ZERO_EXTEND:
17823 case ISD::ANY_EXTEND:
17824 Promote = true;
17825 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017826 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017827 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017828 SDValue N0 = Op.getOperand(0);
17829 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017830 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017831 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017832 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017833 break;
17834 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017835 case ISD::ADD:
17836 case ISD::MUL:
17837 case ISD::AND:
17838 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017839 case ISD::XOR:
17840 Commute = true;
17841 // fallthrough
17842 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017843 SDValue N0 = Op.getOperand(0);
17844 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017845 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017846 return false;
17847 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017848 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017849 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017850 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017851 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017852 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017853 }
17854 }
17855
17856 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017857 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017858}
17859
Evan Cheng60c07e12006-07-05 22:17:51 +000017860//===----------------------------------------------------------------------===//
17861// X86 Inline Assembly Support
17862//===----------------------------------------------------------------------===//
17863
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017864namespace {
17865 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017866 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017867 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017868
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017869 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017870 StringRef piece(*args[i]);
17871 if (!s.startswith(piece)) // Check if the piece matches.
17872 return false;
17873
17874 s = s.substr(piece.size());
17875 StringRef::size_type pos = s.find_first_not_of(" \t");
17876 if (pos == 0) // We matched a prefix.
17877 return false;
17878
17879 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017880 }
17881
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017882 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017883 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017884 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017885}
17886
Chris Lattnerb8105652009-07-20 17:51:36 +000017887bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17888 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017889
17890 std::string AsmStr = IA->getAsmString();
17891
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017892 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17893 if (!Ty || Ty->getBitWidth() % 16 != 0)
17894 return false;
17895
Chris Lattnerb8105652009-07-20 17:51:36 +000017896 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017897 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017898 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017899
17900 switch (AsmPieces.size()) {
17901 default: return false;
17902 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017903 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017904 // we will turn this bswap into something that will be lowered to logical
17905 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17906 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017907 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017908 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17909 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17910 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17911 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17912 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17913 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017914 // No need to check constraints, nothing other than the equivalent of
17915 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017916 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017917 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017918
Chris Lattnerb8105652009-07-20 17:51:36 +000017919 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017920 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017921 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017922 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17923 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017924 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017925 const std::string &ConstraintsStr = IA->getConstraintString();
17926 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017927 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000017928 if (AsmPieces.size() == 4 &&
17929 AsmPieces[0] == "~{cc}" &&
17930 AsmPieces[1] == "~{dirflag}" &&
17931 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017932 AsmPieces[3] == "~{fpsr}")
17933 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017934 }
17935 break;
17936 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017937 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017938 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017939 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17940 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17941 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017942 AsmPieces.clear();
17943 const std::string &ConstraintsStr = IA->getConstraintString();
17944 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017945 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017946 if (AsmPieces.size() == 4 &&
17947 AsmPieces[0] == "~{cc}" &&
17948 AsmPieces[1] == "~{dirflag}" &&
17949 AsmPieces[2] == "~{flags}" &&
17950 AsmPieces[3] == "~{fpsr}")
17951 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017952 }
Evan Cheng55d42002011-01-08 01:24:27 +000017953
17954 if (CI->getType()->isIntegerTy(64)) {
17955 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17956 if (Constraints.size() >= 2 &&
17957 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17958 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17959 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017960 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17961 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17962 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017963 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017964 }
17965 }
17966 break;
17967 }
17968 return false;
17969}
17970
Chris Lattnerf4dff842006-07-11 02:54:03 +000017971/// getConstraintType - Given a constraint letter, return the type of
17972/// constraint it is for this target.
17973X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017974X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17975 if (Constraint.size() == 1) {
17976 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017977 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017978 case 'q':
17979 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017980 case 'f':
17981 case 't':
17982 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017983 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017984 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017985 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017986 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017987 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017988 case 'a':
17989 case 'b':
17990 case 'c':
17991 case 'd':
17992 case 'S':
17993 case 'D':
17994 case 'A':
17995 return C_Register;
17996 case 'I':
17997 case 'J':
17998 case 'K':
17999 case 'L':
18000 case 'M':
18001 case 'N':
18002 case 'G':
18003 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018004 case 'e':
18005 case 'Z':
18006 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018007 default:
18008 break;
18009 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018010 }
Chris Lattner4234f572007-03-25 02:14:49 +000018011 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018012}
18013
John Thompson44ab89e2010-10-29 17:29:13 +000018014/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018015/// This object must already have been set up with the operand type
18016/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018017TargetLowering::ConstraintWeight
18018 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018019 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018020 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018021 Value *CallOperandVal = info.CallOperandVal;
18022 // If we don't have a value, we can't do a match,
18023 // but allow it at the lowest weight.
18024 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018025 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018026 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018027 // Look at the constraint type.
18028 switch (*constraint) {
18029 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018030 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18031 case 'R':
18032 case 'q':
18033 case 'Q':
18034 case 'a':
18035 case 'b':
18036 case 'c':
18037 case 'd':
18038 case 'S':
18039 case 'D':
18040 case 'A':
18041 if (CallOperandVal->getType()->isIntegerTy())
18042 weight = CW_SpecificReg;
18043 break;
18044 case 'f':
18045 case 't':
18046 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018047 if (type->isFloatingPointTy())
18048 weight = CW_SpecificReg;
18049 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018050 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018051 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18052 weight = CW_SpecificReg;
18053 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018054 case 'x':
18055 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018056 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018057 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018058 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018059 break;
18060 case 'I':
18061 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18062 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018063 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018064 }
18065 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018066 case 'J':
18067 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18068 if (C->getZExtValue() <= 63)
18069 weight = CW_Constant;
18070 }
18071 break;
18072 case 'K':
18073 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18074 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18075 weight = CW_Constant;
18076 }
18077 break;
18078 case 'L':
18079 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18080 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18081 weight = CW_Constant;
18082 }
18083 break;
18084 case 'M':
18085 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18086 if (C->getZExtValue() <= 3)
18087 weight = CW_Constant;
18088 }
18089 break;
18090 case 'N':
18091 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18092 if (C->getZExtValue() <= 0xff)
18093 weight = CW_Constant;
18094 }
18095 break;
18096 case 'G':
18097 case 'C':
18098 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18099 weight = CW_Constant;
18100 }
18101 break;
18102 case 'e':
18103 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18104 if ((C->getSExtValue() >= -0x80000000LL) &&
18105 (C->getSExtValue() <= 0x7fffffffLL))
18106 weight = CW_Constant;
18107 }
18108 break;
18109 case 'Z':
18110 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18111 if (C->getZExtValue() <= 0xffffffff)
18112 weight = CW_Constant;
18113 }
18114 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018115 }
18116 return weight;
18117}
18118
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018119/// LowerXConstraint - try to replace an X constraint, which matches anything,
18120/// with another that has more specific requirements based on the type of the
18121/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018122const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018123LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018124 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18125 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018126 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018127 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018128 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018129 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018130 return "x";
18131 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018132
Chris Lattner5e764232008-04-26 23:02:14 +000018133 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018134}
18135
Chris Lattner48884cd2007-08-25 00:47:38 +000018136/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18137/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018138void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018139 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018140 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018141 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018142 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018143
Eric Christopher100c8332011-06-02 23:16:42 +000018144 // Only support length 1 constraints for now.
18145 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018146
Eric Christopher100c8332011-06-02 23:16:42 +000018147 char ConstraintLetter = Constraint[0];
18148 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018149 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018150 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018151 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018152 if (C->getZExtValue() <= 31) {
18153 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018154 break;
18155 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018156 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018157 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018158 case 'J':
18159 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018160 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018161 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18162 break;
18163 }
18164 }
18165 return;
18166 case 'K':
18167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018168 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018169 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18170 break;
18171 }
18172 }
18173 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018174 case 'N':
18175 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018176 if (C->getZExtValue() <= 255) {
18177 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018178 break;
18179 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018180 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018181 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018182 case 'e': {
18183 // 32-bit signed value
18184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018185 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18186 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018187 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018188 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018189 break;
18190 }
18191 // FIXME gcc accepts some relocatable values here too, but only in certain
18192 // memory models; it's complicated.
18193 }
18194 return;
18195 }
18196 case 'Z': {
18197 // 32-bit unsigned value
18198 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018199 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18200 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018201 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18202 break;
18203 }
18204 }
18205 // FIXME gcc accepts some relocatable values here too, but only in certain
18206 // memory models; it's complicated.
18207 return;
18208 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018209 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018210 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018211 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018212 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018213 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018214 break;
18215 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018216
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018217 // In any sort of PIC mode addresses need to be computed at runtime by
18218 // adding in a register or some sort of table lookup. These can't
18219 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018220 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018221 return;
18222
Chris Lattnerdc43a882007-05-03 16:52:29 +000018223 // If we are in non-pic codegen mode, we allow the address of a global (with
18224 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018225 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018226 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018227
Chris Lattner49921962009-05-08 18:23:14 +000018228 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18229 while (1) {
18230 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18231 Offset += GA->getOffset();
18232 break;
18233 } else if (Op.getOpcode() == ISD::ADD) {
18234 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18235 Offset += C->getZExtValue();
18236 Op = Op.getOperand(0);
18237 continue;
18238 }
18239 } else if (Op.getOpcode() == ISD::SUB) {
18240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18241 Offset += -C->getZExtValue();
18242 Op = Op.getOperand(0);
18243 continue;
18244 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018245 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018246
Chris Lattner49921962009-05-08 18:23:14 +000018247 // Otherwise, this isn't something we can handle, reject it.
18248 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018249 }
Eric Christopherfd179292009-08-27 18:07:15 +000018250
Dan Gohman46510a72010-04-15 01:51:59 +000018251 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018252 // If we require an extra load to get this address, as in PIC mode, we
18253 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018254 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18255 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018256 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018257
Devang Patel0d881da2010-07-06 22:08:15 +000018258 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18259 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018260 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018261 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018262 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018263
Gabor Greifba36cb52008-08-28 21:40:38 +000018264 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018265 Ops.push_back(Result);
18266 return;
18267 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018268 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018269}
18270
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018271std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018272X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018273 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018274 // First, see if this is a constraint that directly corresponds to an LLVM
18275 // register class.
18276 if (Constraint.size() == 1) {
18277 // GCC Constraint Letters
18278 switch (Constraint[0]) {
18279 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018280 // TODO: Slight differences here in allocation order and leaving
18281 // RIP in the class. Do they matter any more here than they do
18282 // in the normal allocation?
18283 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18284 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018285 if (VT == MVT::i32 || VT == MVT::f32)
18286 return std::make_pair(0U, &X86::GR32RegClass);
18287 if (VT == MVT::i16)
18288 return std::make_pair(0U, &X86::GR16RegClass);
18289 if (VT == MVT::i8 || VT == MVT::i1)
18290 return std::make_pair(0U, &X86::GR8RegClass);
18291 if (VT == MVT::i64 || VT == MVT::f64)
18292 return std::make_pair(0U, &X86::GR64RegClass);
18293 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018294 }
18295 // 32-bit fallthrough
18296 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018297 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018298 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18299 if (VT == MVT::i16)
18300 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18301 if (VT == MVT::i8 || VT == MVT::i1)
18302 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18303 if (VT == MVT::i64)
18304 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018305 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018306 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018307 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018308 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018309 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018310 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018311 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018312 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018313 return std::make_pair(0U, &X86::GR32RegClass);
18314 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018315 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018316 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018317 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018318 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018319 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018320 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018321 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18322 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018323 case 'f': // FP Stack registers.
18324 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18325 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018326 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018327 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018328 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018329 return std::make_pair(0U, &X86::RFP64RegClass);
18330 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018331 case 'y': // MMX_REGS if MMX allowed.
18332 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018333 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018334 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018335 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018336 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018337 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018338 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018339
Owen Anderson825b72b2009-08-11 20:47:22 +000018340 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018341 default: break;
18342 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018343 case MVT::f32:
18344 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018345 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018346 case MVT::f64:
18347 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018348 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018349 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018350 case MVT::v16i8:
18351 case MVT::v8i16:
18352 case MVT::v4i32:
18353 case MVT::v2i64:
18354 case MVT::v4f32:
18355 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018356 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018357 // AVX types.
18358 case MVT::v32i8:
18359 case MVT::v16i16:
18360 case MVT::v8i32:
18361 case MVT::v4i64:
18362 case MVT::v8f32:
18363 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018364 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018365 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018366 break;
18367 }
18368 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018369
Chris Lattnerf76d1802006-07-31 23:26:50 +000018370 // Use the default implementation in TargetLowering to convert the register
18371 // constraint into a member of a register class.
18372 std::pair<unsigned, const TargetRegisterClass*> Res;
18373 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018374
18375 // Not found as a standard register?
18376 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018377 // Map st(0) -> st(7) -> ST0
18378 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18379 tolower(Constraint[1]) == 's' &&
18380 tolower(Constraint[2]) == 't' &&
18381 Constraint[3] == '(' &&
18382 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18383 Constraint[5] == ')' &&
18384 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018385
Chris Lattner56d77c72009-09-13 22:41:48 +000018386 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018387 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018388 return Res;
18389 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018390
Chris Lattner56d77c72009-09-13 22:41:48 +000018391 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018392 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018393 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018394 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018395 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018396 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018397
18398 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018399 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018400 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018401 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018402 return Res;
18403 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018404
Dale Johannesen330169f2008-11-13 21:52:36 +000018405 // 'A' means EAX + EDX.
18406 if (Constraint == "A") {
18407 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018408 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018409 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018410 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018411 return Res;
18412 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018413
Chris Lattnerf76d1802006-07-31 23:26:50 +000018414 // Otherwise, check to see if this is a register class of the wrong value
18415 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18416 // turn into {ax},{dx}.
18417 if (Res.second->hasType(VT))
18418 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018419
Chris Lattnerf76d1802006-07-31 23:26:50 +000018420 // All of the single-register GCC register classes map their values onto
18421 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18422 // really want an 8-bit or 32-bit register, map to the appropriate register
18423 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018424 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018425 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018426 unsigned DestReg = 0;
18427 switch (Res.first) {
18428 default: break;
18429 case X86::AX: DestReg = X86::AL; break;
18430 case X86::DX: DestReg = X86::DL; break;
18431 case X86::CX: DestReg = X86::CL; break;
18432 case X86::BX: DestReg = X86::BL; break;
18433 }
18434 if (DestReg) {
18435 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018436 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018437 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018438 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018439 unsigned DestReg = 0;
18440 switch (Res.first) {
18441 default: break;
18442 case X86::AX: DestReg = X86::EAX; break;
18443 case X86::DX: DestReg = X86::EDX; break;
18444 case X86::CX: DestReg = X86::ECX; break;
18445 case X86::BX: DestReg = X86::EBX; break;
18446 case X86::SI: DestReg = X86::ESI; break;
18447 case X86::DI: DestReg = X86::EDI; break;
18448 case X86::BP: DestReg = X86::EBP; break;
18449 case X86::SP: DestReg = X86::ESP; break;
18450 }
18451 if (DestReg) {
18452 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018453 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018454 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018455 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018456 unsigned DestReg = 0;
18457 switch (Res.first) {
18458 default: break;
18459 case X86::AX: DestReg = X86::RAX; break;
18460 case X86::DX: DestReg = X86::RDX; break;
18461 case X86::CX: DestReg = X86::RCX; break;
18462 case X86::BX: DestReg = X86::RBX; break;
18463 case X86::SI: DestReg = X86::RSI; break;
18464 case X86::DI: DestReg = X86::RDI; break;
18465 case X86::BP: DestReg = X86::RBP; break;
18466 case X86::SP: DestReg = X86::RSP; break;
18467 }
18468 if (DestReg) {
18469 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018470 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018471 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018472 }
Craig Topperc9099502012-04-20 06:31:50 +000018473 } else if (Res.second == &X86::FR32RegClass ||
18474 Res.second == &X86::FR64RegClass ||
18475 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018476 // Handle references to XMM physical registers that got mapped into the
18477 // wrong class. This can happen with constraints like {xmm0} where the
18478 // target independent register mapper will just pick the first match it can
18479 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018480
18481 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018482 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018483 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018484 Res.second = &X86::FR64RegClass;
18485 else if (X86::VR128RegClass.hasType(VT))
18486 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018487 else if (X86::VR256RegClass.hasType(VT))
18488 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018489 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018490
Chris Lattnerf76d1802006-07-31 23:26:50 +000018491 return Res;
18492}