blob: b7a9f6f3179084d3a4e81d8554edd634b8816d32 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000164 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Preston Gurd2e2efd92012-09-04 18:22:17 +0000185 // Bypass i32 with i8 on Atom when compiling with O2
186 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000187 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000188
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000189 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000190 // Setup Windows compiler runtime calls.
191 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000192 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000193 setLibcallName(RTLIB::SREM_I64, "_allrem");
194 setLibcallName(RTLIB::UREM_I64, "_aullrem");
195 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000201
202 // The _ftol2 runtime function has an unusual calling conv, which
203 // is modeled by a special pseudo-instruction.
204 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
206 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i8, &X86::GR8RegClass);
225 addRegisterClass(MVT::i16, &X86::GR16RegClass);
226 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000228 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000257 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000271 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000314 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000315 // Since AVX is a superset of SSE3, only check for SSE here.
316 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000327 if (isTargetFTOL()) {
328 // Use the _ftol2 runtime function, which has a pseudo-instruction
329 // to handle its weird calling convention.
330 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
331 }
332
Chris Lattner399610a2006-12-05 18:22:22 +0000333 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000334 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
336 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000337 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000338 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000339 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000341 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000342 }
Chris Lattner21f66852005-12-23 05:15:23 +0000343
Dan Gohmanb00ee212008-02-18 19:34:53 +0000344 // Scalar integer divide and remainder are lowered to use operations that
345 // produce two results, to match the available instructions. This exposes
346 // the two-result form to trivial CSE, which is able to combine x/y and x%y
347 // into a single instruction.
348 //
349 // Scalar integer multiply-high is also lowered to use two-result
350 // operations, to match the available instructions. However, plain multiply
351 // (low) operations are left as Legal, as there are single-result
352 // instructions for this in x86. Using the two-result multiply instructions
353 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000354 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000355 MVT VT = IntVTs[i];
356 setOperationAction(ISD::MULHS, VT, Expand);
357 setOperationAction(ISD::MULHU, VT, Expand);
358 setOperationAction(ISD::SDIV, VT, Expand);
359 setOperationAction(ISD::UDIV, VT, Expand);
360 setOperationAction(ISD::SREM, VT, Expand);
361 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000363 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000364 setOperationAction(ISD::ADDC, VT, Custom);
365 setOperationAction(ISD::ADDE, VT, Custom);
366 setOperationAction(ISD::SUBC, VT, Custom);
367 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000368 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000369
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
371 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
372 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
373 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000374 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
379 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f32 , Expand);
381 setOperationAction(ISD::FREM , MVT::f64 , Expand);
382 setOperationAction(ISD::FREM , MVT::f80 , Expand);
383 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000384
Chandler Carruth77821022011-12-24 12:12:34 +0000385 // Promote the i8 variants and force them on up to i32 which has a shorter
386 // encoding.
387 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
388 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000391 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
393 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
394 if (Subtarget->is64Bit())
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000396 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000397 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
398 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
401 }
Craig Topper37f21672011-10-11 06:44:02 +0000402
403 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000404 // When promoting the i8 variants, force them to i32 for a shorter
405 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000406 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000407 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
409 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000414 } else {
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
421 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 }
426
Benjamin Kramer1292c222010-12-04 20:32:23 +0000427 if (Subtarget->hasPOPCNT()) {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
429 } else {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
433 if (Subtarget->is64Bit())
434 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
435 }
436
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
438 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000444 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000450 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000457 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000460
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000461 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
468 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000469 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
472 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
473 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
474 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000475 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000476 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000477 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486
Craig Topper1accb7e2012-01-10 06:54:16 +0000487 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000489
Eric Christopher9a9d2752010-07-22 02:48:34 +0000490 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000491 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000492
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000493 // On X86 and X86-64, atomic operations are lowered to locked instructions.
494 // Locked instructions, in turn, have implicit fence semantics (all memory
495 // operations are flushed before issuing the locked instruction, and they
496 // are not buffered), so we can fold away the common pattern of
497 // fence-atomic-fence.
498 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000499
Mon P Wang63307c32008-05-05 19:05:59 +0000500 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000501 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 MVT VT = IntVTs[i];
503 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000505 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000509 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000517 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000521 }
522
Eli Friedman43f51ae2011-08-26 21:21:21 +0000523 if (Subtarget->hasCmpxchg16b()) {
524 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
525 }
526
Evan Cheng3c992d22006-03-07 02:02:57 +0000527 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000528 if (!Subtarget->isTargetDarwin() &&
529 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000530 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000532 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
535 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
536 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
537 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000538 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000539 setExceptionPointerRegister(X86::RAX);
540 setExceptionSelectorRegister(X86::RDX);
541 } else {
542 setExceptionPointerRegister(X86::EAX);
543 setExceptionSelectorRegister(X86::EDX);
544 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
546 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000547
Duncan Sands4a544a72011-09-06 13:37:06 +0000548 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
549 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000552
Nate Begemanacc398c2006-01-25 18:21:52 +0000553 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::VASTART , MVT::Other, Custom);
555 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000556 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::VAARG , MVT::Other, Custom);
558 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000559 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::VAARG , MVT::Other, Expand);
561 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000562 }
Evan Chengae642192007-03-02 23:16:35 +0000563
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
565 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000566
567 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
568 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
569 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000570 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000571 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
572 MVT::i64 : MVT::i32, Custom);
573 else
574 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
575 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000576
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000577 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000579 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000580 addRegisterClass(MVT::f32, &X86::FR32RegClass);
581 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Evan Cheng223547a2006-01-31 22:28:30 +0000583 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FABS , MVT::f64, Custom);
585 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
587 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FNEG , MVT::f64, Custom);
589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000590
Evan Cheng68c47cb2007-01-05 07:55:56 +0000591 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
593 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000594
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000595 // Lower this to FGETSIGNx86 plus an AND.
596 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
597 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
598
Evan Chengd25e9e82006-02-02 00:28:23 +0000599 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64, Expand);
601 setOperationAction(ISD::FCOS , MVT::f64, Expand);
602 setOperationAction(ISD::FSIN , MVT::f32, Expand);
603 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604
Chris Lattnera54aa942006-01-29 06:26:08 +0000605 // Expand FP immediates into loads from the stack, except for the special
606 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607 addLegalFPImmediate(APFloat(+0.0)); // xorpd
608 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000609 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 // Use SSE for f32, x87 for f64.
611 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000612 addRegisterClass(MVT::f32, &X86::FR32RegClass);
613 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617
618 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
625 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
627 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::FSIN , MVT::f32, Expand);
629 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
Nate Begemane1795842008-02-14 08:57:00 +0000631 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // xorps
633 addLegalFPImmediate(APFloat(+0.0)); // FLD0
634 addLegalFPImmediate(APFloat(+1.0)); // FLD1
635 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
636 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
637
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
640 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000642 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000644 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000645 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
646 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
649 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
650 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
651 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000652
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000653 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000654 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000656 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000658 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000659 addLegalFPImmediate(APFloat(+0.0)); // FLD0
660 addLegalFPImmediate(APFloat(+1.0)); // FLD1
661 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
662 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000663 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
664 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
665 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
666 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000667 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000668
Cameron Zwarich33390842011-07-08 21:39:21 +0000669 // We don't support FMA.
670 setOperationAction(ISD::FMA, MVT::f64, Expand);
671 setOperationAction(ISD::FMA, MVT::f32, Expand);
672
Dale Johannesen59a58732007-08-05 18:49:15 +0000673 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000674 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000675 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
677 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000678 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000679 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000680 addLegalFPImmediate(TmpFlt); // FLD0
681 TmpFlt.changeSign();
682 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000683
684 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000685 APFloat TmpFlt2(+1.0);
686 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
687 &ignored);
688 addLegalFPImmediate(TmpFlt2); // FLD1
689 TmpFlt2.changeSign();
690 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
691 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000692
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000693 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
695 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000696 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000697
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000698 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
699 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
700 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
701 setOperationAction(ISD::FRINT, MVT::f80, Expand);
702 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000703 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000704 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000705
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000706 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
708 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
709 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000710
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::FLOG, MVT::f80, Expand);
712 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
713 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
714 setOperationAction(ISD::FEXP, MVT::f80, Expand);
715 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000716
Mon P Wangf007a8b2008-11-06 05:31:54 +0000717 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000718 // (for widening) or expand (for scalarization). Then we will selectively
719 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000720 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
721 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000738 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
739 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000744 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000748 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000756 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000758 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000765 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
774 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000775 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000776 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
777 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
778 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
779 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000780 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000781 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
782 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000783 setTruncStoreAction((MVT::SimpleValueType)VT,
784 (MVT::SimpleValueType)InnerVT, Expand);
785 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
786 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
787 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000788 }
789
Evan Chengc7ce29b2009-02-13 22:36:38 +0000790 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
791 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000792 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000793 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000794 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Dale Johannesen0488fb62010-09-30 23:57:10 +0000797 // MMX-sized vectors (other than x86mmx) are expected to be expanded
798 // into smaller operations.
799 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
800 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
801 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
802 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
803 setOperationAction(ISD::AND, MVT::v8i8, Expand);
804 setOperationAction(ISD::AND, MVT::v4i16, Expand);
805 setOperationAction(ISD::AND, MVT::v2i32, Expand);
806 setOperationAction(ISD::AND, MVT::v1i64, Expand);
807 setOperationAction(ISD::OR, MVT::v8i8, Expand);
808 setOperationAction(ISD::OR, MVT::v4i16, Expand);
809 setOperationAction(ISD::OR, MVT::v2i32, Expand);
810 setOperationAction(ISD::OR, MVT::v1i64, Expand);
811 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
812 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
813 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
814 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
815 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
816 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
817 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
818 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
819 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
820 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
821 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
822 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
823 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000824 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
825 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
826 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
827 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000828
Craig Topper1accb7e2012-01-10 06:54:16 +0000829 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000830 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
833 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
834 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
835 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
836 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
837 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000838 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
840 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
841 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
843 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844 }
845
Craig Topper1accb7e2012-01-10 06:54:16 +0000846 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000847 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000848
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000849 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
850 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000851 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
852 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
853 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
854 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
857 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
858 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
859 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
860 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
861 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
862 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
863 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
864 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
865 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
866 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
867 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
868 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
869 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
870 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
871 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000872 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000873
Nadav Rotem354efd82011-09-18 14:57:03 +0000874 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000875 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
876 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
877 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000878
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
880 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
881 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
882 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
883 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000884
Evan Cheng2c3ae372006-04-12 21:21:57 +0000885 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000886 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000887 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000888 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000890 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000891 // Do not attempt to custom lower non-128-bit vectors
892 if (!VT.is128BitVector())
893 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000894 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
895 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
896 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000897 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
900 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
901 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
902 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
903 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000905
Nate Begemancdd1eec2008-02-12 22:51:28 +0000906 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
908 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000909 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000911 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000912 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000914
915 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000916 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000917 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000918
Craig Topper0d1f1762012-08-12 00:34:56 +0000919 setOperationAction(ISD::AND, VT, Promote);
920 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
921 setOperationAction(ISD::OR, VT, Promote);
922 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
923 setOperationAction(ISD::XOR, VT, Promote);
924 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
925 setOperationAction(ISD::LOAD, VT, Promote);
926 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
927 setOperationAction(ISD::SELECT, VT, Promote);
928 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000929 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000930
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000932
Evan Cheng2c3ae372006-04-12 21:21:57 +0000933 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
935 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
936 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
937 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
940 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000941
Michael Liao9d796db2012-10-10 16:32:15 +0000942 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000943 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000944
Michael Liaob8150d82012-09-10 18:33:51 +0000945 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000946 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000947
Craig Topperd0a31172012-01-10 06:37:29 +0000948 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000949 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
950 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
951 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
952 setOperationAction(ISD::FRINT, MVT::f32, Legal);
953 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
954 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
955 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
956 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
957 setOperationAction(ISD::FRINT, MVT::f64, Legal);
958 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
959
Craig Topper12fb5c62012-09-08 17:42:27 +0000960 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
961 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
962
Nate Begeman14d12ca2008-02-11 04:19:36 +0000963 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000965
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000966 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
967 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
968 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
969 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
970 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000971
Nate Begeman14d12ca2008-02-11 04:19:36 +0000972 // i8 and i16 vectors are custom , because the source register and source
973 // source memory operand types are not the same width. f32 vectors are
974 // custom since the immediate controlling the insert encodes additional
975 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
977 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
978 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
979 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000980
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
982 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
983 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
984 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000985
Pete Coopera77214a2011-11-14 19:38:42 +0000986 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000987 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000988 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000989 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
990 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000991 }
992 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000993
Craig Topper1accb7e2012-01-10 06:54:16 +0000994 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000995 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000996 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000997
Nadav Rotem43012222011-05-11 08:12:09 +0000998 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000999 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001000
Nadav Rotem43012222011-05-11 08:12:09 +00001001 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001002 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001003
1004 if (Subtarget->hasAVX2()) {
1005 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1006 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1007
1008 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1009 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1010
1011 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1012 } else {
1013 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1014 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1015
1016 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1017 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1018
1019 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1020 }
Nadav Rotem43012222011-05-11 08:12:09 +00001021 }
1022
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001023 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001024 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1025 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1026 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1027 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1028 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1029 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001030
Owen Anderson825b72b2009-08-11 20:47:22 +00001031 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1033 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001034
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1036 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1037 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1038 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1039 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001040 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001042 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001043
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1045 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1046 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1047 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1048 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001049 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001051 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001052
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001053 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1054 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001055 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001056
Michael Liaob8150d82012-09-10 18:33:51 +00001057 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1058
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001059 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1060 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1061
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001062 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1063 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1064
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001065 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001066 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001067
Duncan Sands28b77e92011-09-06 19:07:46 +00001068 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1069 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1070 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1071 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001072
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001073 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1074 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1075 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1076
Craig Topperaaa643c2011-11-09 07:28:55 +00001077 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1078 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1079 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1080 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001081
Craig Topperbf404372012-08-31 15:40:30 +00001082 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001083 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1084 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1085 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1086 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1087 setOperationAction(ISD::FMA, MVT::f32, Custom);
1088 setOperationAction(ISD::FMA, MVT::f64, Custom);
1089 }
Craig Topper880ef452012-08-11 22:34:26 +00001090
Craig Topperaaa643c2011-11-09 07:28:55 +00001091 if (Subtarget->hasAVX2()) {
1092 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1093 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1094 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1095 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001096
Craig Topperaaa643c2011-11-09 07:28:55 +00001097 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1098 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1099 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1100 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001101
Craig Topperaaa643c2011-11-09 07:28:55 +00001102 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1103 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1104 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001105 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001106
1107 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001108
1109 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1110 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1111
1112 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1113 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1114
1115 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001116 } else {
1117 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1118 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1119 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1120 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1121
1122 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1123 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1124 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1125 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1126
1127 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1128 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1129 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1130 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001131
1132 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1133 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1134
1135 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1136 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1137
1138 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001139 }
Craig Topper13894fa2011-08-24 06:14:18 +00001140
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001141 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001142 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1143 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001144 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001145
1146 // Extract subvector is special because the value type
1147 // (result) is 128-bit but the source is 256-bit wide.
1148 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001149 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001150
1151 // Do not attempt to custom lower other non-256-bit vectors
1152 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001153 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001154
Craig Topper0d1f1762012-08-12 00:34:56 +00001155 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1156 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1157 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1158 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1159 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1160 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1161 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001162 }
1163
David Greene54d8eba2011-01-27 22:38:56 +00001164 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001165 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001166 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001167
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001168 // Do not attempt to promote non-256-bit vectors
1169 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001170 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001171
Craig Topper0d1f1762012-08-12 00:34:56 +00001172 setOperationAction(ISD::AND, VT, Promote);
1173 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1174 setOperationAction(ISD::OR, VT, Promote);
1175 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1176 setOperationAction(ISD::XOR, VT, Promote);
1177 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1178 setOperationAction(ISD::LOAD, VT, Promote);
1179 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1180 setOperationAction(ISD::SELECT, VT, Promote);
1181 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001182 }
David Greene9b9838d2009-06-29 16:47:10 +00001183 }
1184
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001185 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1186 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001187 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1188 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001189 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1190 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001191 }
1192
Evan Cheng6be2c582006-04-05 23:38:46 +00001193 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001195 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001196
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001197
Eli Friedman962f5492010-06-02 19:35:46 +00001198 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1199 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001200 //
Eli Friedman962f5492010-06-02 19:35:46 +00001201 // FIXME: We really should do custom legalization for addition and
1202 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1203 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001204 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1205 // Add/Sub/Mul with overflow operations are custom lowered.
1206 MVT VT = IntVTs[i];
1207 setOperationAction(ISD::SADDO, VT, Custom);
1208 setOperationAction(ISD::UADDO, VT, Custom);
1209 setOperationAction(ISD::SSUBO, VT, Custom);
1210 setOperationAction(ISD::USUBO, VT, Custom);
1211 setOperationAction(ISD::SMULO, VT, Custom);
1212 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001213 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001214
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001215 // There are no 8-bit 3-address imul/mul instructions
1216 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1217 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001218
Evan Chengd54f2d52009-03-31 19:38:51 +00001219 if (!Subtarget->is64Bit()) {
1220 // These libcalls are not available in 32-bit.
1221 setLibcallName(RTLIB::SHL_I128, 0);
1222 setLibcallName(RTLIB::SRL_I128, 0);
1223 setLibcallName(RTLIB::SRA_I128, 0);
1224 }
1225
Evan Cheng206ee9d2006-07-07 08:33:52 +00001226 // We have target-specific dag combine patterns for the following nodes:
1227 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001228 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001229 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001230 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001231 setTargetDAGCombine(ISD::SHL);
1232 setTargetDAGCombine(ISD::SRA);
1233 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001234 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001235 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001236 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001237 setTargetDAGCombine(ISD::FADD);
1238 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001239 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001240 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001241 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001242 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001243 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001244 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001245 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001246 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001247 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001248 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001249 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001250 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001251 if (Subtarget->is64Bit())
1252 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001253 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001254
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001255 computeRegisterProperties();
1256
Evan Cheng05219282011-01-06 06:52:41 +00001257 // On Darwin, -Os means optimize for size without hurting performance,
1258 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001259 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001260 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001261 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001262 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1263 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1264 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001265 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001266 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001267
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001268 // Predictable cmov don't hurt on atom because it's in-order.
1269 predictableSelectIsExpensive = !Subtarget->isAtom();
1270
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001271 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001272}
1273
Scott Michel5b8f82e2008-03-10 15:42:14 +00001274
Duncan Sands28b77e92011-09-06 19:07:46 +00001275EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1276 if (!VT.isVector()) return MVT::i8;
1277 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001278}
1279
1280
Evan Cheng29286502008-01-23 23:17:41 +00001281/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1282/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001283static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001284 if (MaxAlign == 16)
1285 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001286 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001287 if (VTy->getBitWidth() == 128)
1288 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001289 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001290 unsigned EltAlign = 0;
1291 getMaxByValAlign(ATy->getElementType(), EltAlign);
1292 if (EltAlign > MaxAlign)
1293 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001294 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001295 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1296 unsigned EltAlign = 0;
1297 getMaxByValAlign(STy->getElementType(i), EltAlign);
1298 if (EltAlign > MaxAlign)
1299 MaxAlign = EltAlign;
1300 if (MaxAlign == 16)
1301 break;
1302 }
1303 }
Evan Cheng29286502008-01-23 23:17:41 +00001304}
1305
1306/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1307/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001308/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1309/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001310unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001311 if (Subtarget->is64Bit()) {
1312 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001313 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001314 if (TyAlign > 8)
1315 return TyAlign;
1316 return 8;
1317 }
1318
Evan Cheng29286502008-01-23 23:17:41 +00001319 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001320 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001321 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001322 return Align;
1323}
Chris Lattner2b02a442007-02-25 08:29:00 +00001324
Evan Chengf0df0312008-05-15 08:39:06 +00001325/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001326/// and store operations as a result of memset, memcpy, and memmove
1327/// lowering. If DstAlign is zero that means it's safe to destination
1328/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1329/// means there isn't a need to check it against alignment requirement,
1330/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001331/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001332/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1333/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1334/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001335/// It returns EVT::Other if the type should be determined using generic
1336/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001337EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001338X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1339 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001340 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001341 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001342 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001343 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1344 // linux. This is because the stack realignment code can't handle certain
1345 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001346 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001347 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00001348 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001349 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001350 (Subtarget->isUnalignedMemAccessFast() ||
1351 ((DstAlign == 0 || DstAlign >= 16) &&
1352 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001353 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001354 if (Subtarget->getStackAlignment() >= 32) {
1355 if (Subtarget->hasAVX2())
1356 return MVT::v8i32;
1357 if (Subtarget->hasAVX())
1358 return MVT::v8f32;
1359 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001360 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001361 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001362 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001363 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001364 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001365 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001366 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001367 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001368 // Do not use f64 to lower memcpy if source is string constant. It's
1369 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001370 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001371 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001372 }
Evan Chengf0df0312008-05-15 08:39:06 +00001373 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001374 return MVT::i64;
1375 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001376}
1377
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001378/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1379/// current function. The returned value is a member of the
1380/// MachineJumpTableInfo::JTEntryKind enum.
1381unsigned X86TargetLowering::getJumpTableEncoding() const {
1382 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1383 // symbol.
1384 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1385 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001386 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001387
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001388 // Otherwise, use the normal jump table encoding heuristics.
1389 return TargetLowering::getJumpTableEncoding();
1390}
1391
Chris Lattnerc64daab2010-01-26 05:02:42 +00001392const MCExpr *
1393X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1394 const MachineBasicBlock *MBB,
1395 unsigned uid,MCContext &Ctx) const{
1396 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1397 Subtarget->isPICStyleGOT());
1398 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1399 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001400 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1401 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001402}
1403
Evan Chengcc415862007-11-09 01:32:10 +00001404/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1405/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001406SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001407 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001408 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001409 // This doesn't have DebugLoc associated with it, but is not really the
1410 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001411 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001412 return Table;
1413}
1414
Chris Lattner589c6f62010-01-26 06:28:43 +00001415/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1416/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1417/// MCExpr.
1418const MCExpr *X86TargetLowering::
1419getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1420 MCContext &Ctx) const {
1421 // X86-64 uses RIP relative addressing based on the jump table label.
1422 if (Subtarget->isPICStyleRIPRel())
1423 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1424
1425 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001426 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001427}
1428
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001429// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001430std::pair<const TargetRegisterClass*, uint8_t>
1431X86TargetLowering::findRepresentativeClass(EVT VT) const{
1432 const TargetRegisterClass *RRC = 0;
1433 uint8_t Cost = 1;
1434 switch (VT.getSimpleVT().SimpleTy) {
1435 default:
1436 return TargetLowering::findRepresentativeClass(VT);
1437 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001438 RRC = Subtarget->is64Bit() ?
1439 (const TargetRegisterClass*)&X86::GR64RegClass :
1440 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001441 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001442 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001443 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001444 break;
1445 case MVT::f32: case MVT::f64:
1446 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1447 case MVT::v4f32: case MVT::v2f64:
1448 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1449 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001450 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001451 break;
1452 }
1453 return std::make_pair(RRC, Cost);
1454}
1455
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001456bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1457 unsigned &Offset) const {
1458 if (!Subtarget->isTargetLinux())
1459 return false;
1460
1461 if (Subtarget->is64Bit()) {
1462 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1463 Offset = 0x28;
1464 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1465 AddressSpace = 256;
1466 else
1467 AddressSpace = 257;
1468 } else {
1469 // %gs:0x14 on i386
1470 Offset = 0x14;
1471 AddressSpace = 256;
1472 }
1473 return true;
1474}
1475
1476
Chris Lattner2b02a442007-02-25 08:29:00 +00001477//===----------------------------------------------------------------------===//
1478// Return Value Calling Convention Implementation
1479//===----------------------------------------------------------------------===//
1480
Chris Lattner59ed56b2007-02-28 04:55:35 +00001481#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001482
Michael J. Spencerec38de22010-10-10 22:04:20 +00001483bool
Eric Christopher471e4222011-06-08 23:55:35 +00001484X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001485 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001486 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001487 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001488 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001489 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001490 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001491 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001492}
1493
Dan Gohman98ca4f22009-08-05 01:29:28 +00001494SDValue
1495X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001496 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001497 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001498 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001499 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001500 MachineFunction &MF = DAG.getMachineFunction();
1501 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Chris Lattner9774c912007-02-27 05:28:59 +00001503 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001504 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505 RVLocs, *DAG.getContext());
1506 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001507
Evan Chengdcea1632010-02-04 02:40:39 +00001508 // Add the regs to the liveout set for the function.
1509 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1510 for (unsigned i = 0; i != RVLocs.size(); ++i)
1511 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1512 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Dan Gohman475871a2008-07-27 21:46:04 +00001514 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001515
Dan Gohman475871a2008-07-27 21:46:04 +00001516 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001517 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1518 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001519 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1520 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001522 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001523 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1524 CCValAssign &VA = RVLocs[i];
1525 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001526 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001527 EVT ValVT = ValToCopy.getValueType();
1528
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001529 // Promote values to the appropriate types
1530 if (VA.getLocInfo() == CCValAssign::SExt)
1531 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1532 else if (VA.getLocInfo() == CCValAssign::ZExt)
1533 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1534 else if (VA.getLocInfo() == CCValAssign::AExt)
1535 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1536 else if (VA.getLocInfo() == CCValAssign::BCvt)
1537 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1538
Dale Johannesenc4510512010-09-24 19:05:48 +00001539 // If this is x86-64, and we disabled SSE, we can't return FP values,
1540 // or SSE or MMX vectors.
1541 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1542 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001543 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001544 report_fatal_error("SSE register return with SSE disabled");
1545 }
1546 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1547 // llvm-gcc has never done it right and no one has noticed, so this
1548 // should be OK for now.
1549 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001550 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001551 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Chris Lattner447ff682008-03-11 03:23:40 +00001553 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1554 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001555 if (VA.getLocReg() == X86::ST0 ||
1556 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001557 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1558 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001559 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001561 RetOps.push_back(ValToCopy);
1562 // Don't emit a copytoreg.
1563 continue;
1564 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001565
Evan Cheng242b38b2009-02-23 09:03:22 +00001566 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1567 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001568 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001569 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001570 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001571 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001572 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1573 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001574 // If we don't have SSE2 available, convert to v4f32 so the generated
1575 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001576 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001577 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001578 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001579 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001580 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001581
Dale Johannesendd64c412009-02-04 00:33:20 +00001582 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001583 Flag = Chain.getValue(1);
1584 }
Dan Gohman61a92132008-04-21 23:59:07 +00001585
1586 // The x86-64 ABI for returning structs by value requires that we copy
1587 // the sret argument into %rax for the return. We saved the argument into
1588 // a virtual register in the entry block, so now we copy the value out
1589 // and into %rax.
1590 if (Subtarget->is64Bit() &&
1591 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1592 MachineFunction &MF = DAG.getMachineFunction();
1593 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1594 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001595 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001596 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001597 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001598
Dale Johannesendd64c412009-02-04 00:33:20 +00001599 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001600 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001601
1602 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001603 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001604 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Chris Lattner447ff682008-03-11 03:23:40 +00001606 RetOps[0] = Chain; // Update chain.
1607
1608 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001610 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001611
1612 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001614}
1615
Evan Chengbf010eb2012-04-10 01:51:00 +00001616bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001617 if (N->getNumValues() != 1)
1618 return false;
1619 if (!N->hasNUsesOfValue(1, 0))
1620 return false;
1621
Evan Chengbf010eb2012-04-10 01:51:00 +00001622 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001624 if (Copy->getOpcode() == ISD::CopyToReg) {
1625 // If the copy has a glue operand, we conservatively assume it isn't safe to
1626 // perform a tail call.
1627 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1628 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001629 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001630 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001631 return false;
1632
Evan Cheng1bf891a2010-12-01 22:59:46 +00001633 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001634 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001635 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001636 if (UI->getOpcode() != X86ISD::RET_FLAG)
1637 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001638 HasRet = true;
1639 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001640
Evan Chengbf010eb2012-04-10 01:51:00 +00001641 if (!HasRet)
1642 return false;
1643
1644 Chain = TCChain;
1645 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001646}
1647
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001648EVT
1649X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001650 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001651 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001652 // TODO: Is this also valid on 32-bit?
1653 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001654 ReturnMVT = MVT::i8;
1655 else
1656 ReturnMVT = MVT::i32;
1657
1658 EVT MinVT = getRegisterType(Context, ReturnMVT);
1659 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001660}
1661
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662/// LowerCallResult - Lower the result values of a call into the
1663/// appropriate copies out of appropriate physical registers.
1664///
1665SDValue
1666X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001667 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668 const SmallVectorImpl<ISD::InputArg> &Ins,
1669 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001670 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001671
Chris Lattnere32bbf62007-02-28 07:09:55 +00001672 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001673 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001674 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001675 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001676 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001678
Chris Lattner3085e152007-02-25 08:59:22 +00001679 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001680 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001681 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001682 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Torok Edwin3f142c32009-02-01 18:15:56 +00001684 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001686 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001687 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001688 }
1689
Evan Cheng79fb3b42009-02-20 20:43:02 +00001690 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001691
1692 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001693 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001694 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001695 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001696 // instead.
1697 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1698 // If we prefer to use the value in xmm registers, copy it out as f80 and
1699 // use a truncate to move it from fp stack reg to xmm reg.
1700 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001701 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001702 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1703 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001704 Val = Chain.getValue(0);
1705
1706 // Round the f80 to the right size, which also moves it to the appropriate
1707 // xmm register.
1708 if (CopyVT != VA.getValVT())
1709 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1710 // This truncation won't change the value.
1711 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001712 } else {
1713 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1714 CopyVT, InFlag).getValue(1);
1715 Val = Chain.getValue(0);
1716 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001717 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001719 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001720
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001722}
1723
1724
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001725//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001726// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001727//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001728// StdCall calling convention seems to be standard for many Windows' API
1729// routines and around. It differs from C calling convention just a little:
1730// callee should clean up the stack, not caller. Symbols should be also
1731// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001732// For info on fast calling convention see Fast Calling Convention (tail call)
1733// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001734
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001736/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001737enum StructReturnType {
1738 NotStructReturn,
1739 RegStructReturn,
1740 StackStructReturn
1741};
1742static StructReturnType
1743callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001745 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001746
Rafael Espindola1cee7102012-07-25 13:41:10 +00001747 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1748 if (!Flags.isSRet())
1749 return NotStructReturn;
1750 if (Flags.isInReg())
1751 return RegStructReturn;
1752 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001753}
1754
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001755/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001756/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001757static StructReturnType
1758argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001760 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001761
Rafael Espindola1cee7102012-07-25 13:41:10 +00001762 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1763 if (!Flags.isSRet())
1764 return NotStructReturn;
1765 if (Flags.isInReg())
1766 return RegStructReturn;
1767 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001768}
1769
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001770/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1771/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001772/// the specific parameter attribute. The copy will be passed as a byval
1773/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001774static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001775CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001776 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1777 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001778 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001779
Dale Johannesendd64c412009-02-04 00:33:20 +00001780 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001781 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001782 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001783}
1784
Chris Lattner29689432010-03-11 00:22:57 +00001785/// IsTailCallConvention - Return true if the calling convention is one that
1786/// supports tail call optimization.
1787static bool IsTailCallConvention(CallingConv::ID CC) {
1788 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1789}
1790
Evan Cheng485fafc2011-03-21 01:19:09 +00001791bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001792 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001793 return false;
1794
1795 CallSite CS(CI);
1796 CallingConv::ID CalleeCC = CS.getCallingConv();
1797 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1798 return false;
1799
1800 return true;
1801}
1802
Evan Cheng0c439eb2010-01-27 00:07:07 +00001803/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1804/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001805static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1806 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001807 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001808}
1809
Dan Gohman98ca4f22009-08-05 01:29:28 +00001810SDValue
1811X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001812 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 const SmallVectorImpl<ISD::InputArg> &Ins,
1814 DebugLoc dl, SelectionDAG &DAG,
1815 const CCValAssign &VA,
1816 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001817 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001818 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001819 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001820 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1821 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001822 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001823 EVT ValVT;
1824
1825 // If value is passed by pointer we have address passed instead of the value
1826 // itself.
1827 if (VA.getLocInfo() == CCValAssign::Indirect)
1828 ValVT = VA.getLocVT();
1829 else
1830 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001831
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001832 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001833 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001834 // In case of tail call optimization mark all arguments mutable. Since they
1835 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001836 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001837 unsigned Bytes = Flags.getByValSize();
1838 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1839 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001840 return DAG.getFrameIndex(FI, getPointerTy());
1841 } else {
1842 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001843 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001844 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1845 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001846 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001847 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001848 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001849}
1850
Dan Gohman475871a2008-07-27 21:46:04 +00001851SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001853 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001854 bool isVarArg,
1855 const SmallVectorImpl<ISD::InputArg> &Ins,
1856 DebugLoc dl,
1857 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001858 SmallVectorImpl<SDValue> &InVals)
1859 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001860 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 const Function* Fn = MF.getFunction();
1864 if (Fn->hasExternalLinkage() &&
1865 Subtarget->isTargetCygMing() &&
1866 Fn->getName() == "main")
1867 FuncInfo->setForceFramePointer(true);
1868
Evan Cheng1bc78042006-04-26 01:20:17 +00001869 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001870 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001871 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001872 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001873
Chris Lattner29689432010-03-11 00:22:57 +00001874 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1875 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001876
Chris Lattner638402b2007-02-28 07:00:42 +00001877 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001878 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001879 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001880 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001881
1882 // Allocate shadow area for Win64
1883 if (IsWin64) {
1884 CCInfo.AllocateStack(32, 8);
1885 }
1886
Duncan Sands45907662010-10-31 13:21:44 +00001887 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Chris Lattnerf39f7712007-02-28 05:46:49 +00001889 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001890 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1892 CCValAssign &VA = ArgLocs[i];
1893 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1894 // places.
1895 assert(VA.getValNo() != LastVal &&
1896 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001897 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001898 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001901 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001902 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001903 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001904 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001905 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001906 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001908 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001910 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001911 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001912 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001913 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001914 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001915 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001916 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001918 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001919
Devang Patel68e6bee2011-02-21 23:21:26 +00001920 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001922
Chris Lattnerf39f7712007-02-28 05:46:49 +00001923 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1924 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1925 // right size.
1926 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001927 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001928 DAG.getValueType(VA.getValVT()));
1929 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001930 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001931 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001932 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001933 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001934
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001935 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001936 // Handle MMX values passed in XMM regs.
1937 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001938 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1939 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001940 } else
1941 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001942 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001943 } else {
1944 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001945 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001946 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001947
1948 // If value is passed via pointer - do a load.
1949 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001950 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001951 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001952
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001954 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955
Dan Gohman61a92132008-04-21 23:59:07 +00001956 // The x86-64 ABI for returning structs by value requires that we copy
1957 // the sret argument into %rax for the return. Save the argument into
1958 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001959 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001960 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1961 unsigned Reg = FuncInfo->getSRetReturnReg();
1962 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001963 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001964 FuncInfo->setSRetReturnReg(Reg);
1965 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001967 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001968 }
1969
Chris Lattnerf39f7712007-02-28 05:46:49 +00001970 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001971 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001972 if (FuncIsMadeTailCallSafe(CallConv,
1973 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001974 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001975
Evan Cheng1bc78042006-04-26 01:20:17 +00001976 // If the function takes variable number of arguments, make a frame index for
1977 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001978 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001979 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1980 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001981 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 }
1983 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001984 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1985
1986 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001987 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001989 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001990 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001991 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1992 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001993 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1995 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1996 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001997 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001998 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001999
2000 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002001 // The XMM registers which might contain var arg parameters are shadowed
2002 // in their paired GPR. So we only need to save the GPR to their home
2003 // slots.
2004 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002005 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002006 } else {
2007 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2008 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002009
Chad Rosier30450e82011-12-22 22:35:21 +00002010 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2011 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002012 }
2013 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2014 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002015
Bill Wendling67658342012-10-09 07:45:08 +00002016 bool NoImplicitFloatOps = Fn->getFnAttributes().
2017 hasAttribute(Attributes::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002018 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002019 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002020 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2021 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002022 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002023 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002024 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002025 // Kernel mode asks for SSE to be disabled, so don't push them
2026 // on the stack.
2027 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002028
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002029 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002030 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002031 // Get to the caller-allocated home save location. Add 8 to account
2032 // for the return address.
2033 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002034 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002035 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002036 // Fixup to set vararg frame on shadow area (4 x i64).
2037 if (NumIntRegs < 4)
2038 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002039 } else {
2040 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002041 // registers, then we must store them to their spots on the stack so
2042 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002043 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2044 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2045 FuncInfo->setRegSaveFrameIndex(
2046 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002047 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002048 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002049
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002051 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002052 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2053 getPointerTy());
2054 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002055 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002056 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2057 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002058 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002059 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002062 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002063 MachinePointerInfo::getFixedStack(
2064 FuncInfo->getRegSaveFrameIndex(), Offset),
2065 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002066 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002067 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002068 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002069
Dan Gohmanface41a2009-08-16 21:24:25 +00002070 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2071 // Now store the XMM (fp + vector) parameter registers.
2072 SmallVector<SDValue, 11> SaveXMMOps;
2073 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002074
Craig Topperc9099502012-04-20 06:31:50 +00002075 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002076 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2077 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002078
Dan Gohman1e93df62010-04-17 14:41:14 +00002079 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2080 FuncInfo->getRegSaveFrameIndex()));
2081 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2082 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002083
Dan Gohmanface41a2009-08-16 21:24:25 +00002084 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002085 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002086 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002087 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2088 SaveXMMOps.push_back(Val);
2089 }
2090 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2091 MVT::Other,
2092 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002093 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002094
2095 if (!MemOps.empty())
2096 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2097 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002098 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002100
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002102 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2103 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002104 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002105 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002107 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002108 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002109 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002110 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002111 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002112
Gordon Henriksen86737662008-01-05 16:56:59 +00002113 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002114 // RegSaveFrameIndex is X86-64 only.
2115 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002116 if (CallConv == CallingConv::X86_FastCall ||
2117 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002118 // fastcc functions can't have varargs.
2119 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002120 }
Evan Cheng25caf632006-05-23 21:06:34 +00002121
Rafael Espindola76927d752011-08-30 19:39:58 +00002122 FuncInfo->setArgumentStackSize(StackSize);
2123
Dan Gohman98ca4f22009-08-05 01:29:28 +00002124 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002125}
2126
Dan Gohman475871a2008-07-27 21:46:04 +00002127SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002128X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2129 SDValue StackPtr, SDValue Arg,
2130 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002131 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002132 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002133 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002134 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002135 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002136 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002137 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002138
2139 return DAG.getStore(Chain, dl, Arg, PtrOff,
2140 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002141 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002142}
2143
Bill Wendling64e87322009-01-16 19:25:27 +00002144/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002146SDValue
2147X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002148 SDValue &OutRetAddr, SDValue Chain,
2149 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002150 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002151 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002152 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002153 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002154
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002155 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002156 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002157 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002158 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002159}
2160
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002161/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002162/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002163static SDValue
2164EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002166 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002167 // Store the return address to the appropriate stack slot.
2168 if (!FPDiff) return Chain;
2169 // Calculate the new stack slot for the return address.
2170 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002171 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002172 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002174 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002175 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002176 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002177 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002178 return Chain;
2179}
2180
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002182X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002183 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002184 SelectionDAG &DAG = CLI.DAG;
2185 DebugLoc &dl = CLI.DL;
2186 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2187 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2188 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2189 SDValue Chain = CLI.Chain;
2190 SDValue Callee = CLI.Callee;
2191 CallingConv::ID CallConv = CLI.CallConv;
2192 bool &isTailCall = CLI.IsTailCall;
2193 bool isVarArg = CLI.IsVarArg;
2194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 MachineFunction &MF = DAG.getMachineFunction();
2196 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002197 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002198 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002199 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002200 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201
Nick Lewycky22de16d2012-01-19 00:34:10 +00002202 if (MF.getTarget().Options.DisableTailCalls)
2203 isTailCall = false;
2204
Evan Cheng5f941932010-02-05 02:21:12 +00002205 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002206 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002207 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002208 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002209 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002210 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002211
2212 // Sibcalls are automatically detected tailcalls which do not require
2213 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002214 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002215 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002216
2217 if (isTailCall)
2218 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002219 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002220
Chris Lattner29689432010-03-11 00:22:57 +00002221 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2222 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002223
Chris Lattner638402b2007-02-28 07:00:42 +00002224 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002225 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002226 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002228
2229 // Allocate shadow area for Win64
2230 if (IsWin64) {
2231 CCInfo.AllocateStack(32, 8);
2232 }
2233
Duncan Sands45907662010-10-31 13:21:44 +00002234 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002235
Chris Lattner423c5f42007-02-28 05:31:48 +00002236 // Get a count of how many bytes are to be pushed on the stack.
2237 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002238 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002239 // This is a sibcall. The memory operands are available in caller's
2240 // own caller's stack.
2241 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002242 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2243 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002244 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002245
Gordon Henriksen86737662008-01-05 16:56:59 +00002246 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002247 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002248 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002249 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002250 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2251 FPDiff = NumBytesCallerPushed - NumBytes;
2252
2253 // Set the delta of movement of the returnaddr stackslot.
2254 // But only set if delta is greater than previous delta.
2255 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2256 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2257 }
2258
Evan Chengf22f9b32010-02-06 03:28:46 +00002259 if (!IsSibcall)
2260 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002261
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002263 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002264 if (isTailCall && FPDiff)
2265 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2266 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002267
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2269 SmallVector<SDValue, 8> MemOpChains;
2270 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002271
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002272 // Walk the register/memloc assignments, inserting copies/loads. In the case
2273 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002274 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2275 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002277 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002279 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002280
Chris Lattner423c5f42007-02-28 05:31:48 +00002281 // Promote the value if needed.
2282 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002283 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002284 case CCValAssign::Full: break;
2285 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002286 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002287 break;
2288 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002289 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002290 break;
2291 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002292 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002293 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002294 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2296 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002297 } else
2298 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2299 break;
2300 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002302 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002303 case CCValAssign::Indirect: {
2304 // Store the argument.
2305 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002306 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002307 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002308 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002309 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002310 Arg = SpillSlot;
2311 break;
2312 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Chris Lattner423c5f42007-02-28 05:31:48 +00002315 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002316 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2317 if (isVarArg && IsWin64) {
2318 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2319 // shadow reg if callee is a varargs function.
2320 unsigned ShadowReg = 0;
2321 switch (VA.getLocReg()) {
2322 case X86::XMM0: ShadowReg = X86::RCX; break;
2323 case X86::XMM1: ShadowReg = X86::RDX; break;
2324 case X86::XMM2: ShadowReg = X86::R8; break;
2325 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002326 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002327 if (ShadowReg)
2328 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002329 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002330 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002331 assert(VA.isMemLoc());
2332 if (StackPtr.getNode() == 0)
2333 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2334 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2335 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002336 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002338
Evan Cheng32fe1032006-05-25 00:59:30 +00002339 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002340 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002341 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002342
Chris Lattner88e1fd52009-07-09 04:24:46 +00002343 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002344 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2345 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002346 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002347 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2348 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002349 } else {
2350 // If we are tail calling and generating PIC/GOT style code load the
2351 // address of the callee into ECX. The value in ecx is used as target of
2352 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2353 // for tail calls on PIC/GOT architectures. Normally we would just put the
2354 // address of GOT into ebx and then call target@PLT. But for tail calls
2355 // ebx would be restored (since ebx is callee saved) before jumping to the
2356 // target@PLT.
2357
2358 // Note: The actual moving to ECX is done further down.
2359 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2360 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2361 !G->getGlobal()->hasProtectedVisibility())
2362 Callee = LowerGlobalAddress(Callee, DAG);
2363 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002364 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002365 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002366 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002367
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002368 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 // From AMD64 ABI document:
2370 // For calls that may call functions that use varargs or stdargs
2371 // (prototype-less calls or calls to functions containing ellipsis (...) in
2372 // the declaration) %al is used as hidden argument to specify the number
2373 // of SSE registers used. The contents of %al do not need to match exactly
2374 // the number of registers, but must be an ubound on the number of SSE
2375 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002376
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002378 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2380 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2381 };
2382 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002383 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002384 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002385
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002386 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2387 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 }
2389
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002390 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002391 if (isTailCall) {
2392 // Force all the incoming stack arguments to be loaded from the stack
2393 // before any new outgoing arguments are stored to the stack, because the
2394 // outgoing stack slots may alias the incoming argument stack slots, and
2395 // the alias isn't otherwise explicit. This is slightly more conservative
2396 // than necessary, because it means that each store effectively depends
2397 // on every argument instead of just those arguments it would clobber.
2398 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2399
Dan Gohman475871a2008-07-27 21:46:04 +00002400 SmallVector<SDValue, 8> MemOpChains2;
2401 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002402 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002403 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002404 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2405 CCValAssign &VA = ArgLocs[i];
2406 if (VA.isRegLoc())
2407 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002408 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002409 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002410 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002411 // Create frame index.
2412 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002413 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002414 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002415 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002416
Duncan Sands276dcbd2008-03-21 09:14:45 +00002417 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002418 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002419 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002420 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002421 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002422 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002423 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002424
Dan Gohman98ca4f22009-08-05 01:29:28 +00002425 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2426 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002427 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002428 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002429 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002430 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002432 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002433 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002434 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002435 }
2436 }
2437
2438 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002439 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002440 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002441
2442 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002443 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002444 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002445 }
2446
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002447 // Build a sequence of copy-to-reg nodes chained together with token chain
2448 // and flag operands which copy the outgoing args into registers.
2449 SDValue InFlag;
2450 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2451 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2452 RegsToPass[i].second, InFlag);
2453 InFlag = Chain.getValue(1);
2454 }
2455
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002456 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2457 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2458 // In the 64-bit large code model, we have to make all calls
2459 // through a register, since the call instruction's 32-bit
2460 // pc-relative offset may not be large enough to hold the whole
2461 // address.
2462 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002463 // If the callee is a GlobalAddress node (quite common, every direct call
2464 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2465 // it.
2466
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002467 // We should use extra load for direct calls to dllimported functions in
2468 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002469 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002470 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002471 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002472 bool ExtraLoad = false;
2473 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002474
Chris Lattner48a7d022009-07-09 05:02:21 +00002475 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2476 // external symbols most go through the PLT in PIC mode. If the symbol
2477 // has hidden or protected visibility, or if it is static or local, then
2478 // we don't need to use the PLT - we can directly call it.
2479 if (Subtarget->isTargetELF() &&
2480 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002481 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002482 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002483 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002484 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002485 (!Subtarget->getTargetTriple().isMacOSX() ||
2486 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002487 // PC-relative references to external symbols should go through $stub,
2488 // unless we're building with the leopard linker or later, which
2489 // automatically synthesizes these stubs.
2490 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002491 } else if (Subtarget->isPICStyleRIPRel() &&
2492 isa<Function>(GV) &&
Bill Wendling67658342012-10-09 07:45:08 +00002493 cast<Function>(GV)->getFnAttributes().
2494 hasAttribute(Attributes::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002495 // If the function is marked as non-lazy, generate an indirect call
2496 // which loads from the GOT directly. This avoids runtime overhead
2497 // at the cost of eager binding (and one extra byte of encoding).
2498 OpFlags = X86II::MO_GOTPCREL;
2499 WrapperKind = X86ISD::WrapperRIP;
2500 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002501 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002502
Devang Patel0d881da2010-07-06 22:08:15 +00002503 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002504 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002505
2506 // Add a wrapper if needed.
2507 if (WrapperKind != ISD::DELETED_NODE)
2508 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2509 // Add extra indirection if needed.
2510 if (ExtraLoad)
2511 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2512 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002513 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002514 }
Bill Wendling056292f2008-09-16 21:48:12 +00002515 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002516 unsigned char OpFlags = 0;
2517
Evan Cheng1bf891a2010-12-01 22:59:46 +00002518 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2519 // external symbols should go through the PLT.
2520 if (Subtarget->isTargetELF() &&
2521 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2522 OpFlags = X86II::MO_PLT;
2523 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002524 (!Subtarget->getTargetTriple().isMacOSX() ||
2525 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002526 // PC-relative references to external symbols should go through $stub,
2527 // unless we're building with the leopard linker or later, which
2528 // automatically synthesizes these stubs.
2529 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002530 }
Eric Christopherfd179292009-08-27 18:07:15 +00002531
Chris Lattner48a7d022009-07-09 05:02:21 +00002532 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2533 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002534 }
2535
Chris Lattnerd96d0722007-02-25 06:40:16 +00002536 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002537 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002538 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002539
Evan Chengf22f9b32010-02-06 03:28:46 +00002540 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002541 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2542 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002543 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002544 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002545
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002546 Ops.push_back(Chain);
2547 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002548
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002550 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002551
Gordon Henriksen86737662008-01-05 16:56:59 +00002552 // Add argument registers to the end of the list so that they are known live
2553 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002554 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2555 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2556 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002558 // Add a register mask operand representing the call-preserved registers.
2559 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2560 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2561 assert(Mask && "Missing call preserved mask for calling convention");
2562 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002563
Gabor Greifba36cb52008-08-28 21:40:38 +00002564 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002565 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002566
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002568 // We used to do:
2569 //// If this is the first return lowered for this function, add the regs
2570 //// to the liveout set for the function.
2571 // This isn't right, although it's probably harmless on x86; liveouts
2572 // should be computed from returns not tail calls. Consider a void
2573 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002574 return DAG.getNode(X86ISD::TC_RETURN, dl,
2575 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002576 }
2577
Dale Johannesenace16102009-02-03 19:33:06 +00002578 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002579 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002580
Chris Lattner2d297092006-05-23 18:50:38 +00002581 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002582 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002583 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2584 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002585 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002586 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002587 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002588 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002589 // pops the hidden struct pointer, so we have to push it back.
2590 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002591 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002592 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002593 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002594 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002595
Gordon Henriksenae636f82008-01-03 16:47:34 +00002596 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002597 if (!IsSibcall) {
2598 Chain = DAG.getCALLSEQ_END(Chain,
2599 DAG.getIntPtrConstant(NumBytes, true),
2600 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2601 true),
2602 InFlag);
2603 InFlag = Chain.getValue(1);
2604 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002605
Chris Lattner3085e152007-02-25 08:59:22 +00002606 // Handle result values, copying them out of physregs into vregs that we
2607 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002608 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2609 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002610}
2611
Evan Cheng25ab6902006-09-08 06:48:29 +00002612
2613//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002614// Fast Calling Convention (tail call) implementation
2615//===----------------------------------------------------------------------===//
2616
2617// Like std call, callee cleans arguments, convention except that ECX is
2618// reserved for storing the tail called function address. Only 2 registers are
2619// free for argument passing (inreg). Tail call optimization is performed
2620// provided:
2621// * tailcallopt is enabled
2622// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002623// On X86_64 architecture with GOT-style position independent code only local
2624// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002625// To keep the stack aligned according to platform abi the function
2626// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2627// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002628// If a tail called function callee has more arguments than the caller the
2629// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002630// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002631// original REtADDR, but before the saved framepointer or the spilled registers
2632// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2633// stack layout:
2634// arg1
2635// arg2
2636// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002637// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002638// move area ]
2639// (possible EBP)
2640// ESI
2641// EDI
2642// local1 ..
2643
2644/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2645/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002646unsigned
2647X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2648 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002649 MachineFunction &MF = DAG.getMachineFunction();
2650 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002651 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002652 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002653 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002654 int64_t Offset = StackSize;
Micah Villmowfb384d62012-10-11 21:27:41 +00002655 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002656 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2657 // Number smaller than 12 so just add the difference.
2658 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2659 } else {
2660 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002661 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002662 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002663 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002664 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002665}
2666
Evan Cheng5f941932010-02-05 02:21:12 +00002667/// MatchingStackOffset - Return true if the given stack call argument is
2668/// already available in the same position (relatively) of the caller's
2669/// incoming argument stack.
2670static
2671bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2672 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2673 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002674 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2675 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002676 if (Arg.getOpcode() == ISD::CopyFromReg) {
2677 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002678 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002679 return false;
2680 MachineInstr *Def = MRI->getVRegDef(VR);
2681 if (!Def)
2682 return false;
2683 if (!Flags.isByVal()) {
2684 if (!TII->isLoadFromStackSlot(Def, FI))
2685 return false;
2686 } else {
2687 unsigned Opcode = Def->getOpcode();
2688 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2689 Def->getOperand(1).isFI()) {
2690 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002691 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002692 } else
2693 return false;
2694 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002695 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2696 if (Flags.isByVal())
2697 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002698 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002699 // define @foo(%struct.X* %A) {
2700 // tail call @bar(%struct.X* byval %A)
2701 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002702 return false;
2703 SDValue Ptr = Ld->getBasePtr();
2704 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2705 if (!FINode)
2706 return false;
2707 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002708 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002709 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002710 FI = FINode->getIndex();
2711 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002712 } else
2713 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002714
Evan Cheng4cae1332010-03-05 08:38:04 +00002715 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002716 if (!MFI->isFixedObjectIndex(FI))
2717 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002718 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002719}
2720
Dan Gohman98ca4f22009-08-05 01:29:28 +00002721/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2722/// for tail call optimization. Targets which want to do tail call
2723/// optimization should implement this function.
2724bool
2725X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002726 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002727 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002728 bool isCalleeStructRet,
2729 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002730 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002731 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002732 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002733 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002734 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002735 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002736 CalleeCC != CallingConv::C)
2737 return false;
2738
Evan Cheng7096ae42010-01-29 06:45:59 +00002739 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002740 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002741 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002742
2743 // If the function return type is x86_fp80 and the callee return type is not,
2744 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2745 // perform a tailcall optimization here.
2746 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2747 return false;
2748
Evan Cheng13617962010-04-30 01:12:32 +00002749 CallingConv::ID CallerCC = CallerF->getCallingConv();
2750 bool CCMatch = CallerCC == CalleeCC;
2751
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002752 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002753 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002754 return true;
2755 return false;
2756 }
2757
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002758 // Look for obvious safe cases to perform tail call optimization that do not
2759 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002760
Evan Cheng2c12cb42010-03-26 16:26:03 +00002761 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2762 // emit a special epilogue.
2763 if (RegInfo->needsStackRealignment(MF))
2764 return false;
2765
Evan Chenga375d472010-03-15 18:54:48 +00002766 // Also avoid sibcall optimization if either caller or callee uses struct
2767 // return semantics.
2768 if (isCalleeStructRet || isCallerStructRet)
2769 return false;
2770
Chad Rosier2416da32011-06-24 21:15:36 +00002771 // An stdcall caller is expected to clean up its arguments; the callee
2772 // isn't going to do that.
2773 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2774 return false;
2775
Chad Rosier871f6642011-05-18 19:59:50 +00002776 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002777 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002778 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002779
2780 // Optimizing for varargs on Win64 is unlikely to be safe without
2781 // additional testing.
2782 if (Subtarget->isTargetWin64())
2783 return false;
2784
Chad Rosier871f6642011-05-18 19:59:50 +00002785 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002786 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002787 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002788
Chad Rosier871f6642011-05-18 19:59:50 +00002789 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2790 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2791 if (!ArgLocs[i].isRegLoc())
2792 return false;
2793 }
2794
Chad Rosier30450e82011-12-22 22:35:21 +00002795 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2796 // stack. Therefore, if it's not used by the call it is not safe to optimize
2797 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002798 bool Unused = false;
2799 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2800 if (!Ins[i].Used) {
2801 Unused = true;
2802 break;
2803 }
2804 }
2805 if (Unused) {
2806 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002807 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002808 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002809 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002810 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002811 CCValAssign &VA = RVLocs[i];
2812 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2813 return false;
2814 }
2815 }
2816
Evan Cheng13617962010-04-30 01:12:32 +00002817 // If the calling conventions do not match, then we'd better make sure the
2818 // results are returned in the same way as what the caller expects.
2819 if (!CCMatch) {
2820 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002821 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002822 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002823 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2824
2825 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002826 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002827 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002828 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2829
2830 if (RVLocs1.size() != RVLocs2.size())
2831 return false;
2832 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2833 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2834 return false;
2835 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2836 return false;
2837 if (RVLocs1[i].isRegLoc()) {
2838 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2839 return false;
2840 } else {
2841 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2842 return false;
2843 }
2844 }
2845 }
2846
Evan Chenga6bff982010-01-30 01:22:00 +00002847 // If the callee takes no arguments then go on to check the results of the
2848 // call.
2849 if (!Outs.empty()) {
2850 // Check if stack adjustment is needed. For now, do not do this if any
2851 // argument is passed on the stack.
2852 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002853 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002854 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002855
2856 // Allocate shadow area for Win64
2857 if (Subtarget->isTargetWin64()) {
2858 CCInfo.AllocateStack(32, 8);
2859 }
2860
Duncan Sands45907662010-10-31 13:21:44 +00002861 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002862 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002863 MachineFunction &MF = DAG.getMachineFunction();
2864 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2865 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002866
2867 // Check if the arguments are already laid out in the right way as
2868 // the caller's fixed stack objects.
2869 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002870 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2871 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002872 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002873 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2874 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002875 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002876 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002877 if (VA.getLocInfo() == CCValAssign::Indirect)
2878 return false;
2879 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002880 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2881 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002882 return false;
2883 }
2884 }
2885 }
Evan Cheng9c044672010-05-29 01:35:22 +00002886
2887 // If the tailcall address may be in a register, then make sure it's
2888 // possible to register allocate for it. In 32-bit, the call address can
2889 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002890 // callee-saved registers are restored. These happen to be the same
2891 // registers used to pass 'inreg' arguments so watch out for those.
2892 if (!Subtarget->is64Bit() &&
2893 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002894 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002895 unsigned NumInRegs = 0;
2896 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2897 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002898 if (!VA.isRegLoc())
2899 continue;
2900 unsigned Reg = VA.getLocReg();
2901 switch (Reg) {
2902 default: break;
2903 case X86::EAX: case X86::EDX: case X86::ECX:
2904 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002905 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002906 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002907 }
2908 }
2909 }
Evan Chenga6bff982010-01-30 01:22:00 +00002910 }
Evan Chengb1712452010-01-27 06:25:16 +00002911
Evan Cheng86809cc2010-02-03 03:28:02 +00002912 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002913}
2914
Dan Gohman3df24e62008-09-03 23:12:08 +00002915FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002916X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2917 const TargetLibraryInfo *libInfo) const {
2918 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002919}
2920
2921
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002922//===----------------------------------------------------------------------===//
2923// Other Lowering Hooks
2924//===----------------------------------------------------------------------===//
2925
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002926static bool MayFoldLoad(SDValue Op) {
2927 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2928}
2929
2930static bool MayFoldIntoStore(SDValue Op) {
2931 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2932}
2933
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002934static bool isTargetShuffle(unsigned Opcode) {
2935 switch(Opcode) {
2936 default: return false;
2937 case X86ISD::PSHUFD:
2938 case X86ISD::PSHUFHW:
2939 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002940 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002941 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002942 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002943 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002944 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002945 case X86ISD::MOVLPS:
2946 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002947 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002948 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002949 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002950 case X86ISD::MOVSS:
2951 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002952 case X86ISD::UNPCKL:
2953 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002954 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002955 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002956 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002957 return true;
2958 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002959}
2960
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002961static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002962 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002963 switch(Opc) {
2964 default: llvm_unreachable("Unknown x86 shuffle node");
2965 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002966 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002967 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002968 return DAG.getNode(Opc, dl, VT, V1);
2969 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002970}
2971
2972static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002973 SDValue V1, unsigned TargetMask,
2974 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002975 switch(Opc) {
2976 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002977 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002978 case X86ISD::PSHUFHW:
2979 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002980 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002981 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002982 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2983 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002984}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002985
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002986static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002987 SDValue V1, SDValue V2, unsigned TargetMask,
2988 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002989 switch(Opc) {
2990 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002991 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002992 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002993 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002994 return DAG.getNode(Opc, dl, VT, V1, V2,
2995 DAG.getConstant(TargetMask, MVT::i8));
2996 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002997}
2998
2999static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3000 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3001 switch(Opc) {
3002 default: llvm_unreachable("Unknown x86 shuffle node");
3003 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003004 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003005 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003006 case X86ISD::MOVLPS:
3007 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003008 case X86ISD::MOVSS:
3009 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003010 case X86ISD::UNPCKL:
3011 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003012 return DAG.getNode(Opc, dl, VT, V1, V2);
3013 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003014}
3015
Dan Gohmand858e902010-04-17 15:26:15 +00003016SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003017 MachineFunction &MF = DAG.getMachineFunction();
3018 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3019 int ReturnAddrIndex = FuncInfo->getRAIndex();
3020
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003021 if (ReturnAddrIndex == 0) {
3022 // Set up a frame object for the return address.
Micah Villmowfb384d62012-10-11 21:27:41 +00003023 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003024 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003025 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003026 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003027 }
3028
Evan Cheng25ab6902006-09-08 06:48:29 +00003029 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003030}
3031
3032
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003033bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3034 bool hasSymbolicDisplacement) {
3035 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003036 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003037 return false;
3038
3039 // If we don't have a symbolic displacement - we don't have any extra
3040 // restrictions.
3041 if (!hasSymbolicDisplacement)
3042 return true;
3043
3044 // FIXME: Some tweaks might be needed for medium code model.
3045 if (M != CodeModel::Small && M != CodeModel::Kernel)
3046 return false;
3047
3048 // For small code model we assume that latest object is 16MB before end of 31
3049 // bits boundary. We may also accept pretty large negative constants knowing
3050 // that all objects are in the positive half of address space.
3051 if (M == CodeModel::Small && Offset < 16*1024*1024)
3052 return true;
3053
3054 // For kernel code model we know that all object resist in the negative half
3055 // of 32bits address space. We may not accept negative offsets, since they may
3056 // be just off and we may accept pretty large positive ones.
3057 if (M == CodeModel::Kernel && Offset > 0)
3058 return true;
3059
3060 return false;
3061}
3062
Evan Chengef41ff62011-06-23 17:54:54 +00003063/// isCalleePop - Determines whether the callee is required to pop its
3064/// own arguments. Callee pop is necessary to support tail calls.
3065bool X86::isCalleePop(CallingConv::ID CallingConv,
3066 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3067 if (IsVarArg)
3068 return false;
3069
3070 switch (CallingConv) {
3071 default:
3072 return false;
3073 case CallingConv::X86_StdCall:
3074 return !is64Bit;
3075 case CallingConv::X86_FastCall:
3076 return !is64Bit;
3077 case CallingConv::X86_ThisCall:
3078 return !is64Bit;
3079 case CallingConv::Fast:
3080 return TailCallOpt;
3081 case CallingConv::GHC:
3082 return TailCallOpt;
3083 }
3084}
3085
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003086/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3087/// specific condition code, returning the condition code and the LHS/RHS of the
3088/// comparison to make.
3089static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3090 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003091 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003092 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3093 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3094 // X > -1 -> X == 0, jump !sign.
3095 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003096 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003097 }
3098 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003099 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003100 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003101 }
3102 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003103 // X < 1 -> X <= 0
3104 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003105 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003106 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003107 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003108
Evan Chengd9558e02006-01-06 00:43:03 +00003109 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003110 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003111 case ISD::SETEQ: return X86::COND_E;
3112 case ISD::SETGT: return X86::COND_G;
3113 case ISD::SETGE: return X86::COND_GE;
3114 case ISD::SETLT: return X86::COND_L;
3115 case ISD::SETLE: return X86::COND_LE;
3116 case ISD::SETNE: return X86::COND_NE;
3117 case ISD::SETULT: return X86::COND_B;
3118 case ISD::SETUGT: return X86::COND_A;
3119 case ISD::SETULE: return X86::COND_BE;
3120 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003121 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003123
Chris Lattner4c78e022008-12-23 23:42:27 +00003124 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003125
Chris Lattner4c78e022008-12-23 23:42:27 +00003126 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003127 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3128 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003129 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3130 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003131 }
3132
Chris Lattner4c78e022008-12-23 23:42:27 +00003133 switch (SetCCOpcode) {
3134 default: break;
3135 case ISD::SETOLT:
3136 case ISD::SETOLE:
3137 case ISD::SETUGT:
3138 case ISD::SETUGE:
3139 std::swap(LHS, RHS);
3140 break;
3141 }
3142
3143 // On a floating point condition, the flags are set as follows:
3144 // ZF PF CF op
3145 // 0 | 0 | 0 | X > Y
3146 // 0 | 0 | 1 | X < Y
3147 // 1 | 0 | 0 | X == Y
3148 // 1 | 1 | 1 | unordered
3149 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003150 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003151 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003152 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003153 case ISD::SETOLT: // flipped
3154 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003155 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003156 case ISD::SETOLE: // flipped
3157 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003158 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003159 case ISD::SETUGT: // flipped
3160 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003161 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003162 case ISD::SETUGE: // flipped
3163 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003164 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003165 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003166 case ISD::SETNE: return X86::COND_NE;
3167 case ISD::SETUO: return X86::COND_P;
3168 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003169 case ISD::SETOEQ:
3170 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003171 }
Evan Chengd9558e02006-01-06 00:43:03 +00003172}
3173
Evan Cheng4a460802006-01-11 00:33:36 +00003174/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3175/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003176/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003177static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003178 switch (X86CC) {
3179 default:
3180 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003181 case X86::COND_B:
3182 case X86::COND_BE:
3183 case X86::COND_E:
3184 case X86::COND_P:
3185 case X86::COND_A:
3186 case X86::COND_AE:
3187 case X86::COND_NE:
3188 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003189 return true;
3190 }
3191}
3192
Evan Chengeb2f9692009-10-27 19:56:55 +00003193/// isFPImmLegal - Returns true if the target can instruction select the
3194/// specified FP immediate natively. If false, the legalizer will
3195/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003196bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003197 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3198 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3199 return true;
3200 }
3201 return false;
3202}
3203
Nate Begeman9008ca62009-04-27 18:41:29 +00003204/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3205/// the specified range (L, H].
3206static bool isUndefOrInRange(int Val, int Low, int Hi) {
3207 return (Val < 0) || (Val >= Low && Val < Hi);
3208}
3209
3210/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3211/// specified value.
3212static bool isUndefOrEqual(int Val, int CmpVal) {
3213 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003214 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003216}
3217
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003218/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003219/// from position Pos and ending in Pos+Size, falls within the specified
3220/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003221static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003222 unsigned Pos, unsigned Size, int Low) {
3223 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003224 if (!isUndefOrEqual(Mask[i], Low))
3225 return false;
3226 return true;
3227}
3228
Nate Begeman9008ca62009-04-27 18:41:29 +00003229/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3230/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3231/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003232static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003233 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 return (Mask[0] < 2 && Mask[1] < 2);
3237 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003238}
3239
Nate Begeman9008ca62009-04-27 18:41:29 +00003240/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3241/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003242static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3243 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003244 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003247 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3248 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003249
Evan Cheng506d3df2006-03-29 23:07:14 +00003250 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003251 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003252 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003253 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003254
Craig Toppera9a568a2012-05-02 08:03:44 +00003255 if (VT == MVT::v16i16) {
3256 // Lower quadword copied in order or undef.
3257 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3258 return false;
3259
3260 // Upper quadword shuffled.
3261 for (unsigned i = 12; i != 16; ++i)
3262 if (!isUndefOrInRange(Mask[i], 12, 16))
3263 return false;
3264 }
3265
Evan Cheng506d3df2006-03-29 23:07:14 +00003266 return true;
3267}
3268
Nate Begeman9008ca62009-04-27 18:41:29 +00003269/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3270/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003271static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3272 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003273 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003274
Rafael Espindola15684b22009-04-24 12:40:33 +00003275 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003276 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3277 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003278
Rafael Espindola15684b22009-04-24 12:40:33 +00003279 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003280 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003281 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003282 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003283
Craig Toppera9a568a2012-05-02 08:03:44 +00003284 if (VT == MVT::v16i16) {
3285 // Upper quadword copied in order.
3286 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3287 return false;
3288
3289 // Lower quadword shuffled.
3290 for (unsigned i = 8; i != 12; ++i)
3291 if (!isUndefOrInRange(Mask[i], 8, 12))
3292 return false;
3293 }
3294
Rafael Espindola15684b22009-04-24 12:40:33 +00003295 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003296}
3297
Nate Begemana09008b2009-10-19 02:17:23 +00003298/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3299/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003300static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3301 const X86Subtarget *Subtarget) {
3302 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3303 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003304 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003305
Craig Topper0e2037b2012-01-20 05:53:00 +00003306 unsigned NumElts = VT.getVectorNumElements();
3307 unsigned NumLanes = VT.getSizeInBits()/128;
3308 unsigned NumLaneElts = NumElts/NumLanes;
3309
3310 // Do not handle 64-bit element shuffles with palignr.
3311 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003312 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003313
Craig Topper0e2037b2012-01-20 05:53:00 +00003314 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3315 unsigned i;
3316 for (i = 0; i != NumLaneElts; ++i) {
3317 if (Mask[i+l] >= 0)
3318 break;
3319 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003320
Craig Topper0e2037b2012-01-20 05:53:00 +00003321 // Lane is all undef, go to next lane
3322 if (i == NumLaneElts)
3323 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003324
Craig Topper0e2037b2012-01-20 05:53:00 +00003325 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003326
Craig Topper0e2037b2012-01-20 05:53:00 +00003327 // Make sure its in this lane in one of the sources
3328 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3329 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003330 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003331
3332 // If not lane 0, then we must match lane 0
3333 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3334 return false;
3335
3336 // Correct second source to be contiguous with first source
3337 if (Start >= (int)NumElts)
3338 Start -= NumElts - NumLaneElts;
3339
3340 // Make sure we're shifting in the right direction.
3341 if (Start <= (int)(i+l))
3342 return false;
3343
3344 Start -= i;
3345
3346 // Check the rest of the elements to see if they are consecutive.
3347 for (++i; i != NumLaneElts; ++i) {
3348 int Idx = Mask[i+l];
3349
3350 // Make sure its in this lane
3351 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3352 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3353 return false;
3354
3355 // If not lane 0, then we must match lane 0
3356 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3357 return false;
3358
3359 if (Idx >= (int)NumElts)
3360 Idx -= NumElts - NumLaneElts;
3361
3362 if (!isUndefOrEqual(Idx, Start+i))
3363 return false;
3364
3365 }
Nate Begemana09008b2009-10-19 02:17:23 +00003366 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003367
Nate Begemana09008b2009-10-19 02:17:23 +00003368 return true;
3369}
3370
Craig Topper1a7700a2012-01-19 08:19:12 +00003371/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3372/// the two vector operands have swapped position.
3373static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3374 unsigned NumElems) {
3375 for (unsigned i = 0; i != NumElems; ++i) {
3376 int idx = Mask[i];
3377 if (idx < 0)
3378 continue;
3379 else if (idx < (int)NumElems)
3380 Mask[i] = idx + NumElems;
3381 else
3382 Mask[i] = idx - NumElems;
3383 }
3384}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003385
Craig Topper1a7700a2012-01-19 08:19:12 +00003386/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3387/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3388/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3389/// reverse of what x86 shuffles want.
3390static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3391 bool Commuted = false) {
3392 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003393 return false;
3394
Craig Topper1a7700a2012-01-19 08:19:12 +00003395 unsigned NumElems = VT.getVectorNumElements();
3396 unsigned NumLanes = VT.getSizeInBits()/128;
3397 unsigned NumLaneElems = NumElems/NumLanes;
3398
3399 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003400 return false;
3401
3402 // VSHUFPSY divides the resulting vector into 4 chunks.
3403 // The sources are also splitted into 4 chunks, and each destination
3404 // chunk must come from a different source chunk.
3405 //
3406 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3407 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3408 //
3409 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3410 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3411 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003412 // VSHUFPDY divides the resulting vector into 4 chunks.
3413 // The sources are also splitted into 4 chunks, and each destination
3414 // chunk must come from a different source chunk.
3415 //
3416 // SRC1 => X3 X2 X1 X0
3417 // SRC2 => Y3 Y2 Y1 Y0
3418 //
3419 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3420 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003421 unsigned HalfLaneElems = NumLaneElems/2;
3422 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3423 for (unsigned i = 0; i != NumLaneElems; ++i) {
3424 int Idx = Mask[i+l];
3425 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3426 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3427 return false;
3428 // For VSHUFPSY, the mask of the second half must be the same as the
3429 // first but with the appropriate offsets. This works in the same way as
3430 // VPERMILPS works with masks.
3431 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3432 continue;
3433 if (!isUndefOrEqual(Idx, Mask[i]+l))
3434 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003435 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003436 }
3437
3438 return true;
3439}
3440
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003441/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3442/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003443static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003444 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003445 return false;
3446
Craig Topper7a9a28b2012-08-12 02:23:29 +00003447 unsigned NumElems = VT.getVectorNumElements();
3448
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003449 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003450 return false;
3451
Evan Cheng2064a2b2006-03-28 06:50:32 +00003452 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003453 return isUndefOrEqual(Mask[0], 6) &&
3454 isUndefOrEqual(Mask[1], 7) &&
3455 isUndefOrEqual(Mask[2], 2) &&
3456 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003457}
3458
Nate Begeman0b10b912009-11-07 23:17:15 +00003459/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3460/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3461/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003462static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003463 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003464 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003465
Craig Topper7a9a28b2012-08-12 02:23:29 +00003466 unsigned NumElems = VT.getVectorNumElements();
3467
Nate Begeman0b10b912009-11-07 23:17:15 +00003468 if (NumElems != 4)
3469 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003470
Craig Topperdd637ae2012-02-19 05:41:45 +00003471 return isUndefOrEqual(Mask[0], 2) &&
3472 isUndefOrEqual(Mask[1], 3) &&
3473 isUndefOrEqual(Mask[2], 2) &&
3474 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003475}
3476
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3478/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003479static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003480 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003481 return false;
3482
Craig Topperdd637ae2012-02-19 05:41:45 +00003483 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003484
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485 if (NumElems != 2 && NumElems != 4)
3486 return false;
3487
Chad Rosier238ae312012-04-30 17:47:15 +00003488 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003489 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003490 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491
Chad Rosier238ae312012-04-30 17:47:15 +00003492 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003493 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003494 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003495
3496 return true;
3497}
3498
Nate Begeman0b10b912009-11-07 23:17:15 +00003499/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3500/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003501static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003502 if (!VT.is128BitVector())
3503 return false;
3504
Craig Topperdd637ae2012-02-19 05:41:45 +00003505 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003506
Craig Topper7a9a28b2012-08-12 02:23:29 +00003507 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003508 return false;
3509
Chad Rosier238ae312012-04-30 17:47:15 +00003510 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003511 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003512 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003513
Chad Rosier238ae312012-04-30 17:47:15 +00003514 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3515 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003516 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003517
3518 return true;
3519}
3520
Elena Demikhovsky15963732012-06-26 08:04:10 +00003521//
3522// Some special combinations that can be optimized.
3523//
3524static
3525SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3526 SelectionDAG &DAG) {
3527 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003528 DebugLoc dl = SVOp->getDebugLoc();
3529
3530 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3531 return SDValue();
3532
3533 ArrayRef<int> Mask = SVOp->getMask();
3534
3535 // These are the special masks that may be optimized.
3536 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3537 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3538 bool MatchEvenMask = true;
3539 bool MatchOddMask = true;
3540 for (int i=0; i<8; ++i) {
3541 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3542 MatchEvenMask = false;
3543 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3544 MatchOddMask = false;
3545 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003546
Elena Demikhovsky32510202012-09-04 12:49:02 +00003547 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003548 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003549
Elena Demikhovsky15963732012-06-26 08:04:10 +00003550 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3551
Elena Demikhovsky32510202012-09-04 12:49:02 +00003552 SDValue Op0 = SVOp->getOperand(0);
3553 SDValue Op1 = SVOp->getOperand(1);
3554
3555 if (MatchEvenMask) {
3556 // Shift the second operand right to 32 bits.
3557 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3558 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3559 } else {
3560 // Shift the first operand left to 32 bits.
3561 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3562 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3563 }
3564 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3565 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003566}
3567
Evan Cheng0038e592006-03-28 00:39:58 +00003568/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3569/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003570static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003571 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003572 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003573
3574 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3575 "Unsupported vector type for unpckh");
3576
Craig Topper6347e862011-11-21 06:57:39 +00003577 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003578 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003579 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003580
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003581 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3582 // independently on 128-bit lanes.
3583 unsigned NumLanes = VT.getSizeInBits()/128;
3584 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003585
Craig Topper94438ba2011-12-16 08:06:31 +00003586 for (unsigned l = 0; l != NumLanes; ++l) {
3587 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3588 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003589 i += 2, ++j) {
3590 int BitI = Mask[i];
3591 int BitI1 = Mask[i+1];
3592 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003593 return false;
David Greenea20244d2011-03-02 17:23:43 +00003594 if (V2IsSplat) {
3595 if (!isUndefOrEqual(BitI1, NumElts))
3596 return false;
3597 } else {
3598 if (!isUndefOrEqual(BitI1, j + NumElts))
3599 return false;
3600 }
Evan Cheng39623da2006-04-20 08:58:49 +00003601 }
Evan Cheng0038e592006-03-28 00:39:58 +00003602 }
David Greenea20244d2011-03-02 17:23:43 +00003603
Evan Cheng0038e592006-03-28 00:39:58 +00003604 return true;
3605}
3606
Evan Cheng4fcb9222006-03-28 02:43:26 +00003607/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3608/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003609static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003610 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003611 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003612
3613 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3614 "Unsupported vector type for unpckh");
3615
Craig Topper6347e862011-11-21 06:57:39 +00003616 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003617 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003618 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003619
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003620 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3621 // independently on 128-bit lanes.
3622 unsigned NumLanes = VT.getSizeInBits()/128;
3623 unsigned NumLaneElts = NumElts/NumLanes;
3624
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003625 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003626 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3627 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003628 int BitI = Mask[i];
3629 int BitI1 = Mask[i+1];
3630 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003631 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003632 if (V2IsSplat) {
3633 if (isUndefOrEqual(BitI1, NumElts))
3634 return false;
3635 } else {
3636 if (!isUndefOrEqual(BitI1, j+NumElts))
3637 return false;
3638 }
Evan Cheng39623da2006-04-20 08:58:49 +00003639 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003640 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003641 return true;
3642}
3643
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003644/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3645/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3646/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003647static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003648 bool HasAVX2) {
3649 unsigned NumElts = VT.getVectorNumElements();
3650
3651 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3652 "Unsupported vector type for unpckh");
3653
3654 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3655 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003656 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003657
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003658 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3659 // FIXME: Need a better way to get rid of this, there's no latency difference
3660 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3661 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003662 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003663 return false;
3664
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003665 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3666 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003667 unsigned NumLanes = VT.getSizeInBits()/128;
3668 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003669
Craig Topper94438ba2011-12-16 08:06:31 +00003670 for (unsigned l = 0; l != NumLanes; ++l) {
3671 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3672 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003673 i += 2, ++j) {
3674 int BitI = Mask[i];
3675 int BitI1 = Mask[i+1];
3676
3677 if (!isUndefOrEqual(BitI, j))
3678 return false;
3679 if (!isUndefOrEqual(BitI1, j))
3680 return false;
3681 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003682 }
David Greenea20244d2011-03-02 17:23:43 +00003683
Rafael Espindola15684b22009-04-24 12:40:33 +00003684 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003685}
3686
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003687/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3688/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3689/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003690static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003691 unsigned NumElts = VT.getVectorNumElements();
3692
3693 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3694 "Unsupported vector type for unpckh");
3695
3696 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3697 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003698 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Craig Topper94438ba2011-12-16 08:06:31 +00003700 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3701 // independently on 128-bit lanes.
3702 unsigned NumLanes = VT.getSizeInBits()/128;
3703 unsigned NumLaneElts = NumElts/NumLanes;
3704
3705 for (unsigned l = 0; l != NumLanes; ++l) {
3706 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3707 i != (l+1)*NumLaneElts; i += 2, ++j) {
3708 int BitI = Mask[i];
3709 int BitI1 = Mask[i+1];
3710 if (!isUndefOrEqual(BitI, j))
3711 return false;
3712 if (!isUndefOrEqual(BitI1, j))
3713 return false;
3714 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003715 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003716 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003717}
3718
Evan Cheng017dcc62006-04-21 01:05:10 +00003719/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3720/// specifies a shuffle of elements that is suitable for input to MOVSS,
3721/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003722static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003723 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003724 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003725 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003726 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003727
Craig Topperc612d792012-01-02 09:17:37 +00003728 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003729
Nate Begeman9008ca62009-04-27 18:41:29 +00003730 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003731 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003732
Craig Topperc612d792012-01-02 09:17:37 +00003733 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003734 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003735 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003736
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003737 return true;
3738}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003739
Craig Topper70b883b2011-11-28 10:14:51 +00003740/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003741/// as permutations between 128-bit chunks or halves. As an example: this
3742/// shuffle bellow:
3743/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3744/// The first half comes from the second half of V1 and the second half from the
3745/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003746static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003747 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003748 return false;
3749
3750 // The shuffle result is divided into half A and half B. In total the two
3751 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3752 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003753 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003754 bool MatchA = false, MatchB = false;
3755
3756 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003757 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003758 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3759 MatchA = true;
3760 break;
3761 }
3762 }
3763
3764 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003765 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003766 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3767 MatchB = true;
3768 break;
3769 }
3770 }
3771
3772 return MatchA && MatchB;
3773}
3774
Craig Topper70b883b2011-11-28 10:14:51 +00003775/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3776/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003777static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003778 EVT VT = SVOp->getValueType(0);
3779
Craig Topperc612d792012-01-02 09:17:37 +00003780 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003781
Craig Topperc612d792012-01-02 09:17:37 +00003782 unsigned FstHalf = 0, SndHalf = 0;
3783 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003784 if (SVOp->getMaskElt(i) > 0) {
3785 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3786 break;
3787 }
3788 }
Craig Topperc612d792012-01-02 09:17:37 +00003789 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003790 if (SVOp->getMaskElt(i) > 0) {
3791 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3792 break;
3793 }
3794 }
3795
3796 return (FstHalf | (SndHalf << 4));
3797}
3798
Craig Topper70b883b2011-11-28 10:14:51 +00003799/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003800/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3801/// Note that VPERMIL mask matching is different depending whether theunderlying
3802/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3803/// to the same elements of the low, but to the higher half of the source.
3804/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003805/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003806static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003807 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003808 return false;
3809
Craig Topperc612d792012-01-02 09:17:37 +00003810 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003811 // Only match 256-bit with 32/64-bit types
3812 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003813 return false;
3814
Craig Topperc612d792012-01-02 09:17:37 +00003815 unsigned NumLanes = VT.getSizeInBits()/128;
3816 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003817 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003818 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003819 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003820 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003821 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003822 continue;
3823 // VPERMILPS handling
3824 if (Mask[i] < 0)
3825 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003826 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003827 return false;
3828 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003829 }
3830
3831 return true;
3832}
3833
Craig Topper5aaffa82012-02-19 02:53:47 +00003834/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003835/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003836/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003837static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003839 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003840 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003841
3842 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003843 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003844 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003845
Nate Begeman9008ca62009-04-27 18:41:29 +00003846 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003847 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003848
Craig Topperc612d792012-01-02 09:17:37 +00003849 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3851 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3852 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003853 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003854
Evan Cheng39623da2006-04-20 08:58:49 +00003855 return true;
3856}
3857
Evan Chengd9539472006-04-14 21:59:03 +00003858/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3859/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003860/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003861static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003862 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003863 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003864 return false;
3865
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003866 unsigned NumElems = VT.getVectorNumElements();
3867
3868 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3869 (VT.getSizeInBits() == 256 && NumElems != 8))
3870 return false;
3871
3872 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003873 for (unsigned i = 0; i != NumElems; i += 2)
3874 if (!isUndefOrEqual(Mask[i], i+1) ||
3875 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003877
3878 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003879}
3880
3881/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3882/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003883/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003884static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003885 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003886 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003887 return false;
3888
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003889 unsigned NumElems = VT.getVectorNumElements();
3890
3891 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3892 (VT.getSizeInBits() == 256 && NumElems != 8))
3893 return false;
3894
3895 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003896 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003897 if (!isUndefOrEqual(Mask[i], i) ||
3898 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003900
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003901 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003902}
3903
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003904/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3905/// specifies a shuffle of elements that is suitable for input to 256-bit
3906/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003907static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003908 if (!HasAVX || !VT.is256BitVector())
3909 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003910
Craig Topper7a9a28b2012-08-12 02:23:29 +00003911 unsigned NumElts = VT.getVectorNumElements();
3912 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003913 return false;
3914
Craig Topperc612d792012-01-02 09:17:37 +00003915 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003916 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003917 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003918 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003919 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003920 return false;
3921 return true;
3922}
3923
Evan Cheng0b457f02008-09-25 20:50:48 +00003924/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003925/// specifies a shuffle of elements that is suitable for input to 128-bit
3926/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003927static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003928 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003929 return false;
3930
Craig Topperc612d792012-01-02 09:17:37 +00003931 unsigned e = VT.getVectorNumElements() / 2;
3932 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003933 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003934 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003935 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003936 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003937 return false;
3938 return true;
3939}
3940
David Greenec38a03e2011-02-03 15:50:00 +00003941/// isVEXTRACTF128Index - Return true if the specified
3942/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3943/// suitable for input to VEXTRACTF128.
3944bool X86::isVEXTRACTF128Index(SDNode *N) {
3945 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3946 return false;
3947
3948 // The index should be aligned on a 128-bit boundary.
3949 uint64_t Index =
3950 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3951
3952 unsigned VL = N->getValueType(0).getVectorNumElements();
3953 unsigned VBits = N->getValueType(0).getSizeInBits();
3954 unsigned ElSize = VBits / VL;
3955 bool Result = (Index * ElSize) % 128 == 0;
3956
3957 return Result;
3958}
3959
David Greeneccacdc12011-02-04 16:08:29 +00003960/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3961/// operand specifies a subvector insert that is suitable for input to
3962/// VINSERTF128.
3963bool X86::isVINSERTF128Index(SDNode *N) {
3964 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3965 return false;
3966
3967 // The index should be aligned on a 128-bit boundary.
3968 uint64_t Index =
3969 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3970
3971 unsigned VL = N->getValueType(0).getVectorNumElements();
3972 unsigned VBits = N->getValueType(0).getSizeInBits();
3973 unsigned ElSize = VBits / VL;
3974 bool Result = (Index * ElSize) % 128 == 0;
3975
3976 return Result;
3977}
3978
Evan Cheng63d33002006-03-22 08:01:21 +00003979/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003980/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003981/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003982static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003983 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003984
Craig Topper1a7700a2012-01-19 08:19:12 +00003985 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3986 "Unsupported vector type for PSHUF/SHUFP");
3987
3988 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3989 // independently on 128-bit lanes.
3990 unsigned NumElts = VT.getVectorNumElements();
3991 unsigned NumLanes = VT.getSizeInBits()/128;
3992 unsigned NumLaneElts = NumElts/NumLanes;
3993
3994 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3995 "Only supports 2 or 4 elements per lane");
3996
3997 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003998 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003999 for (unsigned i = 0; i != NumElts; ++i) {
4000 int Elt = N->getMaskElt(i);
4001 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004002 Elt &= NumLaneElts - 1;
4003 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004004 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004005 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004006
Evan Cheng63d33002006-03-22 08:01:21 +00004007 return Mask;
4008}
4009
Evan Cheng506d3df2006-03-29 23:07:14 +00004010/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004011/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004012static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004013 EVT VT = N->getValueType(0);
4014
4015 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4016 "Unsupported vector type for PSHUFHW");
4017
4018 unsigned NumElts = VT.getVectorNumElements();
4019
Evan Cheng506d3df2006-03-29 23:07:14 +00004020 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004021 for (unsigned l = 0; l != NumElts; l += 8) {
4022 // 8 nodes per lane, but we only care about the last 4.
4023 for (unsigned i = 0; i < 4; ++i) {
4024 int Elt = N->getMaskElt(l+i+4);
4025 if (Elt < 0) continue;
4026 Elt &= 0x3; // only 2-bits.
4027 Mask |= Elt << (i * 2);
4028 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004029 }
Craig Topper6b28d352012-05-03 07:12:59 +00004030
Evan Cheng506d3df2006-03-29 23:07:14 +00004031 return Mask;
4032}
4033
4034/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004035/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004036static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004037 EVT VT = N->getValueType(0);
4038
4039 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4040 "Unsupported vector type for PSHUFHW");
4041
4042 unsigned NumElts = VT.getVectorNumElements();
4043
Evan Cheng506d3df2006-03-29 23:07:14 +00004044 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004045 for (unsigned l = 0; l != NumElts; l += 8) {
4046 // 8 nodes per lane, but we only care about the first 4.
4047 for (unsigned i = 0; i < 4; ++i) {
4048 int Elt = N->getMaskElt(l+i);
4049 if (Elt < 0) continue;
4050 Elt &= 0x3; // only 2-bits
4051 Mask |= Elt << (i * 2);
4052 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 }
Craig Topper6b28d352012-05-03 07:12:59 +00004054
Evan Cheng506d3df2006-03-29 23:07:14 +00004055 return Mask;
4056}
4057
Nate Begemana09008b2009-10-19 02:17:23 +00004058/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4059/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004060static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4061 EVT VT = SVOp->getValueType(0);
4062 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004063
Craig Topper0e2037b2012-01-20 05:53:00 +00004064 unsigned NumElts = VT.getVectorNumElements();
4065 unsigned NumLanes = VT.getSizeInBits()/128;
4066 unsigned NumLaneElts = NumElts/NumLanes;
4067
4068 int Val = 0;
4069 unsigned i;
4070 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004071 Val = SVOp->getMaskElt(i);
4072 if (Val >= 0)
4073 break;
4074 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004075 if (Val >= (int)NumElts)
4076 Val -= NumElts - NumLaneElts;
4077
Eli Friedman63f8dde2011-07-25 21:36:45 +00004078 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004079 return (Val - i) * EltSize;
4080}
4081
David Greenec38a03e2011-02-03 15:50:00 +00004082/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4083/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4084/// instructions.
4085unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4086 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4087 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4088
4089 uint64_t Index =
4090 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4091
4092 EVT VecVT = N->getOperand(0).getValueType();
4093 EVT ElVT = VecVT.getVectorElementType();
4094
4095 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004096 return Index / NumElemsPerChunk;
4097}
4098
David Greeneccacdc12011-02-04 16:08:29 +00004099/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4100/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4101/// instructions.
4102unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4103 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4104 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4105
4106 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004107 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004108
4109 EVT VecVT = N->getValueType(0);
4110 EVT ElVT = VecVT.getVectorElementType();
4111
4112 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004113 return Index / NumElemsPerChunk;
4114}
4115
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004116/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4117/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4118/// Handles 256-bit.
4119static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4120 EVT VT = N->getValueType(0);
4121
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004122 unsigned NumElts = VT.getVectorNumElements();
4123
Craig Topper095c5282012-04-15 23:48:57 +00004124 assert((VT.is256BitVector() && NumElts == 4) &&
4125 "Unsupported vector type for VPERMQ/VPERMPD");
4126
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004127 unsigned Mask = 0;
4128 for (unsigned i = 0; i != NumElts; ++i) {
4129 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004130 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004131 continue;
4132 Mask |= Elt << (i*2);
4133 }
4134
4135 return Mask;
4136}
Evan Cheng37b73872009-07-30 08:33:02 +00004137/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4138/// constant +0.0.
4139bool X86::isZeroNode(SDValue Elt) {
4140 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004141 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004142 (isa<ConstantFPSDNode>(Elt) &&
4143 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4144}
4145
Nate Begeman9008ca62009-04-27 18:41:29 +00004146/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4147/// their permute mask.
4148static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4149 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004150 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004151 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004153
Nate Begeman5a5ca152009-04-29 05:20:52 +00004154 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004155 int Idx = SVOp->getMaskElt(i);
4156 if (Idx >= 0) {
4157 if (Idx < (int)NumElems)
4158 Idx += NumElems;
4159 else
4160 Idx -= NumElems;
4161 }
4162 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004163 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004164 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4165 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004166}
4167
Evan Cheng533a0aa2006-04-19 20:35:22 +00004168/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4169/// match movhlps. The lower half elements should come from upper half of
4170/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004171/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004172static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004173 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004174 return false;
4175 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004176 return false;
4177 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004178 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004179 return false;
4180 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004181 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004182 return false;
4183 return true;
4184}
4185
Evan Cheng5ced1d82006-04-06 23:23:56 +00004186/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004187/// is promoted to a vector. It also returns the LoadSDNode by reference if
4188/// required.
4189static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004190 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4191 return false;
4192 N = N->getOperand(0).getNode();
4193 if (!ISD::isNON_EXTLoad(N))
4194 return false;
4195 if (LD)
4196 *LD = cast<LoadSDNode>(N);
4197 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004198}
4199
Dan Gohman65fd6562011-11-03 21:49:52 +00004200// Test whether the given value is a vector value which will be legalized
4201// into a load.
4202static bool WillBeConstantPoolLoad(SDNode *N) {
4203 if (N->getOpcode() != ISD::BUILD_VECTOR)
4204 return false;
4205
4206 // Check for any non-constant elements.
4207 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4208 switch (N->getOperand(i).getNode()->getOpcode()) {
4209 case ISD::UNDEF:
4210 case ISD::ConstantFP:
4211 case ISD::Constant:
4212 break;
4213 default:
4214 return false;
4215 }
4216
4217 // Vectors of all-zeros and all-ones are materialized with special
4218 // instructions rather than being loaded.
4219 return !ISD::isBuildVectorAllZeros(N) &&
4220 !ISD::isBuildVectorAllOnes(N);
4221}
4222
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4224/// match movlp{s|d}. The lower half elements should come from lower half of
4225/// V1 (and in order), and the upper half elements should come from the upper
4226/// half of V2 (and in order). And since V1 will become the source of the
4227/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004228static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004229 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004230 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004231 return false;
4232
Evan Cheng466685d2006-10-09 20:57:25 +00004233 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004234 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004235 // Is V2 is a vector load, don't do this transformation. We will try to use
4236 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004237 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004239
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004240 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004241
Evan Cheng533a0aa2006-04-19 20:35:22 +00004242 if (NumElems != 2 && NumElems != 4)
4243 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004244 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004245 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004246 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004247 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004248 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004249 return false;
4250 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251}
4252
Evan Cheng39623da2006-04-20 08:58:49 +00004253/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4254/// all the same.
4255static bool isSplatVector(SDNode *N) {
4256 if (N->getOpcode() != ISD::BUILD_VECTOR)
4257 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004258
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004260 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4261 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004262 return false;
4263 return true;
4264}
4265
Evan Cheng213d2cf2007-05-17 18:45:50 +00004266/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004267/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004268/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004269static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004270 SDValue V1 = N->getOperand(0);
4271 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004272 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4273 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004275 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004277 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4278 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004279 if (Opc != ISD::BUILD_VECTOR ||
4280 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004281 return false;
4282 } else if (Idx >= 0) {
4283 unsigned Opc = V1.getOpcode();
4284 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4285 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004286 if (Opc != ISD::BUILD_VECTOR ||
4287 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004288 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004289 }
4290 }
4291 return true;
4292}
4293
4294/// getZeroVector - Returns a vector of specified type with all zero elements.
4295///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004296static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004297 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004298 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004299 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004300
Dale Johannesen0488fb62010-09-30 23:57:10 +00004301 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004302 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004304 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004305 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004306 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4307 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4308 } else { // SSE1
4309 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4310 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4311 }
Craig Topper9d352402012-04-23 07:24:41 +00004312 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004313 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004314 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4315 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4316 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4317 } else {
4318 // 256-bit logic and arithmetic instructions in AVX are all
4319 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4320 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4321 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4322 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4323 }
Craig Topper9d352402012-04-23 07:24:41 +00004324 } else
4325 llvm_unreachable("Unexpected vector type");
4326
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004327 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004328}
4329
Chris Lattner8a594482007-11-25 00:24:49 +00004330/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004331/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4332/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4333/// Then bitcast to their original type, ensuring they get CSE'd.
4334static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4335 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004336 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004337 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004338
Owen Anderson825b72b2009-08-11 20:47:22 +00004339 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004340 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004341 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004342 if (HasAVX2) { // AVX2
4343 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4344 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4345 } else { // AVX
4346 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004347 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004348 }
Craig Topper9d352402012-04-23 07:24:41 +00004349 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004350 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004351 } else
4352 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004353
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004354 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004355}
4356
Evan Cheng39623da2006-04-20 08:58:49 +00004357/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4358/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004359static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004360 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004361 if (Mask[i] > (int)NumElems) {
4362 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004363 }
Evan Cheng39623da2006-04-20 08:58:49 +00004364 }
Evan Cheng39623da2006-04-20 08:58:49 +00004365}
4366
Evan Cheng017dcc62006-04-21 01:05:10 +00004367/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4368/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004369static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 SDValue V2) {
4371 unsigned NumElems = VT.getVectorNumElements();
4372 SmallVector<int, 8> Mask;
4373 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004374 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004375 Mask.push_back(i);
4376 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004377}
4378
Nate Begeman9008ca62009-04-27 18:41:29 +00004379/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004380static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 SDValue V2) {
4382 unsigned NumElems = VT.getVectorNumElements();
4383 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004384 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 Mask.push_back(i);
4386 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004387 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004389}
4390
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004392static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 SDValue V2) {
4394 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004396 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 Mask.push_back(i + Half);
4398 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004399 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004401}
4402
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004403// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404// a generic shuffle instruction because the target has no such instructions.
4405// Generate shuffles which repeat i16 and i8 several times until they can be
4406// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004407static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004410 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004411
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 while (NumElems > 4) {
4413 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 EltNo -= NumElems/2;
4418 }
4419 NumElems >>= 1;
4420 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004421 return V;
4422}
Eric Christopherfd179292009-08-27 18:07:15 +00004423
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4425static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4426 EVT VT = V.getValueType();
4427 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004428 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004429
Craig Topper9d352402012-04-23 07:24:41 +00004430 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004431 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004433 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4434 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004435 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004436 // To use VPERMILPS to splat scalars, the second half of indicies must
4437 // refer to the higher part, which is a duplication of the lower one,
4438 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004439 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4440 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004441
4442 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4443 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4444 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004445 } else
4446 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447
4448 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4449}
4450
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004451/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4453 EVT SrcVT = SV->getValueType(0);
4454 SDValue V1 = SV->getOperand(0);
4455 DebugLoc dl = SV->getDebugLoc();
4456
4457 int EltNo = SV->getSplatIndex();
4458 int NumElems = SrcVT.getVectorNumElements();
4459 unsigned Size = SrcVT.getSizeInBits();
4460
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004461 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4462 "Unknown how to promote splat for type");
4463
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 // Extract the 128-bit part containing the splat element and update
4465 // the splat element index when it refers to the higher register.
4466 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004467 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4468 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469 EltNo -= NumElems/2;
4470 }
4471
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004472 // All i16 and i8 vector types can't be used directly by a generic shuffle
4473 // instruction because the target has no such instruction. Generate shuffles
4474 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004475 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004476 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004477 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004478 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004479
4480 // Recreate the 256-bit vector and place the same 128-bit vector
4481 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004482 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004484 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004485 }
4486
4487 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004488}
4489
Evan Chengba05f722006-04-21 23:03:30 +00004490/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004491/// vector of zero or undef vector. This produces a shuffle where the low
4492/// element of V2 is swizzled into the zero/undef vector, landing at element
4493/// 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 +00004494static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004495 bool IsZero,
4496 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004497 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004498 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004499 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004500 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 unsigned NumElems = VT.getVectorNumElements();
4502 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004503 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 // If this is the insertion idx, put the low elt of V2 here.
4505 MaskVec.push_back(i == Idx ? NumElems : i);
4506 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004507}
4508
Craig Toppera1ffc682012-03-20 06:42:26 +00004509/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4510/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004511/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004512static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004513 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004514 unsigned NumElems = VT.getVectorNumElements();
4515 SDValue ImmN;
4516
Craig Topper89f4e662012-03-20 07:17:59 +00004517 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004518 switch(N->getOpcode()) {
4519 case X86ISD::SHUFP:
4520 ImmN = N->getOperand(N->getNumOperands()-1);
4521 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4522 break;
4523 case X86ISD::UNPCKH:
4524 DecodeUNPCKHMask(VT, Mask);
4525 break;
4526 case X86ISD::UNPCKL:
4527 DecodeUNPCKLMask(VT, Mask);
4528 break;
4529 case X86ISD::MOVHLPS:
4530 DecodeMOVHLPSMask(NumElems, Mask);
4531 break;
4532 case X86ISD::MOVLHPS:
4533 DecodeMOVLHPSMask(NumElems, Mask);
4534 break;
4535 case X86ISD::PSHUFD:
4536 case X86ISD::VPERMILP:
4537 ImmN = N->getOperand(N->getNumOperands()-1);
4538 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004539 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004540 break;
4541 case X86ISD::PSHUFHW:
4542 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004543 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004544 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004545 break;
4546 case X86ISD::PSHUFLW:
4547 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004548 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004549 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004550 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004551 case X86ISD::VPERMI:
4552 ImmN = N->getOperand(N->getNumOperands()-1);
4553 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4554 IsUnary = true;
4555 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004556 case X86ISD::MOVSS:
4557 case X86ISD::MOVSD: {
4558 // The index 0 always comes from the first element of the second source,
4559 // this is why MOVSS and MOVSD are used in the first place. The other
4560 // elements come from the other positions of the first source vector
4561 Mask.push_back(NumElems);
4562 for (unsigned i = 1; i != NumElems; ++i) {
4563 Mask.push_back(i);
4564 }
4565 break;
4566 }
4567 case X86ISD::VPERM2X128:
4568 ImmN = N->getOperand(N->getNumOperands()-1);
4569 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004570 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004571 break;
4572 case X86ISD::MOVDDUP:
4573 case X86ISD::MOVLHPD:
4574 case X86ISD::MOVLPD:
4575 case X86ISD::MOVLPS:
4576 case X86ISD::MOVSHDUP:
4577 case X86ISD::MOVSLDUP:
4578 case X86ISD::PALIGN:
4579 // Not yet implemented
4580 return false;
4581 default: llvm_unreachable("unknown target shuffle node");
4582 }
4583
4584 return true;
4585}
4586
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004587/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4588/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004589static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004590 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004591 if (Depth == 6)
4592 return SDValue(); // Limit search depth.
4593
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004594 SDValue V = SDValue(N, 0);
4595 EVT VT = V.getValueType();
4596 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004597
4598 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4599 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004600 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601
Craig Topper3d092db2012-03-21 02:14:01 +00004602 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004603 return DAG.getUNDEF(VT.getVectorElementType());
4604
Craig Topperd156dc12012-02-06 07:17:51 +00004605 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004606 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4607 : SV->getOperand(1);
4608 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004609 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004610
4611 // Recurse into target specific vector shuffles to find scalars.
4612 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004613 MVT ShufVT = V.getValueType().getSimpleVT();
4614 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004615 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004616 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004617 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004618
Craig Topperd978c542012-05-06 19:46:21 +00004619 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004620 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004621
Craig Topper3d092db2012-03-21 02:14:01 +00004622 int Elt = ShuffleMask[Index];
4623 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004624 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004625
Craig Topper3d092db2012-03-21 02:14:01 +00004626 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004627 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004628 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004629 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 }
4631
4632 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004633 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 V = V.getOperand(0);
4635 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004636 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004637
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004638 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 return SDValue();
4640 }
4641
4642 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4643 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004644 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645
4646 if (V.getOpcode() == ISD::BUILD_VECTOR)
4647 return V.getOperand(Index);
4648
4649 return SDValue();
4650}
4651
4652/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4653/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004654/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655static
Craig Topper3d092db2012-03-21 02:14:01 +00004656unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004658 unsigned i;
4659 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004661 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004662 if (!(Elt.getNode() &&
4663 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4664 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004665 }
4666
4667 return i;
4668}
4669
Craig Topper3d092db2012-03-21 02:14:01 +00004670/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4671/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4673static
Craig Topper3d092db2012-03-21 02:14:01 +00004674bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4675 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4676 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004677 bool SeenV1 = false;
4678 bool SeenV2 = false;
4679
Craig Topper3d092db2012-03-21 02:14:01 +00004680 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004681 int Idx = SVOp->getMaskElt(i);
4682 // Ignore undef indicies
4683 if (Idx < 0)
4684 continue;
4685
Craig Topper3d092db2012-03-21 02:14:01 +00004686 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004687 SeenV1 = true;
4688 else
4689 SeenV2 = true;
4690
4691 // Only accept consecutive elements from the same vector
4692 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4693 return false;
4694 }
4695
4696 OpNum = SeenV1 ? 0 : 1;
4697 return true;
4698}
4699
4700/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4701/// logical left shift of a vector.
4702static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4703 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4704 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4705 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4706 false /* check zeros from right */, DAG);
4707 unsigned OpSrc;
4708
4709 if (!NumZeros)
4710 return false;
4711
4712 // Considering the elements in the mask that are not consecutive zeros,
4713 // check if they consecutively come from only one of the source vectors.
4714 //
4715 // V1 = {X, A, B, C} 0
4716 // \ \ \ /
4717 // vector_shuffle V1, V2 <1, 2, 3, X>
4718 //
4719 if (!isShuffleMaskConsecutive(SVOp,
4720 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004721 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004722 NumZeros, // Where to start looking in the src vector
4723 NumElems, // Number of elements in vector
4724 OpSrc)) // Which source operand ?
4725 return false;
4726
4727 isLeft = false;
4728 ShAmt = NumZeros;
4729 ShVal = SVOp->getOperand(OpSrc);
4730 return true;
4731}
4732
4733/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4734/// logical left shift of a vector.
4735static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4736 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4737 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4738 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4739 true /* check zeros from left */, DAG);
4740 unsigned OpSrc;
4741
4742 if (!NumZeros)
4743 return false;
4744
4745 // Considering the elements in the mask that are not consecutive zeros,
4746 // check if they consecutively come from only one of the source vectors.
4747 //
4748 // 0 { A, B, X, X } = V2
4749 // / \ / /
4750 // vector_shuffle V1, V2 <X, X, 4, 5>
4751 //
4752 if (!isShuffleMaskConsecutive(SVOp,
4753 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004754 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004755 0, // Where to start looking in the src vector
4756 NumElems, // Number of elements in vector
4757 OpSrc)) // Which source operand ?
4758 return false;
4759
4760 isLeft = true;
4761 ShAmt = NumZeros;
4762 ShVal = SVOp->getOperand(OpSrc);
4763 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004764}
4765
4766/// isVectorShift - Returns true if the shuffle can be implemented as a
4767/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004768static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004769 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004770 // Although the logic below support any bitwidth size, there are no
4771 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004772 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004773 return false;
4774
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004775 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4776 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4777 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004778
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004779 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004780}
4781
Evan Chengc78d3b42006-04-24 18:01:45 +00004782/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4783///
Dan Gohman475871a2008-07-27 21:46:04 +00004784static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004786 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004787 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004788 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004789 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004790 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004791
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004792 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004793 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004794 bool First = true;
4795 for (unsigned i = 0; i < 16; ++i) {
4796 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4797 if (ThisIsNonZero && First) {
4798 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004799 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004800 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004802 First = false;
4803 }
4804
4805 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4808 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004809 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 }
4812 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4814 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4815 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 } else
4819 ThisElt = LastElt;
4820
Gabor Greifba36cb52008-08-28 21:40:38 +00004821 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004823 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 }
4825 }
4826
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004827 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004828}
4829
Bill Wendlinga348c562007-03-22 18:42:45 +00004830/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004831///
Dan Gohman475871a2008-07-27 21:46:04 +00004832static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004833 unsigned NumNonZero, unsigned NumZero,
4834 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004835 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004836 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004837 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004838 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004839
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004840 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004841 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004842 bool First = true;
4843 for (unsigned i = 0; i < 8; ++i) {
4844 bool isNonZero = (NonZeros & (1 << i)) != 0;
4845 if (isNonZero) {
4846 if (First) {
4847 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004848 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 First = false;
4852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004853 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004855 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 }
4857 }
4858
4859 return V;
4860}
4861
Evan Chengf26ffe92008-05-29 08:22:04 +00004862/// getVShift - Return a vector logical shift node.
4863///
Owen Andersone50ed302009-08-10 22:56:29 +00004864static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004865 unsigned NumBits, SelectionDAG &DAG,
4866 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004867 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004868 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004869 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004870 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4871 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004872 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004873 DAG.getConstant(NumBits,
4874 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004875}
4876
Dan Gohman475871a2008-07-27 21:46:04 +00004877SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004878X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004879 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004880
Evan Chengc3630942009-12-09 21:00:30 +00004881 // Check if the scalar load can be widened into a vector load. And if
4882 // the address is "base + cst" see if the cst can be "absorbed" into
4883 // the shuffle mask.
4884 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4885 SDValue Ptr = LD->getBasePtr();
4886 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4887 return SDValue();
4888 EVT PVT = LD->getValueType(0);
4889 if (PVT != MVT::i32 && PVT != MVT::f32)
4890 return SDValue();
4891
4892 int FI = -1;
4893 int64_t Offset = 0;
4894 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4895 FI = FINode->getIndex();
4896 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004897 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004898 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4899 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4900 Offset = Ptr.getConstantOperandVal(1);
4901 Ptr = Ptr.getOperand(0);
4902 } else {
4903 return SDValue();
4904 }
4905
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004906 // FIXME: 256-bit vector instructions don't require a strict alignment,
4907 // improve this code to support it better.
4908 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004909 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004911 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004912 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004913 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004914 // Can't change the alignment. FIXME: It's possible to compute
4915 // the exact stack offset and reference FI + adjust offset instead.
4916 // If someone *really* cares about this. That's the way to implement it.
4917 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004918 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004919 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004920 }
4921 }
4922
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004923 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004924 // Ptr + (Offset & ~15).
4925 if (Offset < 0)
4926 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004928 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004929 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004930 if (StartOffset)
4931 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4932 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4933
4934 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004935 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004936
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4938 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004939 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004940 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004941
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004942 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004943 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004944 Mask.push_back(EltNo);
4945
Craig Toppercc3000632012-01-30 07:50:31 +00004946 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004947 }
4948
4949 return SDValue();
4950}
4951
Michael J. Spencerec38de22010-10-10 22:04:20 +00004952/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4953/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004954/// load which has the same value as a build_vector whose operands are 'elts'.
4955///
4956/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004957///
Nate Begeman1449f292010-03-24 22:19:06 +00004958/// FIXME: we'd also like to handle the case where the last elements are zero
4959/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4960/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004961static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004962 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004963 EVT EltVT = VT.getVectorElementType();
4964 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965
Nate Begemanfdea31a2010-03-24 20:49:50 +00004966 LoadSDNode *LDBase = NULL;
4967 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004968
Nate Begeman1449f292010-03-24 22:19:06 +00004969 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004970 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004971 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972 for (unsigned i = 0; i < NumElems; ++i) {
4973 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004974
Nate Begemanfdea31a2010-03-24 20:49:50 +00004975 if (!Elt.getNode() ||
4976 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4977 return SDValue();
4978 if (!LDBase) {
4979 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4980 return SDValue();
4981 LDBase = cast<LoadSDNode>(Elt.getNode());
4982 LastLoadedElt = i;
4983 continue;
4984 }
4985 if (Elt.getOpcode() == ISD::UNDEF)
4986 continue;
4987
4988 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4989 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4990 return SDValue();
4991 LastLoadedElt = i;
4992 }
Nate Begeman1449f292010-03-24 22:19:06 +00004993
4994 // If we have found an entire vector of loads and undefs, then return a large
4995 // load of the entire vector width starting at the base pointer. If we found
4996 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004997 if (LastLoadedElt == NumElems - 1) {
4998 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004999 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005000 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005001 LDBase->isVolatile(), LDBase->isNonTemporal(),
5002 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005003 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005004 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005005 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005006 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005007 }
5008 if (NumElems == 4 && LastLoadedElt == 1 &&
5009 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5011 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005012 SDValue ResNode =
5013 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5014 LDBase->getPointerInfo(),
5015 LDBase->getAlignment(),
5016 false/*isVolatile*/, true/*ReadMem*/,
5017 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005018
5019 // Make sure the newly-created LOAD is in the same position as LDBase in
5020 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5021 // update uses of LDBase's output chain to use the TokenFactor.
5022 if (LDBase->hasAnyUseOfValue(1)) {
5023 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5024 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5025 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5026 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5027 SDValue(ResNode.getNode(), 1));
5028 }
5029
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005030 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 }
5032 return SDValue();
5033}
5034
Nadav Rotem9d68b062012-04-08 12:54:54 +00005035/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5036/// to generate a splat value for the following cases:
5037/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005039/// a scalar load, or a constant.
5040/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005041/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005042SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005043X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005044 if (!Subtarget->hasAVX())
5045 return SDValue();
5046
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005047 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005048 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005049
Craig Topper5da8a802012-05-04 05:49:51 +00005050 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5051 "Unsupported vector type for broadcast.");
5052
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005053 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005054 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005055
Nadav Rotem9d68b062012-04-08 12:54:54 +00005056 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005057 default:
5058 // Unknown pattern found.
5059 return SDValue();
5060
5061 case ISD::BUILD_VECTOR: {
5062 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005063 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005064 return SDValue();
5065
Nadav Rotem9d68b062012-04-08 12:54:54 +00005066 Ld = Op.getOperand(0);
5067 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5068 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005069
5070 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005071 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005072 // Constants may have multiple users.
5073 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005074 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005075 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005076 }
5077
5078 case ISD::VECTOR_SHUFFLE: {
5079 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5080
5081 // Shuffles must have a splat mask where the first element is
5082 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005083 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005084 return SDValue();
5085
5086 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005087 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005088 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5089
5090 if (!Subtarget->hasAVX2())
5091 return SDValue();
5092
5093 // Use the register form of the broadcast instruction available on AVX2.
5094 if (VT.is256BitVector())
5095 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5096 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5097 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005098
5099 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005100 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005101 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102
5103 // The scalar_to_vector node and the suspected
5104 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005105 // Constants may have multiple users.
5106 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005107 return SDValue();
5108 break;
5109 }
5110 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005111
Craig Topper7a9a28b2012-08-12 02:23:29 +00005112 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113
5114 // Handle the broadcasting a single constant scalar from the constant pool
5115 // into a vector. On Sandybridge it is still better to load a constant vector
5116 // from the constant pool and not to broadcast it from a scalar.
5117 if (ConstSplatVal && Subtarget->hasAVX2()) {
5118 EVT CVT = Ld.getValueType();
5119 assert(!CVT.isVector() && "Must not broadcast a vector type");
5120 unsigned ScalarSize = CVT.getSizeInBits();
5121
Craig Topper5da8a802012-05-04 05:49:51 +00005122 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005123 const Constant *C = 0;
5124 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5125 C = CI->getConstantIntValue();
5126 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5127 C = CF->getConstantFPValue();
5128
5129 assert(C && "Invalid constant type");
5130
Nadav Rotem154819d2012-04-09 07:45:58 +00005131 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005132 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005133 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005134 MachinePointerInfo::getConstantPool(),
5135 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005136
Nadav Rotem9d68b062012-04-08 12:54:54 +00005137 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5138 }
5139 }
5140
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005141 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005142 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5143
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005144 // Handle AVX2 in-register broadcasts.
5145 if (!IsLoad && Subtarget->hasAVX2() &&
5146 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5147 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5148
5149 // The scalar source must be a normal load.
5150 if (!IsLoad)
5151 return SDValue();
5152
Craig Topper5da8a802012-05-04 05:49:51 +00005153 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005155
Craig Toppera9376332012-01-10 08:23:59 +00005156 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005157 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005158 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005159 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005160 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005161 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005162
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005163 // Unsupported broadcast.
5164 return SDValue();
5165}
5166
Evan Chengc3630942009-12-09 21:00:30 +00005167SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005168X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005169 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005170
David Greenef125a292011-02-08 19:04:41 +00005171 EVT VT = Op.getValueType();
5172 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005173 unsigned NumElems = Op.getNumOperands();
5174
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005175 // Vectors containing all zeros can be matched by pxor and xorps later
5176 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5177 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5178 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005179 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005180 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005181
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005182 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005183 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005184
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005185 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005186 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5187 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005188 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005189 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005190 return Op;
5191
Craig Topper07a27622012-01-22 03:07:48 +00005192 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005193 }
5194
Nadav Rotem154819d2012-04-09 07:45:58 +00005195 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005196 if (Broadcast.getNode())
5197 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005198
Owen Andersone50ed302009-08-10 22:56:29 +00005199 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005200
Evan Cheng0db9fe62006-04-25 20:13:52 +00005201 unsigned NumZero = 0;
5202 unsigned NumNonZero = 0;
5203 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005204 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005205 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005206 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005207 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005208 if (Elt.getOpcode() == ISD::UNDEF)
5209 continue;
5210 Values.insert(Elt);
5211 if (Elt.getOpcode() != ISD::Constant &&
5212 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005213 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005214 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005215 NumZero++;
5216 else {
5217 NonZeros |= (1 << i);
5218 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005219 }
5220 }
5221
Chris Lattner97a2a562010-08-26 05:24:29 +00005222 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5223 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005224 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005225
Chris Lattner67f453a2008-03-09 05:42:06 +00005226 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005227 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005229 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005230
Chris Lattner62098042008-03-09 01:05:04 +00005231 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5232 // the value are obviously zero, truncate the value to i32 and do the
5233 // insertion that way. Only do this if the value is non-constant or if the
5234 // value is a constant being inserted into element 0. It is cheaper to do
5235 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005236 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005237 (!IsAllConstants || Idx == 0)) {
5238 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005239 // Handle SSE only.
5240 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5241 EVT VecVT = MVT::v4i32;
5242 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005243
Chris Lattner62098042008-03-09 01:05:04 +00005244 // Truncate the value (which may itself be a constant) to i32, and
5245 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005247 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005248 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005249
Chris Lattner62098042008-03-09 01:05:04 +00005250 // Now we have our 32-bit value zero extended in the low element of
5251 // a vector. If Idx != 0, swizzle it into place.
5252 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005253 SmallVector<int, 4> Mask;
5254 Mask.push_back(Idx);
5255 for (unsigned i = 1; i != VecElts; ++i)
5256 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005257 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005258 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005259 }
Craig Topper07a27622012-01-22 03:07:48 +00005260 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005261 }
5262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005263
Chris Lattner19f79692008-03-08 22:59:52 +00005264 // If we have a constant or non-constant insertion into the low element of
5265 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5266 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005267 // depending on what the source datatype is.
5268 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005269 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005270 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005271
5272 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005273 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005274 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005275 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005276 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5277 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005278 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005279 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005280 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5281 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005282 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005283 }
5284
5285 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005286 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005287 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005288 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005289 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005290 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005291 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005292 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005293 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005295 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005296 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005297 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005298
5299 // Is it a vector logical left shift?
5300 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005301 X86::isZeroNode(Op.getOperand(0)) &&
5302 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005303 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005304 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005305 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005306 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005307 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005308 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005310 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005311 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005312
Chris Lattner19f79692008-03-08 22:59:52 +00005313 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5314 // is a non-constant being inserted into an element other than the low one,
5315 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5316 // movd/movss) to move this into the low element, then shuffle it into
5317 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005318 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005319 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005320
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005322 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005323 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005324 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 MaskVec.push_back(i == Idx ? 0 : 1);
5326 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005327 }
5328 }
5329
Chris Lattner67f453a2008-03-09 05:42:06 +00005330 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005331 if (Values.size() == 1) {
5332 if (EVTBits == 32) {
5333 // Instead of a shuffle like this:
5334 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5335 // Check if it's possible to issue this instead.
5336 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5337 unsigned Idx = CountTrailingZeros_32(NonZeros);
5338 SDValue Item = Op.getOperand(Idx);
5339 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5340 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5341 }
Dan Gohman475871a2008-07-27 21:46:04 +00005342 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005343 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005344
Dan Gohmana3941172007-07-24 22:55:08 +00005345 // A vector full of immediates; various special cases are already
5346 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005347 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005348 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005349
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005350 // For AVX-length vectors, build the individual 128-bit pieces and use
5351 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005352 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005353 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005354 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005355 V.push_back(Op.getOperand(i));
5356
5357 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5358
5359 // Build both the lower and upper subvector.
5360 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5361 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5362 NumElems/2);
5363
5364 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005365 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005366 }
5367
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005368 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005369 if (EVTBits == 64) {
5370 if (NumNonZero == 1) {
5371 // One half is zero or undef.
5372 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005373 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005374 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005375 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005376 }
Dan Gohman475871a2008-07-27 21:46:04 +00005377 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005378 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379
5380 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005381 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005382 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005383 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005384 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385 }
5386
Bill Wendling826f36f2007-03-28 00:57:11 +00005387 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005388 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005389 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005390 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 }
5392
5393 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005394 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005395 if (NumElems == 4 && NumZero > 0) {
5396 for (unsigned i = 0; i < 4; ++i) {
5397 bool isZero = !(NonZeros & (1 << i));
5398 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005399 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005400 else
Dale Johannesenace16102009-02-03 19:33:06 +00005401 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005402 }
5403
5404 for (unsigned i = 0; i < 2; ++i) {
5405 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5406 default: break;
5407 case 0:
5408 V[i] = V[i*2]; // Must be a zero vector.
5409 break;
5410 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005411 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005412 break;
5413 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005414 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005415 break;
5416 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418 break;
5419 }
5420 }
5421
Benjamin Kramer9c683542012-01-30 15:16:21 +00005422 bool Reverse1 = (NonZeros & 0x3) == 2;
5423 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5424 int MaskVec[] = {
5425 Reverse1 ? 1 : 0,
5426 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005427 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5428 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005429 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005430 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005431 }
5432
Craig Topper7a9a28b2012-08-12 02:23:29 +00005433 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005434 // Check for a build vector of consecutive loads.
5435 for (unsigned i = 0; i < NumElems; ++i)
5436 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005437
Nate Begemanfdea31a2010-03-24 20:49:50 +00005438 // Check for elements which are consecutive loads.
5439 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5440 if (LD.getNode())
5441 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005442
5443 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005444 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005445 SDValue Result;
5446 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5447 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5448 else
5449 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005450
Chris Lattner24faf612010-08-28 17:59:08 +00005451 for (unsigned i = 1; i < NumElems; ++i) {
5452 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5453 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005455 }
5456 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005457 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005458
Chris Lattner6e80e442010-08-28 17:15:43 +00005459 // Otherwise, expand into a number of unpckl*, start by extending each of
5460 // our (non-undef) elements to the full vector width with the element in the
5461 // bottom slot of the vector (which generates no code for SSE).
5462 for (unsigned i = 0; i < NumElems; ++i) {
5463 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5464 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5465 else
5466 V[i] = DAG.getUNDEF(VT);
5467 }
5468
5469 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5471 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5472 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005473 unsigned EltStride = NumElems >> 1;
5474 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005475 for (unsigned i = 0; i < EltStride; ++i) {
5476 // If V[i+EltStride] is undef and this is the first round of mixing,
5477 // then it is safe to just drop this shuffle: V[i] is already in the
5478 // right place, the one element (since it's the first round) being
5479 // inserted as undef can be dropped. This isn't safe for successive
5480 // rounds because they will permute elements within both vectors.
5481 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5482 EltStride == NumElems/2)
5483 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005484
Chris Lattner6e80e442010-08-28 17:15:43 +00005485 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005486 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005487 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488 }
5489 return V[0];
5490 }
Dan Gohman475871a2008-07-27 21:46:04 +00005491 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492}
5493
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005494// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5495// to create 256-bit vectors from two other 128-bit ones.
5496static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5497 DebugLoc dl = Op.getDebugLoc();
5498 EVT ResVT = Op.getValueType();
5499
Craig Topper7a9a28b2012-08-12 02:23:29 +00005500 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005501
5502 SDValue V1 = Op.getOperand(0);
5503 SDValue V2 = Op.getOperand(1);
5504 unsigned NumElems = ResVT.getVectorNumElements();
5505
Craig Topper4c7972d2012-04-22 18:15:59 +00005506 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005507}
5508
Craig Topper55b24052012-09-11 06:15:32 +00005509static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005510 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005511
5512 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5513 // from two other 128-bit ones.
5514 return LowerAVXCONCAT_VECTORS(Op, DAG);
5515}
5516
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005517// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005518static SDValue
5519LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5520 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005521 SDValue V1 = SVOp->getOperand(0);
5522 SDValue V2 = SVOp->getOperand(1);
5523 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005524 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005525 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005526
Nadav Roteme6113782012-04-11 06:40:27 +00005527 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005528 return SDValue();
5529
Craig Topper1842ba02012-04-23 06:38:28 +00005530 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005531 MVT OpTy;
5532
Craig Topper708e44f2012-04-23 07:36:33 +00005533 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005534 default: return SDValue();
5535 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005536 ISDNo = X86ISD::BLENDPW;
5537 OpTy = MVT::v8i16;
5538 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005539 case MVT::v4i32:
5540 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005541 ISDNo = X86ISD::BLENDPS;
5542 OpTy = MVT::v4f32;
5543 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005544 case MVT::v2i64:
5545 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005546 ISDNo = X86ISD::BLENDPD;
5547 OpTy = MVT::v2f64;
5548 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005549 case MVT::v8i32:
5550 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005551 if (!Subtarget->hasAVX())
5552 return SDValue();
5553 ISDNo = X86ISD::BLENDPS;
5554 OpTy = MVT::v8f32;
5555 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005556 case MVT::v4i64:
5557 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005558 if (!Subtarget->hasAVX())
5559 return SDValue();
5560 ISDNo = X86ISD::BLENDPD;
5561 OpTy = MVT::v4f64;
5562 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005563 }
5564 assert(ISDNo && "Invalid Op Number");
5565
5566 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005567
Craig Topper1842ba02012-04-23 06:38:28 +00005568 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005569 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005570 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005571 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005572 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005573 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005574 else
5575 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005576 }
5577
Nadav Roteme6113782012-04-11 06:40:27 +00005578 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5579 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5580 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5581 DAG.getConstant(MaskVals, MVT::i32));
5582 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005583}
5584
Nate Begemanb9a47b82009-02-23 08:49:38 +00005585// v8i16 shuffles - Prefer shuffles in the following order:
5586// 1. [all] pshuflw, pshufhw, optional move
5587// 2. [ssse3] 1 x pshufb
5588// 3. [ssse3] 2 x pshufb + 1 x por
5589// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005590static SDValue
5591LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5592 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005593 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 SDValue V1 = SVOp->getOperand(0);
5595 SDValue V2 = SVOp->getOperand(1);
5596 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005597 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005598
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 // Determine if more than 1 of the words in each of the low and high quadwords
5600 // of the result come from the same quadword of one of the two inputs. Undef
5601 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005602 unsigned LoQuad[] = { 0, 0, 0, 0 };
5603 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005604 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005606 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 MaskVals.push_back(EltIdx);
5609 if (EltIdx < 0) {
5610 ++Quad[0];
5611 ++Quad[1];
5612 ++Quad[2];
5613 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005614 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005615 }
5616 ++Quad[EltIdx / 4];
5617 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005618 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005619
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005621 unsigned MaxQuad = 1;
5622 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 if (LoQuad[i] > MaxQuad) {
5624 BestLoQuad = i;
5625 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005626 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005627 }
5628
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005630 MaxQuad = 1;
5631 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005632 if (HiQuad[i] > MaxQuad) {
5633 BestHiQuad = i;
5634 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005635 }
5636 }
5637
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005639 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 // single pshufb instruction is necessary. If There are more than 2 input
5641 // quads, disable the next transformation since it does not help SSSE3.
5642 bool V1Used = InputQuads[0] || InputQuads[1];
5643 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005644 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005646 BestLoQuad = InputQuads[0] ? 0 : 1;
5647 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005648 }
5649 if (InputQuads.count() > 2) {
5650 BestLoQuad = -1;
5651 BestHiQuad = -1;
5652 }
5653 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005654
Nate Begemanb9a47b82009-02-23 08:49:38 +00005655 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5656 // the shuffle mask. If a quad is scored as -1, that means that it contains
5657 // words from all 4 input quadwords.
5658 SDValue NewV;
5659 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005660 int MaskV[] = {
5661 BestLoQuad < 0 ? 0 : BestLoQuad,
5662 BestHiQuad < 0 ? 1 : BestHiQuad
5663 };
Eric Christopherfd179292009-08-27 18:07:15 +00005664 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005665 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5666 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5667 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005668
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5670 // source words for the shuffle, to aid later transformations.
5671 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005672 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005673 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005674 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005675 if (idx != (int)i)
5676 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005678 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 AllWordsInNewV = false;
5680 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005681 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005682
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5684 if (AllWordsInNewV) {
5685 for (int i = 0; i != 8; ++i) {
5686 int idx = MaskVals[i];
5687 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005688 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005689 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 if ((idx != i) && idx < 4)
5691 pshufhw = false;
5692 if ((idx != i) && idx > 3)
5693 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005694 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 V1 = NewV;
5696 V2Used = false;
5697 BestLoQuad = 0;
5698 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005699 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005700
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5702 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005703 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005704 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5705 unsigned TargetMask = 0;
5706 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005707 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005708 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5709 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5710 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005711 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005712 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005713 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005714 }
Eric Christopherfd179292009-08-27 18:07:15 +00005715
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 // If we have SSSE3, and all words of the result are from 1 input vector,
5717 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5718 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005719 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005723 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 // mask, and elements that come from V1 in the V2 mask, so that the two
5725 // results can be OR'd together.
5726 bool TwoInputs = V1Used && V2Used;
5727 for (unsigned i = 0; i != 8; ++i) {
5728 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005729 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5730 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5731 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5732 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005734 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005735 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005736 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005739 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005740
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 // Calculate the shuffle mask for the second input, shuffle it, and
5742 // OR it with the first shuffled input.
5743 pshufbMask.clear();
5744 for (unsigned i = 0; i != 8; ++i) {
5745 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005746 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5747 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5748 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5749 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005751 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005752 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005753 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005754 MVT::v16i8, &pshufbMask[0], 16));
5755 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005756 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 }
5758
5759 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5760 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005761 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005763 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 for (int i = 0; i != 4; ++i) {
5765 int idx = MaskVals[i];
5766 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 InOrder.set(i);
5768 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005769 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 }
5772 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005773 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005774 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005775
Craig Topperdd637ae2012-02-19 05:41:45 +00005776 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5777 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005778 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005779 NewV.getOperand(0),
5780 getShufflePSHUFLWImmediate(SVOp), DAG);
5781 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 }
Eric Christopherfd179292009-08-27 18:07:15 +00005783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5785 // and update MaskVals with the new element order.
5786 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005787 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 for (unsigned i = 4; i != 8; ++i) {
5789 int idx = MaskVals[i];
5790 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 InOrder.set(i);
5792 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005793 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 }
5796 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005799
Craig Topperdd637ae2012-02-19 05:41:45 +00005800 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5801 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005802 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005803 NewV.getOperand(0),
5804 getShufflePSHUFHWImmediate(SVOp), DAG);
5805 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 }
Eric Christopherfd179292009-08-27 18:07:15 +00005807
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 // In case BestHi & BestLo were both -1, which means each quadword has a word
5809 // from each of the four input quadwords, calculate the InOrder bitvector now
5810 // before falling through to the insert/extract cleanup.
5811 if (BestLoQuad == -1 && BestHiQuad == -1) {
5812 NewV = V1;
5813 for (int i = 0; i != 8; ++i)
5814 if (MaskVals[i] < 0 || MaskVals[i] == i)
5815 InOrder.set(i);
5816 }
Eric Christopherfd179292009-08-27 18:07:15 +00005817
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 // The other elements are put in the right place using pextrw and pinsrw.
5819 for (unsigned i = 0; i != 8; ++i) {
5820 if (InOrder[i])
5821 continue;
5822 int EltIdx = MaskVals[i];
5823 if (EltIdx < 0)
5824 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005825 SDValue ExtOp = (EltIdx < 8) ?
5826 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5827 DAG.getIntPtrConstant(EltIdx)) :
5828 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 DAG.getIntPtrConstant(i));
5832 }
5833 return NewV;
5834}
5835
5836// v16i8 shuffles - Prefer shuffles in the following order:
5837// 1. [ssse3] 1 x pshufb
5838// 2. [ssse3] 2 x pshufb + 1 x por
5839// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5840static
Nate Begeman9008ca62009-04-27 18:41:29 +00005841SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005842 SelectionDAG &DAG,
5843 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005844 SDValue V1 = SVOp->getOperand(0);
5845 SDValue V2 = SVOp->getOperand(1);
5846 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005847 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005848
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005850 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005852
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005854 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005856
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005858 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 //
5860 // Otherwise, we have elements from both input vectors, and must zero out
5861 // elements that come from V2 in the first mask, and V1 in the second mask
5862 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 for (unsigned i = 0; i != 16; ++i) {
5864 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005865 if (EltIdx < 0 || EltIdx >= 16)
5866 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005870 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005871 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005872
5873 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5874 // the 2nd operand if it's undefined or zero.
5875 if (V2.getOpcode() == ISD::UNDEF ||
5876 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005877 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005878
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 // Calculate the shuffle mask for the second input, shuffle it, and
5880 // OR it with the first shuffled input.
5881 pshufbMask.clear();
5882 for (unsigned i = 0; i != 16; ++i) {
5883 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005884 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005885 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005888 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 MVT::v16i8, &pshufbMask[0], 16));
5890 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 }
Eric Christopherfd179292009-08-27 18:07:15 +00005892
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 // No SSSE3 - Calculate in place words and then fix all out of place words
5894 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5895 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005896 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5897 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005898 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005899 for (int i = 0; i != 8; ++i) {
5900 int Elt0 = MaskVals[i*2];
5901 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // This word of the result is all undef, skip it.
5904 if (Elt0 < 0 && Elt1 < 0)
5905 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005906
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005908 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005910
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5912 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5913 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005914
5915 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5916 // using a single extract together, load it and store it.
5917 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005919 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005921 DAG.getIntPtrConstant(i));
5922 continue;
5923 }
5924
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005926 // source byte is not also odd, shift the extracted word left 8 bits
5927 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 DAG.getIntPtrConstant(Elt1 / 2));
5931 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005933 DAG.getConstant(8,
5934 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005935 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5937 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 }
5939 // If Elt0 is defined, extract it from the appropriate source. If the
5940 // source byte is not also even, shift the extracted word right 8 bits. If
5941 // Elt1 was also defined, OR the extracted values together before
5942 // inserting them in the result.
5943 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5946 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005948 DAG.getConstant(8,
5949 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005950 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5952 DAG.getConstant(0x00FF, MVT::i16));
5953 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 : InsElt0;
5955 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 DAG.getIntPtrConstant(i));
5958 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005959 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005960}
5961
Elena Demikhovsky41789462012-09-06 12:42:01 +00005962// v32i8 shuffles - Translate to VPSHUFB if possible.
5963static
5964SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00005965 const X86Subtarget *Subtarget,
5966 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00005967 EVT VT = SVOp->getValueType(0);
5968 SDValue V1 = SVOp->getOperand(0);
5969 SDValue V2 = SVOp->getOperand(1);
5970 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005971 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00005972
5973 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005974 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
5975 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00005976
Michael Liao471b9172012-10-03 23:43:52 +00005977 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005978 // (1) one of input vector is undefined or zeroinitializer.
5979 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
5980 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00005981 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005982 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00005983 return SDValue();
5984
Elena Demikhovsky8100d242012-09-10 12:13:11 +00005985 if (V1IsAllZero && !V2IsAllZero) {
5986 CommuteVectorShuffleMask(MaskVals, 32);
5987 V1 = V2;
5988 }
5989 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00005990 for (unsigned i = 0; i != 32; i++) {
5991 int EltIdx = MaskVals[i];
5992 if (EltIdx < 0 || EltIdx >= 32)
5993 EltIdx = 0x80;
5994 else {
5995 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
5996 // Cross lane is not allowed.
5997 return SDValue();
5998 EltIdx &= 0xf;
5999 }
6000 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6001 }
6002 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6003 DAG.getNode(ISD::BUILD_VECTOR, dl,
6004 MVT::v32i8, &pshufbMask[0], 32));
6005}
6006
Evan Cheng7a831ce2007-12-15 03:00:47 +00006007/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006008/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006009/// done when every pair / quad of shuffle mask elements point to elements in
6010/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006011/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006012static
Nate Begeman9008ca62009-04-27 18:41:29 +00006013SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006014 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006015 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006016 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006017 MVT NewVT;
6018 unsigned Scale;
6019 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006020 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006021 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6022 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6023 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6024 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6025 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6026 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006027 }
6028
Nate Begeman9008ca62009-04-27 18:41:29 +00006029 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006030 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006031 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006032 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006033 int EltIdx = SVOp->getMaskElt(i+j);
6034 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006035 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006036 if (StartIdx < 0)
6037 StartIdx = (EltIdx / Scale);
6038 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006039 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006040 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006041 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006042 }
6043
Craig Topper11ac1f82012-05-04 04:08:44 +00006044 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6045 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006046 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006047}
6048
Evan Chengd880b972008-05-09 21:53:03 +00006049/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006050///
Owen Andersone50ed302009-08-10 22:56:29 +00006051static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006052 SDValue SrcOp, SelectionDAG &DAG,
6053 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006055 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006056 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006057 LD = dyn_cast<LoadSDNode>(SrcOp);
6058 if (!LD) {
6059 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6060 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006061 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006062 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006063 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006064 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006065 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006066 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006067 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006068 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006069 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6070 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6071 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006072 SrcOp.getOperand(0)
6073 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006074 }
6075 }
6076 }
6077
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006078 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006079 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006080 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006081 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006082}
6083
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006084/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6085/// which could not be matched by any known target speficic shuffle
6086static SDValue
6087LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006088
6089 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6090 if (NewOp.getNode())
6091 return NewOp;
6092
Craig Topper8f35c132012-01-20 09:29:03 +00006093 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006094
Craig Topper8f35c132012-01-20 09:29:03 +00006095 unsigned NumElems = VT.getVectorNumElements();
6096 unsigned NumLaneElems = NumElems / 2;
6097
Craig Topper8f35c132012-01-20 09:29:03 +00006098 DebugLoc dl = SVOp->getDebugLoc();
6099 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006100 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006101 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006102
Craig Topper9a2b6e12012-04-06 07:45:23 +00006103 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006104 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006105 // Build a shuffle mask for the output, discovering on the fly which
6106 // input vectors to use as shuffle operands (recorded in InputUsed).
6107 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006108 // out with UseBuildVector set.
6109 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006110 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006111 unsigned LaneStart = l * NumLaneElems;
6112 for (unsigned i = 0; i != NumLaneElems; ++i) {
6113 // The mask element. This indexes into the input.
6114 int Idx = SVOp->getMaskElt(i+LaneStart);
6115 if (Idx < 0) {
6116 // the mask element does not index into any input vector.
6117 Mask.push_back(-1);
6118 continue;
6119 }
Craig Topper8f35c132012-01-20 09:29:03 +00006120
Craig Topper9a2b6e12012-04-06 07:45:23 +00006121 // The input vector this mask element indexes into.
6122 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006123
Craig Topper9a2b6e12012-04-06 07:45:23 +00006124 // Turn the index into an offset from the start of the input vector.
6125 Idx -= Input * NumLaneElems;
6126
6127 // Find or create a shuffle vector operand to hold this input.
6128 unsigned OpNo;
6129 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6130 if (InputUsed[OpNo] == Input)
6131 // This input vector is already an operand.
6132 break;
6133 if (InputUsed[OpNo] < 0) {
6134 // Create a new operand for this input vector.
6135 InputUsed[OpNo] = Input;
6136 break;
6137 }
6138 }
6139
6140 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006141 // More than two input vectors used! Give up on trying to create a
6142 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6143 UseBuildVector = true;
6144 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006145 }
6146
6147 // Add the mask index for the new shuffle vector.
6148 Mask.push_back(Idx + OpNo * NumLaneElems);
6149 }
6150
Craig Topper8ae97ba2012-05-21 06:40:16 +00006151 if (UseBuildVector) {
6152 SmallVector<SDValue, 16> SVOps;
6153 for (unsigned i = 0; i != NumLaneElems; ++i) {
6154 // The mask element. This indexes into the input.
6155 int Idx = SVOp->getMaskElt(i+LaneStart);
6156 if (Idx < 0) {
6157 SVOps.push_back(DAG.getUNDEF(EltVT));
6158 continue;
6159 }
6160
6161 // The input vector this mask element indexes into.
6162 int Input = Idx / NumElems;
6163
6164 // Turn the index into an offset from the start of the input vector.
6165 Idx -= Input * NumElems;
6166
6167 // Extract the vector element by hand.
6168 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6169 SVOp->getOperand(Input),
6170 DAG.getIntPtrConstant(Idx)));
6171 }
6172
6173 // Construct the output using a BUILD_VECTOR.
6174 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6175 SVOps.size());
6176 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006177 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006178 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006179 } else {
6180 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006181 (InputUsed[0] % 2) * NumLaneElems,
6182 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006183 // If only one input was used, use an undefined vector for the other.
6184 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6185 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006186 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006187 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006188 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006189 }
6190
6191 Mask.clear();
6192 }
Craig Topper8f35c132012-01-20 09:29:03 +00006193
6194 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006195 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006196}
6197
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006198/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6199/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006200static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006201LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006202 SDValue V1 = SVOp->getOperand(0);
6203 SDValue V2 = SVOp->getOperand(1);
6204 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006205 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006206
Craig Topper7a9a28b2012-08-12 02:23:29 +00006207 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006208
Benjamin Kramer9c683542012-01-30 15:16:21 +00006209 std::pair<int, int> Locs[4];
6210 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006211 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006212
Evan Chengace3c172008-07-22 21:13:36 +00006213 unsigned NumHi = 0;
6214 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006215 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006216 int Idx = PermMask[i];
6217 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006218 Locs[i] = std::make_pair(-1, -1);
6219 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006220 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6221 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006222 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006224 NumLo++;
6225 } else {
6226 Locs[i] = std::make_pair(1, NumHi);
6227 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006228 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006229 NumHi++;
6230 }
6231 }
6232 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006233
Evan Chengace3c172008-07-22 21:13:36 +00006234 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006235 // If no more than two elements come from either vector. This can be
6236 // implemented with two shuffles. First shuffle gather the elements.
6237 // The second shuffle, which takes the first shuffle as both of its
6238 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006239 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006240
Benjamin Kramer9c683542012-01-30 15:16:21 +00006241 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006242
Benjamin Kramer9c683542012-01-30 15:16:21 +00006243 for (unsigned i = 0; i != 4; ++i)
6244 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006245 unsigned Idx = (i < 2) ? 0 : 4;
6246 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006247 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006248 }
Evan Chengace3c172008-07-22 21:13:36 +00006249
Nate Begeman9008ca62009-04-27 18:41:29 +00006250 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006251 }
6252
6253 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006254 // Otherwise, we must have three elements from one vector, call it X, and
6255 // one element from the other, call it Y. First, use a shufps to build an
6256 // intermediate vector with the one element from Y and the element from X
6257 // that will be in the same half in the final destination (the indexes don't
6258 // matter). Then, use a shufps to build the final vector, taking the half
6259 // containing the element from Y from the intermediate, and the other half
6260 // from X.
6261 if (NumHi == 3) {
6262 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006263 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006264 std::swap(V1, V2);
6265 }
6266
6267 // Find the element from V2.
6268 unsigned HiIndex;
6269 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006270 int Val = PermMask[HiIndex];
6271 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006272 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006273 if (Val >= 4)
6274 break;
6275 }
6276
Nate Begeman9008ca62009-04-27 18:41:29 +00006277 Mask1[0] = PermMask[HiIndex];
6278 Mask1[1] = -1;
6279 Mask1[2] = PermMask[HiIndex^1];
6280 Mask1[3] = -1;
6281 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006282
6283 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006284 Mask1[0] = PermMask[0];
6285 Mask1[1] = PermMask[1];
6286 Mask1[2] = HiIndex & 1 ? 6 : 4;
6287 Mask1[3] = HiIndex & 1 ? 4 : 6;
6288 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006289 }
Craig Topper69947b92012-04-23 06:57:04 +00006290
6291 Mask1[0] = HiIndex & 1 ? 2 : 0;
6292 Mask1[1] = HiIndex & 1 ? 0 : 2;
6293 Mask1[2] = PermMask[2];
6294 Mask1[3] = PermMask[3];
6295 if (Mask1[2] >= 0)
6296 Mask1[2] += 4;
6297 if (Mask1[3] >= 0)
6298 Mask1[3] += 4;
6299 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006300 }
6301
6302 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006303 int LoMask[] = { -1, -1, -1, -1 };
6304 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006305
Benjamin Kramer9c683542012-01-30 15:16:21 +00006306 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006307 unsigned MaskIdx = 0;
6308 unsigned LoIdx = 0;
6309 unsigned HiIdx = 2;
6310 for (unsigned i = 0; i != 4; ++i) {
6311 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006312 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006313 MaskIdx = 1;
6314 LoIdx = 0;
6315 HiIdx = 2;
6316 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006317 int Idx = PermMask[i];
6318 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006319 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006320 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006321 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006322 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006323 LoIdx++;
6324 } else {
6325 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006326 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006327 HiIdx++;
6328 }
6329 }
6330
Nate Begeman9008ca62009-04-27 18:41:29 +00006331 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6332 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006333 int MaskOps[] = { -1, -1, -1, -1 };
6334 for (unsigned i = 0; i != 4; ++i)
6335 if (Locs[i].first != -1)
6336 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006337 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006338}
6339
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006340static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006341 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006342 V = V.getOperand(0);
6343 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6344 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006345 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6346 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6347 // BUILD_VECTOR (load), undef
6348 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006349 if (MayFoldLoad(V))
6350 return true;
6351 return false;
6352}
6353
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006354// FIXME: the version above should always be used. Since there's
6355// a bug where several vector shuffles can't be folded because the
6356// DAG is not updated during lowering and a node claims to have two
6357// uses while it only has one, use this version, and let isel match
6358// another instruction if the load really happens to have more than
6359// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006360// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006361static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006362 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006363 V = V.getOperand(0);
6364 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6365 V = V.getOperand(0);
6366 if (ISD::isNormalLoad(V.getNode()))
6367 return true;
6368 return false;
6369}
6370
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006371static
Evan Cheng835580f2010-10-07 20:50:20 +00006372SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6373 EVT VT = Op.getValueType();
6374
6375 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006376 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6377 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006378 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6379 V1, DAG));
6380}
6381
6382static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006383SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006384 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006385 SDValue V1 = Op.getOperand(0);
6386 SDValue V2 = Op.getOperand(1);
6387 EVT VT = Op.getValueType();
6388
6389 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6390
Craig Topper1accb7e2012-01-10 06:54:16 +00006391 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006392 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6393
Evan Cheng0899f5c2011-08-31 02:05:24 +00006394 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6395 return DAG.getNode(ISD::BITCAST, dl, VT,
6396 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6397 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6398 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006399}
6400
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006401static
6402SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6403 SDValue V1 = Op.getOperand(0);
6404 SDValue V2 = Op.getOperand(1);
6405 EVT VT = Op.getValueType();
6406
6407 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6408 "unsupported shuffle type");
6409
6410 if (V2.getOpcode() == ISD::UNDEF)
6411 V2 = V1;
6412
6413 // v4i32 or v4f32
6414 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6415}
6416
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006417static
Craig Topper1accb7e2012-01-10 06:54:16 +00006418SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006419 SDValue V1 = Op.getOperand(0);
6420 SDValue V2 = Op.getOperand(1);
6421 EVT VT = Op.getValueType();
6422 unsigned NumElems = VT.getVectorNumElements();
6423
6424 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6425 // operand of these instructions is only memory, so check if there's a
6426 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6427 // same masks.
6428 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006429
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006430 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006431 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006432 CanFoldLoad = true;
6433
6434 // When V1 is a load, it can be folded later into a store in isel, example:
6435 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6436 // turns into:
6437 // (MOVLPSmr addr:$src1, VR128:$src2)
6438 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006439 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006440 CanFoldLoad = true;
6441
Dan Gohman65fd6562011-11-03 21:49:52 +00006442 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006443 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006444 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006445 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6446
6447 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006448 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006449 if (SVOp->getMaskElt(1) != -1)
6450 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006451 }
6452
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006453 // movl and movlp will both match v2i64, but v2i64 is never matched by
6454 // movl earlier because we make it strict to avoid messing with the movlp load
6455 // folding logic (see the code above getMOVLP call). Match it here then,
6456 // this is horrible, but will stay like this until we move all shuffle
6457 // matching to x86 specific nodes. Note that for the 1st condition all
6458 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006459 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006460 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6461 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006462 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006463 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006464 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006465 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006466
6467 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6468
6469 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006470 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006471 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006472}
6473
Nadav Rotem154819d2012-04-09 07:45:58 +00006474SDValue
6475X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006476 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6477 EVT VT = Op.getValueType();
6478 DebugLoc dl = Op.getDebugLoc();
6479 SDValue V1 = Op.getOperand(0);
6480 SDValue V2 = Op.getOperand(1);
6481
6482 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006483 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006484
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006485 // Handle splat operations
6486 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006487 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006488 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006489
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006490 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006491 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006492 if (Broadcast.getNode())
6493 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006494
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006495 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006496 if ((Size == 128 && NumElem <= 4) ||
6497 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006498 return SDValue();
6499
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006500 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006501 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006502 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006503
6504 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6505 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006506 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6507 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006508 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6509 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006510 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006511 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006512 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006513 // FIXME: Figure out a cleaner way to do this.
6514 // Try to make use of movq to zero out the top part.
6515 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6516 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6517 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006518 EVT NewVT = NewOp.getValueType();
6519 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6520 NewVT, true, false))
6521 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006522 DAG, Subtarget, dl);
6523 }
6524 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6525 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006526 if (NewOp.getNode()) {
6527 EVT NewVT = NewOp.getValueType();
6528 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6529 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6530 DAG, Subtarget, dl);
6531 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006532 }
6533 }
6534 return SDValue();
6535}
6536
Dan Gohman475871a2008-07-27 21:46:04 +00006537SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006538X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006539 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006540 SDValue V1 = Op.getOperand(0);
6541 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006542 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006543 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006544 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006545 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006546 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006547 bool V1IsSplat = false;
6548 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006549 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006550 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006551 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006552 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling67658342012-10-09 07:45:08 +00006553 bool OptForSize = MF.getFunction()->getFnAttributes().
6554 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006555
Craig Topper3426a3e2011-11-14 06:46:21 +00006556 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006557
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006558 if (V1IsUndef && V2IsUndef)
6559 return DAG.getUNDEF(VT);
6560
6561 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006562
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563 // Vector shuffle lowering takes 3 steps:
6564 //
6565 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6566 // narrowing and commutation of operands should be handled.
6567 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6568 // shuffle nodes.
6569 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6570 // so the shuffle can be broken into other shuffles and the legalizer can
6571 // try the lowering again.
6572 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006573 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006574 // be matched during isel, all of them must be converted to a target specific
6575 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006576
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006577 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6578 // narrowing and commutation of operands should be handled. The actual code
6579 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006580 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006581 if (NewOp.getNode())
6582 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006583
Craig Topper5aaffa82012-02-19 02:53:47 +00006584 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6585
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006586 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6587 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006588 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006589 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006590 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006591 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006592
Craig Topperdd637ae2012-02-19 05:41:45 +00006593 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006594 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006595 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006596
Craig Topperdd637ae2012-02-19 05:41:45 +00006597 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006598 return getMOVHighToLow(Op, dl, DAG);
6599
6600 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006601 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006602 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006603 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006604
Craig Topper5aaffa82012-02-19 02:53:47 +00006605 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006606 // The actual implementation will match the mask in the if above and then
6607 // during isel it can match several different instructions, not only pshufd
6608 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006609 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6610 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006611
Craig Topper5aaffa82012-02-19 02:53:47 +00006612 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006613
Craig Topperdbd98a42012-02-07 06:28:42 +00006614 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6615 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6616
Craig Topper1accb7e2012-01-10 06:54:16 +00006617 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006618 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6619
Craig Topperb3982da2011-12-31 23:50:21 +00006620 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006621 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006622 }
Eric Christopherfd179292009-08-27 18:07:15 +00006623
Evan Chengf26ffe92008-05-29 08:22:04 +00006624 // Check if this can be converted into a logical shift.
6625 bool isLeft = false;
6626 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006627 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006628 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006629 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006630 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006631 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006632 EVT EltVT = VT.getVectorElementType();
6633 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006634 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006635 }
Eric Christopherfd179292009-08-27 18:07:15 +00006636
Craig Topper5aaffa82012-02-19 02:53:47 +00006637 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006638 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006639 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006640 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006641 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006642 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6643
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006644 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006645 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6646 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006647 }
Eric Christopherfd179292009-08-27 18:07:15 +00006648
Nate Begeman9008ca62009-04-27 18:41:29 +00006649 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006650 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006651 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006652
Craig Topperdd637ae2012-02-19 05:41:45 +00006653 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006654 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006655
Craig Topperdd637ae2012-02-19 05:41:45 +00006656 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006657 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006658
Craig Topperdd637ae2012-02-19 05:41:45 +00006659 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006660 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006661
Craig Topperdd637ae2012-02-19 05:41:45 +00006662 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006663 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006664
Craig Topperdd637ae2012-02-19 05:41:45 +00006665 if (ShouldXformToMOVHLPS(M, VT) ||
6666 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006667 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668
Evan Chengf26ffe92008-05-29 08:22:04 +00006669 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006670 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006671 EVT EltVT = VT.getVectorElementType();
6672 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006673 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006674 }
Eric Christopherfd179292009-08-27 18:07:15 +00006675
Evan Cheng9eca5e82006-10-25 21:49:50 +00006676 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006677 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6678 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006679 V1IsSplat = isSplatVector(V1.getNode());
6680 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006681
Chris Lattner8a594482007-11-25 00:24:49 +00006682 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006683 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6684 CommuteVectorShuffleMask(M, NumElems);
6685 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006686 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006687 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006688 }
6689
Craig Topperbeabc6c2011-12-05 06:56:46 +00006690 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006691 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006692 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006693 return V1;
6694 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6695 // the instruction selector will not match, so get a canonical MOVL with
6696 // swapped operands to undo the commute.
6697 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006698 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006699
Craig Topperbeabc6c2011-12-05 06:56:46 +00006700 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006701 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006702
Craig Topperbeabc6c2011-12-05 06:56:46 +00006703 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006704 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006705
Evan Cheng9bbbb982006-10-25 20:48:19 +00006706 if (V2IsSplat) {
6707 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006708 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006709 // new vector_shuffle with the corrected mask.p
6710 SmallVector<int, 8> NewMask(M.begin(), M.end());
6711 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006712 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006713 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006714 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006715 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716 }
6717
Evan Cheng9eca5e82006-10-25 21:49:50 +00006718 if (Commuted) {
6719 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006720 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006721 CommuteVectorShuffleMask(M, NumElems);
6722 std::swap(V1, V2);
6723 std::swap(V1IsSplat, V2IsSplat);
6724 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006725
Craig Topper39a9e482012-02-11 06:24:48 +00006726 if (isUNPCKLMask(M, VT, HasAVX2))
6727 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006728
Craig Topper39a9e482012-02-11 06:24:48 +00006729 if (isUNPCKHMask(M, VT, HasAVX2))
6730 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006731 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732
Nate Begeman9008ca62009-04-27 18:41:29 +00006733 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006734 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006735 return CommuteVectorShuffle(SVOp, DAG);
6736
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006737 // The checks below are all present in isShuffleMaskLegal, but they are
6738 // inlined here right now to enable us to directly emit target specific
6739 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006740
Craig Topper0e2037b2012-01-20 05:53:00 +00006741 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006742 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006743 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006744 DAG);
6745
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006746 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6747 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006748 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006749 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006750 }
6751
Craig Toppera9a568a2012-05-02 08:03:44 +00006752 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006753 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006754 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006755 DAG);
6756
Craig Toppera9a568a2012-05-02 08:03:44 +00006757 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006758 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006759 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006760 DAG);
6761
Craig Topper1a7700a2012-01-19 08:19:12 +00006762 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006763 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006764 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006765
Craig Topper94438ba2011-12-16 08:06:31 +00006766 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006767 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006768 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006769 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006770
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006771 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006772 // Generate target specific nodes for 128 or 256-bit shuffles only
6773 // supported in the AVX instruction set.
6774 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006775
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006776 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006777 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006778 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6779
Craig Topper70b883b2011-11-28 10:14:51 +00006780 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006781 if (isVPERMILPMask(M, VT, HasAVX)) {
6782 if (HasAVX2 && VT == MVT::v8i32)
6783 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006784 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006785 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006786 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006787 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006788
Craig Topper70b883b2011-11-28 10:14:51 +00006789 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006790 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006791 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006792 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006793
Craig Topper1842ba02012-04-23 06:38:28 +00006794 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006795 if (BlendOp.getNode())
6796 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006797
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006798 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006799 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006800 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006801 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006802 }
Craig Topper92040742012-04-16 06:43:40 +00006803 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6804 &permclMask[0], 8);
6805 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006806 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006807 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006808 }
Craig Topper095c5282012-04-15 23:48:57 +00006809
Craig Topper8325c112012-04-16 00:41:45 +00006810 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6811 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006812 getShuffleCLImmediate(SVOp), DAG);
6813
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006814
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006815 //===--------------------------------------------------------------------===//
6816 // Since no target specific shuffle was selected for this generic one,
6817 // lower it into other known shuffles. FIXME: this isn't true yet, but
6818 // this is the plan.
6819 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006820
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006821 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6822 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006823 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006824 if (NewOp.getNode())
6825 return NewOp;
6826 }
6827
6828 if (VT == MVT::v16i8) {
6829 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6830 if (NewOp.getNode())
6831 return NewOp;
6832 }
6833
Elena Demikhovsky41789462012-09-06 12:42:01 +00006834 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006835 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006836 if (NewOp.getNode())
6837 return NewOp;
6838 }
6839
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006840 // Handle all 128-bit wide vectors with 4 elements, and match them with
6841 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006842 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006843 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6844
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006845 // Handle general 256-bit shuffles
6846 if (VT.is256BitVector())
6847 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006850}
6851
Dan Gohman475871a2008-07-27 21:46:04 +00006852SDValue
6853X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006854 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006856 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006857
Craig Topper7a9a28b2012-08-12 02:23:29 +00006858 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006859 return SDValue();
6860
Duncan Sands83ec4b62008-06-06 12:08:01 +00006861 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006863 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006865 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006866 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006867 }
6868
6869 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006870 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6871 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6872 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6874 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006875 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006877 Op.getOperand(0)),
6878 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006880 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006881 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006882 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006883 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006884 }
6885
6886 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006887 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6888 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006889 // result has a single use which is a store or a bitcast to i32. And in
6890 // the case of a store, it's not worth it if the index is a constant 0,
6891 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006892 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006893 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006894 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006895 if ((User->getOpcode() != ISD::STORE ||
6896 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6897 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006898 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006900 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006902 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006903 Op.getOperand(0)),
6904 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006905 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006906 }
6907
6908 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006909 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006910 if (isa<ConstantSDNode>(Op.getOperand(1)))
6911 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006912 }
Dan Gohman475871a2008-07-27 21:46:04 +00006913 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006914}
6915
6916
Dan Gohman475871a2008-07-27 21:46:04 +00006917SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006918X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6919 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006921 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006922
David Greene74a579d2011-02-10 16:57:36 +00006923 SDValue Vec = Op.getOperand(0);
6924 EVT VecVT = Vec.getValueType();
6925
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006926 // If this is a 256-bit vector result, first extract the 128-bit vector and
6927 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006928 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006929 DebugLoc dl = Op.getNode()->getDebugLoc();
6930 unsigned NumElems = VecVT.getVectorNumElements();
6931 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006932 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6933
6934 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006935 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006936
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006937 if (IdxVal >= NumElems/2)
6938 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006939 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006940 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006941 }
6942
Craig Topper7a9a28b2012-08-12 02:23:29 +00006943 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006944
Craig Topperd0a31172012-01-10 06:37:29 +00006945 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006946 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006947 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006948 return Res;
6949 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006950
Owen Andersone50ed302009-08-10 22:56:29 +00006951 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006952 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006954 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006955 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006956 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006957 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006958 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6959 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006960 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006962 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006964 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006965 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00006966 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006967 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006968 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006969 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006970 }
6971
6972 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006973 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 if (Idx == 0)
6975 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006976
Evan Cheng0db9fe62006-04-25 20:13:52 +00006977 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006978 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006979 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006980 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006981 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006982 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006983 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00006984 }
6985
6986 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006987 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6988 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6989 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006990 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 if (Idx == 0)
6992 return Op;
6993
6994 // UNPCKHPD the element to the lowest double word, then movsd.
6995 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6996 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006997 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006998 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006999 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007000 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007001 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007002 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007003 }
7004
Dan Gohman475871a2008-07-27 21:46:04 +00007005 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007006}
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007009X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7010 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007011 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007012 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007013 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007014
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue N0 = Op.getOperand(0);
7016 SDValue N1 = Op.getOperand(1);
7017 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007018
Craig Topper7a9a28b2012-08-12 02:23:29 +00007019 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007020 return SDValue();
7021
Dan Gohman8a55ce42009-09-23 21:02:20 +00007022 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007023 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007024 unsigned Opc;
7025 if (VT == MVT::v8i16)
7026 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007027 else if (VT == MVT::v16i8)
7028 Opc = X86ISD::PINSRB;
7029 else
7030 Opc = X86ISD::PINSRB;
7031
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7033 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 if (N1.getValueType() != MVT::i32)
7035 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7036 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007037 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007038 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007039 }
7040
7041 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007042 // Bits [7:6] of the constant are the source select. This will always be
7043 // zero here. The DAG Combiner may combine an extract_elt index into these
7044 // bits. For example (insert (extract, 3), 2) could be matched by putting
7045 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007046 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007047 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007048 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007049 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007050 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007051 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007052 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007053 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007054 }
7055
7056 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007057 // PINSR* works with constant index.
7058 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007059 }
Dan Gohman475871a2008-07-27 21:46:04 +00007060 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007061}
7062
Dan Gohman475871a2008-07-27 21:46:04 +00007063SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007064X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007065 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007066 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007067
David Greene6b381262011-02-09 15:32:06 +00007068 DebugLoc dl = Op.getDebugLoc();
7069 SDValue N0 = Op.getOperand(0);
7070 SDValue N1 = Op.getOperand(1);
7071 SDValue N2 = Op.getOperand(2);
7072
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007073 // If this is a 256-bit vector result, first extract the 128-bit vector,
7074 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007075 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007076 if (!isa<ConstantSDNode>(N2))
7077 return SDValue();
7078
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007079 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007080 unsigned NumElems = VT.getVectorNumElements();
7081 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007082 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007083
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007084 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007085 bool Upper = IdxVal >= NumElems/2;
7086 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7087 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007088
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007089 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007090 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007091 }
7092
Craig Topperd0a31172012-01-10 06:37:29 +00007093 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7095
Dan Gohman8a55ce42009-09-23 21:02:20 +00007096 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007097 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007098
Dan Gohman8a55ce42009-09-23 21:02:20 +00007099 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007100 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7101 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 if (N1.getValueType() != MVT::i32)
7103 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7104 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007105 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007106 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007107 }
Dan Gohman475871a2008-07-27 21:46:04 +00007108 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007109}
7110
Craig Topper55b24052012-09-11 06:15:32 +00007111static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007112 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007113 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007114 EVT OpVT = Op.getValueType();
7115
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007116 // If this is a 256-bit vector result, first insert into a 128-bit
7117 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007118 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007119 // Insert into a 128-bit vector.
7120 EVT VT128 = EVT::getVectorVT(*Context,
7121 OpVT.getVectorElementType(),
7122 OpVT.getVectorNumElements() / 2);
7123
7124 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7125
7126 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007127 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007128 }
7129
Craig Topperd77d2fe2012-04-29 20:22:05 +00007130 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007131 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007133
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007135 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007136 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007137 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007138}
7139
David Greene91585092011-01-26 15:38:49 +00007140// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7141// a simple subregister reference or explicit instructions to grab
7142// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007143static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7144 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007145 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007146 DebugLoc dl = Op.getNode()->getDebugLoc();
7147 SDValue Vec = Op.getNode()->getOperand(0);
7148 SDValue Idx = Op.getNode()->getOperand(1);
7149
Craig Topper7a9a28b2012-08-12 02:23:29 +00007150 if (Op.getNode()->getValueType(0).is128BitVector() &&
7151 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007152 isa<ConstantSDNode>(Idx)) {
7153 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7154 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007155 }
David Greene91585092011-01-26 15:38:49 +00007156 }
7157 return SDValue();
7158}
7159
David Greenecfe33c42011-01-26 19:13:22 +00007160// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7161// simple superregister reference or explicit instructions to insert
7162// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007163static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7164 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007165 if (Subtarget->hasAVX()) {
7166 DebugLoc dl = Op.getNode()->getDebugLoc();
7167 SDValue Vec = Op.getNode()->getOperand(0);
7168 SDValue SubVec = Op.getNode()->getOperand(1);
7169 SDValue Idx = Op.getNode()->getOperand(2);
7170
Craig Topper7a9a28b2012-08-12 02:23:29 +00007171 if (Op.getNode()->getValueType(0).is256BitVector() &&
7172 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007173 isa<ConstantSDNode>(Idx)) {
7174 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7175 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007176 }
7177 }
7178 return SDValue();
7179}
7180
Bill Wendling056292f2008-09-16 21:48:12 +00007181// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7182// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7183// one of the above mentioned nodes. It has to be wrapped because otherwise
7184// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7185// be used to form addressing mode. These wrapped nodes will be selected
7186// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007187SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007188X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007189 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007190
Chris Lattner41621a22009-06-26 19:22:52 +00007191 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7192 // global base reg.
7193 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007194 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007195 CodeModel::Model M = getTargetMachine().getCodeModel();
7196
Chris Lattner4f066492009-07-11 20:29:19 +00007197 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007198 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007199 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007200 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007201 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007202 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007203 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007204
Evan Cheng1606e8e2009-03-13 07:51:59 +00007205 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007206 CP->getAlignment(),
7207 CP->getOffset(), OpFlag);
7208 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007209 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007210 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007211 if (OpFlag) {
7212 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007213 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007214 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007215 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007216 }
7217
7218 return Result;
7219}
7220
Dan Gohmand858e902010-04-17 15:26:15 +00007221SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007222 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007223
Chris Lattner18c59872009-06-27 04:16:01 +00007224 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7225 // global base reg.
7226 unsigned char OpFlag = 0;
7227 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007228 CodeModel::Model M = getTargetMachine().getCodeModel();
7229
Chris Lattner4f066492009-07-11 20:29:19 +00007230 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007231 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007232 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007233 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007234 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007235 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007236 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007237
Chris Lattner18c59872009-06-27 04:16:01 +00007238 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7239 OpFlag);
7240 DebugLoc DL = JT->getDebugLoc();
7241 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007242
Chris Lattner18c59872009-06-27 04:16:01 +00007243 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007244 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007245 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7246 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007247 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007248 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007249
Chris Lattner18c59872009-06-27 04:16:01 +00007250 return Result;
7251}
7252
7253SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007254X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007255 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007256
Chris Lattner18c59872009-06-27 04:16:01 +00007257 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7258 // global base reg.
7259 unsigned char OpFlag = 0;
7260 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007261 CodeModel::Model M = getTargetMachine().getCodeModel();
7262
Chris Lattner4f066492009-07-11 20:29:19 +00007263 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007264 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7265 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7266 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007267 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007268 } else if (Subtarget->isPICStyleGOT()) {
7269 OpFlag = X86II::MO_GOT;
7270 } else if (Subtarget->isPICStyleStubPIC()) {
7271 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7272 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7273 OpFlag = X86II::MO_DARWIN_NONLAZY;
7274 }
Eric Christopherfd179292009-08-27 18:07:15 +00007275
Chris Lattner18c59872009-06-27 04:16:01 +00007276 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007277
Chris Lattner18c59872009-06-27 04:16:01 +00007278 DebugLoc DL = Op.getDebugLoc();
7279 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007280
7281
Chris Lattner18c59872009-06-27 04:16:01 +00007282 // With PIC, the address is actually $g + Offset.
7283 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007284 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007285 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7286 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007287 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007288 Result);
7289 }
Eric Christopherfd179292009-08-27 18:07:15 +00007290
Eli Friedman586272d2011-08-11 01:48:05 +00007291 // For symbols that require a load from a stub to get the address, emit the
7292 // load.
7293 if (isGlobalStubReference(OpFlag))
7294 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007295 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007296
Chris Lattner18c59872009-06-27 04:16:01 +00007297 return Result;
7298}
7299
Dan Gohman475871a2008-07-27 21:46:04 +00007300SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007301X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007302 // Create the TargetBlockAddressAddress node.
7303 unsigned char OpFlags =
7304 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007305 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007306 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007307 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007308 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007309 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7310 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007311
Dan Gohmanf705adb2009-10-30 01:28:02 +00007312 if (Subtarget->isPICStyleRIPRel() &&
7313 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007314 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7315 else
7316 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007317
Dan Gohman29cbade2009-11-20 23:18:13 +00007318 // With PIC, the address is actually $g + Offset.
7319 if (isGlobalRelativeToPICBase(OpFlags)) {
7320 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7321 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7322 Result);
7323 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007324
7325 return Result;
7326}
7327
7328SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007329X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007330 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007331 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007332 // Create the TargetGlobalAddress node, folding in the constant
7333 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007334 unsigned char OpFlags =
7335 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007336 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007337 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007338 if (OpFlags == X86II::MO_NO_FLAG &&
7339 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007340 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007341 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007342 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007343 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007344 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007345 }
Eric Christopherfd179292009-08-27 18:07:15 +00007346
Chris Lattner4f066492009-07-11 20:29:19 +00007347 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007348 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007349 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7350 else
7351 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007352
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007353 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007354 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007355 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7356 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007357 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007358 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007359
Chris Lattner36c25012009-07-10 07:34:39 +00007360 // For globals that require a load from a stub to get the address, emit the
7361 // load.
7362 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007363 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007364 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007365
Dan Gohman6520e202008-10-18 02:06:02 +00007366 // If there was a non-zero offset that we didn't fold, create an explicit
7367 // addition for it.
7368 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007369 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007370 DAG.getConstant(Offset, getPointerTy()));
7371
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 return Result;
7373}
7374
Evan Chengda43bcf2008-09-24 00:05:32 +00007375SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007376X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007377 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007378 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007379 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007380}
7381
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007382static SDValue
7383GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007384 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007385 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007386 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007387 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007388 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007389 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007390 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007391 GA->getOffset(),
7392 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007393
7394 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7395 : X86ISD::TLSADDR;
7396
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007397 if (InFlag) {
7398 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007399 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007400 } else {
7401 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007402 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007403 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007404
7405 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007406 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007407
Rafael Espindola15f1b662009-04-24 12:59:40 +00007408 SDValue Flag = Chain.getValue(1);
7409 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007410}
7411
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007412// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007413static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007414LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007415 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007416 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007417 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7418 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007419 DAG.getNode(X86ISD::GlobalBaseReg,
7420 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007421 InFlag = Chain.getValue(1);
7422
Chris Lattnerb903bed2009-06-26 21:20:29 +00007423 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007424}
7425
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007426// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007427static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007428LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007429 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007430 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7431 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007432}
7433
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007434static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7435 SelectionDAG &DAG,
7436 const EVT PtrVT,
7437 bool is64Bit) {
7438 DebugLoc dl = GA->getDebugLoc();
7439
7440 // Get the start address of the TLS block for this module.
7441 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7442 .getInfo<X86MachineFunctionInfo>();
7443 MFI->incNumLocalDynamicTLSAccesses();
7444
7445 SDValue Base;
7446 if (is64Bit) {
7447 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7448 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7449 } else {
7450 SDValue InFlag;
7451 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7452 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7453 InFlag = Chain.getValue(1);
7454 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7455 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7456 }
7457
7458 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7459 // of Base.
7460
7461 // Build x@dtpoff.
7462 unsigned char OperandFlags = X86II::MO_DTPOFF;
7463 unsigned WrapperKind = X86ISD::Wrapper;
7464 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7465 GA->getValueType(0),
7466 GA->getOffset(), OperandFlags);
7467 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7468
7469 // Add x@dtpoff with the base.
7470 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7471}
7472
Hans Wennborg228756c2012-05-11 10:11:01 +00007473// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007474static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007475 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007476 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007477 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007478
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007479 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7480 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7481 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007482
Michael J. Spencerec38de22010-10-10 22:04:20 +00007483 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007484 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007485 MachinePointerInfo(Ptr),
7486 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007487
Chris Lattnerb903bed2009-06-26 21:20:29 +00007488 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007489 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7490 // initialexec.
7491 unsigned WrapperKind = X86ISD::Wrapper;
7492 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007493 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007494 } else if (model == TLSModel::InitialExec) {
7495 if (is64Bit) {
7496 OperandFlags = X86II::MO_GOTTPOFF;
7497 WrapperKind = X86ISD::WrapperRIP;
7498 } else {
7499 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7500 }
Chris Lattner18c59872009-06-27 04:16:01 +00007501 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007502 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007503 }
Eric Christopherfd179292009-08-27 18:07:15 +00007504
Hans Wennborg228756c2012-05-11 10:11:01 +00007505 // emit "addl x@ntpoff,%eax" (local exec)
7506 // or "addl x@indntpoff,%eax" (initial exec)
7507 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007509 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007510 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007511 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007512
Hans Wennborg228756c2012-05-11 10:11:01 +00007513 if (model == TLSModel::InitialExec) {
7514 if (isPIC && !is64Bit) {
7515 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7516 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7517 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007518 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007519
7520 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7521 MachinePointerInfo::getGOT(), false, false, false,
7522 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007523 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007524
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007525 // The address of the thread local variable is the add of the thread
7526 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007527 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007528}
7529
Dan Gohman475871a2008-07-27 21:46:04 +00007530SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007531X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007532
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007533 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007534 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007535
Eric Christopher30ef0e52010-06-03 04:07:48 +00007536 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007537 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007538
Eric Christopher30ef0e52010-06-03 04:07:48 +00007539 switch (model) {
7540 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007541 if (Subtarget->is64Bit())
7542 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7543 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007544 case TLSModel::LocalDynamic:
7545 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7546 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007547 case TLSModel::InitialExec:
7548 case TLSModel::LocalExec:
7549 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007550 Subtarget->is64Bit(),
7551 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007552 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007553 llvm_unreachable("Unknown TLS model.");
7554 }
7555
7556 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007557 // Darwin only has one model of TLS. Lower to that.
7558 unsigned char OpFlag = 0;
7559 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7560 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561
Eric Christopher30ef0e52010-06-03 04:07:48 +00007562 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7563 // global base reg.
7564 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7565 !Subtarget->is64Bit();
7566 if (PIC32)
7567 OpFlag = X86II::MO_TLVP_PIC_BASE;
7568 else
7569 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007570 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007571 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007572 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007573 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007574 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007575
Eric Christopher30ef0e52010-06-03 04:07:48 +00007576 // With PIC32, the address is actually $g + Offset.
7577 if (PIC32)
7578 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7579 DAG.getNode(X86ISD::GlobalBaseReg,
7580 DebugLoc(), getPointerTy()),
7581 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582
Eric Christopher30ef0e52010-06-03 04:07:48 +00007583 // Lowering the machine isd will make sure everything is in the right
7584 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007585 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007586 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007587 SDValue Args[] = { Chain, Offset };
7588 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007589
Eric Christopher30ef0e52010-06-03 04:07:48 +00007590 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7591 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7592 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007593
Eric Christopher30ef0e52010-06-03 04:07:48 +00007594 // And our return value (tls address) is in the standard call return value
7595 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007596 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007597 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7598 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007599 }
7600
7601 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007602 // Just use the implicit TLS architecture
7603 // Need to generate someting similar to:
7604 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7605 // ; from TEB
7606 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7607 // mov rcx, qword [rdx+rcx*8]
7608 // mov eax, .tls$:tlsvar
7609 // [rax+rcx] contains the address
7610 // Windows 64bit: gs:0x58
7611 // Windows 32bit: fs:__tls_array
7612
7613 // If GV is an alias then use the aliasee for determining
7614 // thread-localness.
7615 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7616 GV = GA->resolveAliasedGlobal(false);
7617 DebugLoc dl = GA->getDebugLoc();
7618 SDValue Chain = DAG.getEntryNode();
7619
7620 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7621 // %gs:0x58 (64-bit).
7622 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7623 ? Type::getInt8PtrTy(*DAG.getContext(),
7624 256)
7625 : Type::getInt32PtrTy(*DAG.getContext(),
7626 257));
7627
7628 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7629 Subtarget->is64Bit()
7630 ? DAG.getIntPtrConstant(0x58)
7631 : DAG.getExternalSymbol("_tls_array",
7632 getPointerTy()),
7633 MachinePointerInfo(Ptr),
7634 false, false, false, 0);
7635
7636 // Load the _tls_index variable
7637 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7638 if (Subtarget->is64Bit())
7639 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7640 IDX, MachinePointerInfo(), MVT::i32,
7641 false, false, 0);
7642 else
7643 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7644 false, false, false, 0);
7645
Micah Villmowfb384d62012-10-11 21:27:41 +00007646 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007647 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007648 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7649
7650 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7651 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7652 false, false, false, 0);
7653
7654 // Get the offset of start of .tls section
7655 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7656 GA->getValueType(0),
7657 GA->getOffset(), X86II::MO_SECREL);
7658 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7659
7660 // The address of the thread local variable is the add of the thread
7661 // pointer with the offset of the variable.
7662 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007663 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007664
David Blaikie4d6ccb52012-01-20 21:51:11 +00007665 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007666}
7667
Evan Cheng0db9fe62006-04-25 20:13:52 +00007668
Chad Rosierb90d2a92012-01-03 23:19:12 +00007669/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7670/// and take a 2 x i32 value to shift plus a shift amount.
7671SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007672 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007673 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007674 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007675 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007676 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007677 SDValue ShOpLo = Op.getOperand(0);
7678 SDValue ShOpHi = Op.getOperand(1);
7679 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007680 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007681 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007682 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007683
Dan Gohman475871a2008-07-27 21:46:04 +00007684 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007685 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007686 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7687 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007688 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007689 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7690 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007691 }
Evan Chenge3413162006-01-09 18:33:28 +00007692
Owen Anderson825b72b2009-08-11 20:47:22 +00007693 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7694 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007695 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007696 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007697
Dan Gohman475871a2008-07-27 21:46:04 +00007698 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007700 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7701 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007702
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007703 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007704 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7705 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007706 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007707 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7708 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007709 }
7710
Dan Gohman475871a2008-07-27 21:46:04 +00007711 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007712 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007713}
Evan Chenga3195e82006-01-12 22:54:21 +00007714
Dan Gohmand858e902010-04-17 15:26:15 +00007715SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7716 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007717 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007718
Dale Johannesen0488fb62010-09-30 23:57:10 +00007719 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007720 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007721
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007723 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007724
Eli Friedman36df4992009-05-27 00:47:34 +00007725 // These are really Legal; return the operand so the caller accepts it as
7726 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007728 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007730 Subtarget->is64Bit()) {
7731 return Op;
7732 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007733
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007734 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007735 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007736 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007737 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007738 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007739 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007740 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007741 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007742 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007743 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7744}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007745
Owen Andersone50ed302009-08-10 22:56:29 +00007746SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007747 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007748 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007749 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007750 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007751 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007752 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007753 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007754 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007755 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007757
Chris Lattner492a43e2010-09-22 01:28:21 +00007758 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007759
Stuart Hastings84be9582011-06-02 15:57:11 +00007760 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7761 MachineMemOperand *MMO;
7762 if (FI) {
7763 int SSFI = FI->getIndex();
7764 MMO =
7765 DAG.getMachineFunction()
7766 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7767 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7768 } else {
7769 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7770 StackSlot = StackSlot.getOperand(1);
7771 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007772 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007773 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7774 X86ISD::FILD, DL,
7775 Tys, Ops, array_lengthof(Ops),
7776 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007778 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007780 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007781
7782 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7783 // shouldn't be necessary except that RFP cannot be live across
7784 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007785 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007786 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7787 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007788 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007789 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007790 SDValue Ops[] = {
7791 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7792 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007793 MachineMemOperand *MMO =
7794 DAG.getMachineFunction()
7795 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007796 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797
Chris Lattner492a43e2010-09-22 01:28:21 +00007798 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7799 Ops, array_lengthof(Ops),
7800 Op.getValueType(), MMO);
7801 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007802 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007803 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007804 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007805
Evan Cheng0db9fe62006-04-25 20:13:52 +00007806 return Result;
7807}
7808
Bill Wendling8b8a6362009-01-17 03:56:04 +00007809// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007810SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7811 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007812 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007813 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007814 movq %rax, %xmm0
7815 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7816 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7817 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007818 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007819 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007820 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007821 addpd %xmm1, %xmm0
7822 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007823 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007824
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007825 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007826 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007827
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007828 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007829 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7830 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007831 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007832
Chris Lattner97484792012-01-25 09:56:22 +00007833 SmallVector<Constant*,2> CV1;
7834 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007835 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007836 CV1.push_back(
7837 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7838 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007839 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007840
Bill Wendling397ae212012-01-05 02:13:20 +00007841 // Load the 64-bit value into an XMM register.
7842 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7843 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007845 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007846 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007847 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7848 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7849 CLod0);
7850
Owen Anderson825b72b2009-08-11 20:47:22 +00007851 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007852 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007853 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007854 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007856 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007857
Craig Topperd0a31172012-01-10 06:37:29 +00007858 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007859 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7860 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7861 } else {
7862 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7863 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7864 S2F, 0x4E, DAG);
7865 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7866 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7867 Sub);
7868 }
7869
7870 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007871 DAG.getIntPtrConstant(0));
7872}
7873
Bill Wendling8b8a6362009-01-17 03:56:04 +00007874// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007875SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7876 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007877 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007878 // FP constant to bias correct the final result.
7879 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007881
7882 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007883 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007884 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007885
Eli Friedmanf3704762011-08-29 21:15:46 +00007886 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007887 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007888
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007890 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007891 DAG.getIntPtrConstant(0));
7892
7893 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007894 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007895 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007896 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007897 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007898 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007899 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 MVT::v2f64, Bias)));
7901 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007902 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007903 DAG.getIntPtrConstant(0));
7904
7905 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007906 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007907
7908 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007909 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007910
Craig Topper69947b92012-04-23 06:57:04 +00007911 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007912 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007913 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007914 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007915 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007916
7917 // Handle final rounding.
7918 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007919}
7920
Dan Gohmand858e902010-04-17 15:26:15 +00007921SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7922 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007923 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007924 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007925
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007926 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007927 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7928 // the optimization here.
7929 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007930 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007931
Owen Andersone50ed302009-08-10 22:56:29 +00007932 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007933 EVT DstVT = Op.getValueType();
7934 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007935 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007936 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007937 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007938 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007939 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007940
7941 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007942 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007943 if (SrcVT == MVT::i32) {
7944 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7945 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7946 getPointerTy(), StackSlot, WordOff);
7947 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007948 StackSlot, MachinePointerInfo(),
7949 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007950 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007951 OffsetSlot, MachinePointerInfo(),
7952 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007953 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7954 return Fild;
7955 }
7956
7957 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7958 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007959 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007960 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007961 // For i64 source, we need to add the appropriate power of 2 if the input
7962 // was negative. This is the same as the optimization in
7963 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7964 // we must be careful to do the computation in x87 extended precision, not
7965 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007966 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7967 MachineMemOperand *MMO =
7968 DAG.getMachineFunction()
7969 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7970 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007971
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007972 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7973 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007974 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7975 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007976
7977 APInt FF(32, 0x5F800000ULL);
7978
7979 // Check whether the sign bit is set.
7980 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7981 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7982 ISD::SETLT);
7983
7984 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7985 SDValue FudgePtr = DAG.getConstantPool(
7986 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7987 getPointerTy());
7988
7989 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7990 SDValue Zero = DAG.getIntPtrConstant(0);
7991 SDValue Four = DAG.getIntPtrConstant(4);
7992 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7993 Zero, Four);
7994 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7995
7996 // Load the value out, extending it from f32 to f80.
7997 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007998 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007999 FudgePtr, MachinePointerInfo::getConstantPool(),
8000 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008001 // Extend everything to 80 bits to force it to be done on x87.
8002 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8003 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008004}
8005
Dan Gohman475871a2008-07-27 21:46:04 +00008006std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008007FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008008 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008009
Owen Andersone50ed302009-08-10 22:56:29 +00008010 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008011
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008012 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008013 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8014 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008015 }
8016
Owen Anderson825b72b2009-08-11 20:47:22 +00008017 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8018 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008019 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008021 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008022 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008023 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008024 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008025 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008027 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008028 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008029
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008030 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8031 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008032 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008033 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008034 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008035 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008036
Evan Cheng0db9fe62006-04-25 20:13:52 +00008037 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008038 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8039 Opc = X86ISD::WIN_FTOL;
8040 else
8041 switch (DstTy.getSimpleVT().SimpleTy) {
8042 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8043 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8044 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8045 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8046 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008047
Dan Gohman475871a2008-07-27 21:46:04 +00008048 SDValue Chain = DAG.getEntryNode();
8049 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008050 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008051 // FIXME This causes a redundant load/store if the SSE-class value is already
8052 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008053 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008054 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008055 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008056 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008057 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008059 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008060 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008061 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008062
Chris Lattner492a43e2010-09-22 01:28:21 +00008063 MachineMemOperand *MMO =
8064 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8065 MachineMemOperand::MOLoad, MemSize, MemSize);
8066 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8067 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008068 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008069 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008070 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8071 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008072
Chris Lattner07290932010-09-22 01:05:16 +00008073 MachineMemOperand *MMO =
8074 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8075 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008076
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008077 if (Opc != X86ISD::WIN_FTOL) {
8078 // Build the FP_TO_INT*_IN_MEM
8079 SDValue Ops[] = { Chain, Value, StackSlot };
8080 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8081 Ops, 3, DstTy, MMO);
8082 return std::make_pair(FIST, StackSlot);
8083 } else {
8084 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8085 DAG.getVTList(MVT::Other, MVT::Glue),
8086 Chain, Value);
8087 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8088 MVT::i32, ftol.getValue(1));
8089 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8090 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008091 SDValue Ops[] = { eax, edx };
8092 SDValue pair = IsReplace
8093 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8094 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008095 return std::make_pair(pair, SDValue());
8096 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008097}
8098
Dan Gohmand858e902010-04-17 15:26:15 +00008099SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8100 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008101 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008102 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008103
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008104 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8105 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008106 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008107 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8108 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008109
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008110 if (StackSlot.getNode())
8111 // Load the result.
8112 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8113 FIST, StackSlot, MachinePointerInfo(),
8114 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008115
8116 // The node is the result.
8117 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008118}
8119
Dan Gohmand858e902010-04-17 15:26:15 +00008120SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8121 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008122 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8123 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008124 SDValue FIST = Vals.first, StackSlot = Vals.second;
8125 assert(FIST.getNode() && "Unexpected failure");
8126
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008127 if (StackSlot.getNode())
8128 // Load the result.
8129 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8130 FIST, StackSlot, MachinePointerInfo(),
8131 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008132
8133 // The node is the result.
8134 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008135}
8136
Michael Liao9d796db2012-10-10 16:32:15 +00008137SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8138 SelectionDAG &DAG) const {
8139 DebugLoc DL = Op.getDebugLoc();
8140 EVT VT = Op.getValueType();
8141 SDValue In = Op.getOperand(0);
8142 EVT SVT = In.getValueType();
8143
8144 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8145
8146 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8147 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8148 In, DAG.getUNDEF(SVT)));
8149}
8150
Craig Topper43620672012-09-08 07:31:51 +00008151SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008152 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008153 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008154 EVT VT = Op.getValueType();
8155 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008156 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8157 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008158 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008159 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008160 }
Craig Topper43620672012-09-08 07:31:51 +00008161 Constant *C;
8162 if (EltVT == MVT::f64)
8163 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8164 else
8165 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8166 C = ConstantVector::getSplat(NumElts, C);
8167 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8168 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008169 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008170 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008171 false, false, false, Alignment);
8172 if (VT.isVector()) {
8173 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8174 return DAG.getNode(ISD::BITCAST, dl, VT,
8175 DAG.getNode(ISD::AND, dl, ANDVT,
8176 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8177 Op.getOperand(0)),
8178 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8179 }
Dale Johannesenace16102009-02-03 19:33:06 +00008180 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008181}
8182
Dan Gohmand858e902010-04-17 15:26:15 +00008183SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008184 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008185 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008186 EVT VT = Op.getValueType();
8187 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008188 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8189 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008190 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008191 NumElts = VT.getVectorNumElements();
8192 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008193 Constant *C;
8194 if (EltVT == MVT::f64)
8195 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8196 else
8197 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8198 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008199 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8200 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008201 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008202 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008203 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008204 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008205 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008206 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008207 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008208 DAG.getNode(ISD::BITCAST, dl, XORVT,
8209 Op.getOperand(0)),
8210 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008211 }
Craig Topper69947b92012-04-23 06:57:04 +00008212
8213 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008214}
8215
Dan Gohmand858e902010-04-17 15:26:15 +00008216SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008217 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008218 SDValue Op0 = Op.getOperand(0);
8219 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008220 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008221 EVT VT = Op.getValueType();
8222 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008223
8224 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008225 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008226 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008227 SrcVT = VT;
8228 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008229 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008230 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008231 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008232 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008233 }
8234
8235 // At this point the operands and the result should have the same
8236 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008237
Evan Cheng68c47cb2007-01-05 07:55:56 +00008238 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008239 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008240 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008241 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8242 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008243 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008244 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8245 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8246 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8247 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008248 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008249 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008250 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008251 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008252 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008253 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008254 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008255
8256 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008257 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008258 // Op0 is MVT::f32, Op1 is MVT::f64.
8259 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8260 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8261 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008262 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008264 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008265 }
8266
Evan Cheng73d6cf12007-01-05 21:37:56 +00008267 // Clear first operand sign bit.
8268 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008269 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008270 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8271 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008272 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008273 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8274 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8275 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8276 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008277 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008278 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008279 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008280 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008281 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008282 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008283 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008284
8285 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008286 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008287}
8288
Craig Topper55b24052012-09-11 06:15:32 +00008289static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008290 SDValue N0 = Op.getOperand(0);
8291 DebugLoc dl = Op.getDebugLoc();
8292 EVT VT = Op.getValueType();
8293
8294 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8295 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8296 DAG.getConstant(1, VT));
8297 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8298}
8299
Michael Liaof966e4e2012-09-13 20:24:54 +00008300// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8301//
8302SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8303 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8304
8305 if (!Subtarget->hasSSE41())
8306 return SDValue();
8307
8308 if (!Op->hasOneUse())
8309 return SDValue();
8310
8311 SDNode *N = Op.getNode();
8312 DebugLoc DL = N->getDebugLoc();
8313
8314 SmallVector<SDValue, 8> Opnds;
8315 DenseMap<SDValue, unsigned> VecInMap;
8316 EVT VT = MVT::Other;
8317
8318 // Recognize a special case where a vector is casted into wide integer to
8319 // test all 0s.
8320 Opnds.push_back(N->getOperand(0));
8321 Opnds.push_back(N->getOperand(1));
8322
8323 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8324 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8325 // BFS traverse all OR'd operands.
8326 if (I->getOpcode() == ISD::OR) {
8327 Opnds.push_back(I->getOperand(0));
8328 Opnds.push_back(I->getOperand(1));
8329 // Re-evaluate the number of nodes to be traversed.
8330 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8331 continue;
8332 }
8333
8334 // Quit if a non-EXTRACT_VECTOR_ELT
8335 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8336 return SDValue();
8337
8338 // Quit if without a constant index.
8339 SDValue Idx = I->getOperand(1);
8340 if (!isa<ConstantSDNode>(Idx))
8341 return SDValue();
8342
8343 SDValue ExtractedFromVec = I->getOperand(0);
8344 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8345 if (M == VecInMap.end()) {
8346 VT = ExtractedFromVec.getValueType();
8347 // Quit if not 128/256-bit vector.
8348 if (!VT.is128BitVector() && !VT.is256BitVector())
8349 return SDValue();
8350 // Quit if not the same type.
8351 if (VecInMap.begin() != VecInMap.end() &&
8352 VT != VecInMap.begin()->first.getValueType())
8353 return SDValue();
8354 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8355 }
8356 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8357 }
8358
8359 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008360 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008361
8362 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8363 SmallVector<SDValue, 8> VecIns;
8364
8365 for (DenseMap<SDValue, unsigned>::const_iterator
8366 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8367 // Quit if not all elements are used.
8368 if (I->second != FullMask)
8369 return SDValue();
8370 VecIns.push_back(I->first);
8371 }
8372
8373 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8374
8375 // Cast all vectors into TestVT for PTEST.
8376 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8377 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8378
8379 // If more than one full vectors are evaluated, OR them first before PTEST.
8380 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8381 // Each iteration will OR 2 nodes and append the result until there is only
8382 // 1 node left, i.e. the final OR'd value of all vectors.
8383 SDValue LHS = VecIns[Slot];
8384 SDValue RHS = VecIns[Slot + 1];
8385 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8386 }
8387
8388 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8389 VecIns.back(), VecIns.back());
8390}
8391
Dan Gohman076aee32009-03-04 19:44:21 +00008392/// Emit nodes that will be selected as "test Op0,Op0", or something
8393/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008394SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008395 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008396 DebugLoc dl = Op.getDebugLoc();
8397
Dan Gohman31125812009-03-07 01:58:32 +00008398 // CF and OF aren't always set the way we want. Determine which
8399 // of these we need.
8400 bool NeedCF = false;
8401 bool NeedOF = false;
8402 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008403 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008404 case X86::COND_A: case X86::COND_AE:
8405 case X86::COND_B: case X86::COND_BE:
8406 NeedCF = true;
8407 break;
8408 case X86::COND_G: case X86::COND_GE:
8409 case X86::COND_L: case X86::COND_LE:
8410 case X86::COND_O: case X86::COND_NO:
8411 NeedOF = true;
8412 break;
Dan Gohman31125812009-03-07 01:58:32 +00008413 }
8414
Dan Gohman076aee32009-03-04 19:44:21 +00008415 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008416 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8417 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008418 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8419 // Emit a CMP with 0, which is the TEST pattern.
8420 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8421 DAG.getConstant(0, Op.getValueType()));
8422
8423 unsigned Opcode = 0;
8424 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008425
8426 // Truncate operations may prevent the merge of the SETCC instruction
8427 // and the arithmetic intruction before it. Attempt to truncate the operands
8428 // of the arithmetic instruction and use a reduced bit-width instruction.
8429 bool NeedTruncation = false;
8430 SDValue ArithOp = Op;
8431 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8432 SDValue Arith = Op->getOperand(0);
8433 // Both the trunc and the arithmetic op need to have one user each.
8434 if (Arith->hasOneUse())
8435 switch (Arith.getOpcode()) {
8436 default: break;
8437 case ISD::ADD:
8438 case ISD::SUB:
8439 case ISD::AND:
8440 case ISD::OR:
8441 case ISD::XOR: {
8442 NeedTruncation = true;
8443 ArithOp = Arith;
8444 }
8445 }
8446 }
8447
8448 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8449 // which may be the result of a CAST. We use the variable 'Op', which is the
8450 // non-casted variable when we check for possible users.
8451 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008452 case ISD::ADD:
8453 // Due to an isel shortcoming, be conservative if this add is likely to be
8454 // selected as part of a load-modify-store instruction. When the root node
8455 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8456 // uses of other nodes in the match, such as the ADD in this case. This
8457 // leads to the ADD being left around and reselected, with the result being
8458 // two adds in the output. Alas, even if none our users are stores, that
8459 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8460 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8461 // climbing the DAG back to the root, and it doesn't seem to be worth the
8462 // effort.
8463 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008464 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8465 if (UI->getOpcode() != ISD::CopyToReg &&
8466 UI->getOpcode() != ISD::SETCC &&
8467 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008468 goto default_case;
8469
8470 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008471 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008472 // An add of one will be selected as an INC.
8473 if (C->getAPIntValue() == 1) {
8474 Opcode = X86ISD::INC;
8475 NumOperands = 1;
8476 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008477 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008478
8479 // An add of negative one (subtract of one) will be selected as a DEC.
8480 if (C->getAPIntValue().isAllOnesValue()) {
8481 Opcode = X86ISD::DEC;
8482 NumOperands = 1;
8483 break;
8484 }
Dan Gohman076aee32009-03-04 19:44:21 +00008485 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008486
8487 // Otherwise use a regular EFLAGS-setting add.
8488 Opcode = X86ISD::ADD;
8489 NumOperands = 2;
8490 break;
8491 case ISD::AND: {
8492 // If the primary and result isn't used, don't bother using X86ISD::AND,
8493 // because a TEST instruction will be better.
8494 bool NonFlagUse = false;
8495 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8496 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8497 SDNode *User = *UI;
8498 unsigned UOpNo = UI.getOperandNo();
8499 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8500 // Look pass truncate.
8501 UOpNo = User->use_begin().getOperandNo();
8502 User = *User->use_begin();
8503 }
8504
8505 if (User->getOpcode() != ISD::BRCOND &&
8506 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008507 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008508 NonFlagUse = true;
8509 break;
8510 }
Dan Gohman076aee32009-03-04 19:44:21 +00008511 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008512
8513 if (!NonFlagUse)
8514 break;
8515 }
8516 // FALL THROUGH
8517 case ISD::SUB:
8518 case ISD::OR:
8519 case ISD::XOR:
8520 // Due to the ISEL shortcoming noted above, be conservative if this op is
8521 // likely to be selected as part of a load-modify-store instruction.
8522 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8523 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8524 if (UI->getOpcode() == ISD::STORE)
8525 goto default_case;
8526
8527 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008528 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008529 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008530 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008531 case ISD::XOR: Opcode = X86ISD::XOR; break;
8532 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008533 case ISD::OR: {
8534 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8535 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8536 if (EFLAGS.getNode())
8537 return EFLAGS;
8538 }
8539 Opcode = X86ISD::OR;
8540 break;
8541 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008542 }
8543
8544 NumOperands = 2;
8545 break;
8546 case X86ISD::ADD:
8547 case X86ISD::SUB:
8548 case X86ISD::INC:
8549 case X86ISD::DEC:
8550 case X86ISD::OR:
8551 case X86ISD::XOR:
8552 case X86ISD::AND:
8553 return SDValue(Op.getNode(), 1);
8554 default:
8555 default_case:
8556 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008557 }
8558
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008559 // If we found that truncation is beneficial, perform the truncation and
8560 // update 'Op'.
8561 if (NeedTruncation) {
8562 EVT VT = Op.getValueType();
8563 SDValue WideVal = Op->getOperand(0);
8564 EVT WideVT = WideVal.getValueType();
8565 unsigned ConvertedOp = 0;
8566 // Use a target machine opcode to prevent further DAGCombine
8567 // optimizations that may separate the arithmetic operations
8568 // from the setcc node.
8569 switch (WideVal.getOpcode()) {
8570 default: break;
8571 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8572 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8573 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8574 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8575 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8576 }
8577
8578 if (ConvertedOp) {
8579 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8580 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8581 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8582 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8583 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8584 }
8585 }
8586 }
8587
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008588 if (Opcode == 0)
8589 // Emit a CMP with 0, which is the TEST pattern.
8590 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8591 DAG.getConstant(0, Op.getValueType()));
8592
8593 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8594 SmallVector<SDValue, 4> Ops;
8595 for (unsigned i = 0; i != NumOperands; ++i)
8596 Ops.push_back(Op.getOperand(i));
8597
8598 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8599 DAG.ReplaceAllUsesWith(Op, New);
8600 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008601}
8602
8603/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8604/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008605SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008606 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008607 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8608 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008609 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008610
8611 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008612 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8613 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8614 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8615 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8616 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8617 Op0, Op1);
8618 return SDValue(Sub.getNode(), 1);
8619 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008620 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008621}
8622
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008623/// Convert a comparison if required by the subtarget.
8624SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8625 SelectionDAG &DAG) const {
8626 // If the subtarget does not support the FUCOMI instruction, floating-point
8627 // comparisons have to be converted.
8628 if (Subtarget->hasCMov() ||
8629 Cmp.getOpcode() != X86ISD::CMP ||
8630 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8631 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8632 return Cmp;
8633
8634 // The instruction selector will select an FUCOM instruction instead of
8635 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8636 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8637 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8638 DebugLoc dl = Cmp.getDebugLoc();
8639 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8640 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8641 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8642 DAG.getConstant(8, MVT::i8));
8643 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8644 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8645}
8646
Evan Chengd40d03e2010-01-06 19:38:29 +00008647/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8648/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008649SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8650 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008651 SDValue Op0 = And.getOperand(0);
8652 SDValue Op1 = And.getOperand(1);
8653 if (Op0.getOpcode() == ISD::TRUNCATE)
8654 Op0 = Op0.getOperand(0);
8655 if (Op1.getOpcode() == ISD::TRUNCATE)
8656 Op1 = Op1.getOperand(0);
8657
Evan Chengd40d03e2010-01-06 19:38:29 +00008658 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008659 if (Op1.getOpcode() == ISD::SHL)
8660 std::swap(Op0, Op1);
8661 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008662 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8663 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008664 // If we looked past a truncate, check that it's only truncating away
8665 // known zeros.
8666 unsigned BitWidth = Op0.getValueSizeInBits();
8667 unsigned AndBitWidth = And.getValueSizeInBits();
8668 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008669 APInt Zeros, Ones;
8670 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008671 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8672 return SDValue();
8673 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008674 LHS = Op1;
8675 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008676 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008677 } else if (Op1.getOpcode() == ISD::Constant) {
8678 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008679 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008680 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008681
8682 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008683 LHS = AndLHS.getOperand(0);
8684 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008685 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008686
8687 // Use BT if the immediate can't be encoded in a TEST instruction.
8688 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8689 LHS = AndLHS;
8690 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8691 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008692 }
Evan Cheng0488db92007-09-25 01:57:46 +00008693
Evan Chengd40d03e2010-01-06 19:38:29 +00008694 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008695 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008696 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008697 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008698 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008699 // Also promote i16 to i32 for performance / code size reason.
8700 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008701 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008702 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008703
Evan Chengd40d03e2010-01-06 19:38:29 +00008704 // If the operand types disagree, extend the shift amount to match. Since
8705 // BT ignores high bits (like shifts) we can use anyextend.
8706 if (LHS.getValueType() != RHS.getValueType())
8707 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008708
Evan Chengd40d03e2010-01-06 19:38:29 +00008709 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8710 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8711 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8712 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008713 }
8714
Evan Cheng54de3ea2010-01-05 06:52:31 +00008715 return SDValue();
8716}
8717
Dan Gohmand858e902010-04-17 15:26:15 +00008718SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008719
8720 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8721
Evan Cheng54de3ea2010-01-05 06:52:31 +00008722 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8723 SDValue Op0 = Op.getOperand(0);
8724 SDValue Op1 = Op.getOperand(1);
8725 DebugLoc dl = Op.getDebugLoc();
8726 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8727
8728 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008729 // Lower (X & (1 << N)) == 0 to BT(X, N).
8730 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8731 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008732 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008733 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008734 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008735 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8736 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8737 if (NewSetCC.getNode())
8738 return NewSetCC;
8739 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008740
Chris Lattner481eebc2010-12-19 21:23:48 +00008741 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8742 // these.
8743 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008744 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008745 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8746 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008747
Chris Lattner481eebc2010-12-19 21:23:48 +00008748 // If the input is a setcc, then reuse the input setcc or use a new one with
8749 // the inverted condition.
8750 if (Op0.getOpcode() == X86ISD::SETCC) {
8751 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8752 bool Invert = (CC == ISD::SETNE) ^
8753 cast<ConstantSDNode>(Op1)->isNullValue();
8754 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008755
Evan Cheng2c755ba2010-02-27 07:36:59 +00008756 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008757 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8758 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8759 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008760 }
8761
Evan Chenge5b51ac2010-04-17 06:13:15 +00008762 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008763 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008764 if (X86CC == X86::COND_INVALID)
8765 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008766
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008767 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008768 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008770 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008771}
8772
Craig Topper89af15e2011-09-18 08:03:58 +00008773// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008774// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008775static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008776 EVT VT = Op.getValueType();
8777
Craig Topper7a9a28b2012-08-12 02:23:29 +00008778 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008779 "Unsupported value type for operation");
8780
Craig Topper66ddd152012-04-27 22:54:43 +00008781 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008782 DebugLoc dl = Op.getDebugLoc();
8783 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008784
8785 // Extract the LHS vectors
8786 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008787 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8788 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008789
8790 // Extract the RHS vectors
8791 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008792 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8793 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008794
8795 // Issue the operation on the smaller types and concatenate the result back
8796 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8797 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8798 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8799 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8800 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8801}
8802
8803
Dan Gohmand858e902010-04-17 15:26:15 +00008804SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008805 SDValue Cond;
8806 SDValue Op0 = Op.getOperand(0);
8807 SDValue Op1 = Op.getOperand(1);
8808 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008809 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008810 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8811 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008812 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008813
8814 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008815#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008816 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008817 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8818#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008819
Craig Topper523908d2012-08-13 02:34:03 +00008820 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008821 bool Swap = false;
8822
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008823 // SSE Condition code mapping:
8824 // 0 - EQ
8825 // 1 - LT
8826 // 2 - LE
8827 // 3 - UNORD
8828 // 4 - NEQ
8829 // 5 - NLT
8830 // 6 - NLE
8831 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008832 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008833 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008834 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008835 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008836 case ISD::SETOGT:
8837 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008838 case ISD::SETLT:
8839 case ISD::SETOLT: SSECC = 1; break;
8840 case ISD::SETOGE:
8841 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008842 case ISD::SETLE:
8843 case ISD::SETOLE: SSECC = 2; break;
8844 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008845 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008846 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008847 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008848 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008849 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008850 case ISD::SETUGT: SSECC = 6; break;
8851 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008852 case ISD::SETUEQ:
8853 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008854 }
8855 if (Swap)
8856 std::swap(Op0, Op1);
8857
Nate Begemanfb8ead02008-07-25 19:05:58 +00008858 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008859 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008860 unsigned CC0, CC1;
8861 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008862 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008863 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8864 } else {
8865 assert(SetCCOpcode == ISD::SETONE);
8866 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008867 }
Craig Topper523908d2012-08-13 02:34:03 +00008868
8869 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8870 DAG.getConstant(CC0, MVT::i8));
8871 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8872 DAG.getConstant(CC1, MVT::i8));
8873 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008874 }
8875 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008876 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8877 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008879
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008880 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008881 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008882 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008883
Nate Begeman30a0de92008-07-17 16:51:19 +00008884 // We are handling one of the integer comparisons here. Since SSE only has
8885 // GT and EQ comparisons for integer, swapping operands and multiple
8886 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008887 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008888 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008889
Nate Begeman30a0de92008-07-17 16:51:19 +00008890 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008891 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008892 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008893 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008894 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008895 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008896 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008897 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008898 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008899 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008900 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008901 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008902 }
8903 if (Swap)
8904 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008905
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008906 // Check that the operation in question is available (most are plain SSE2,
8907 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008908 if (VT == MVT::v2i64) {
8909 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8910 return SDValue();
8911 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8912 return SDValue();
8913 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008914
Nate Begeman30a0de92008-07-17 16:51:19 +00008915 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8916 // bits of the inputs before performing those operations.
8917 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008918 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008919 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8920 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008921 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008922 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8923 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008924 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8925 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008926 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008927
Dale Johannesenace16102009-02-03 19:33:06 +00008928 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008929
8930 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008931 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008932 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008933
Nate Begeman30a0de92008-07-17 16:51:19 +00008934 return Result;
8935}
Evan Cheng0488db92007-09-25 01:57:46 +00008936
Evan Cheng370e5342008-12-03 08:38:43 +00008937// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008938static bool isX86LogicalCmp(SDValue Op) {
8939 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008940 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8941 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008942 return true;
8943 if (Op.getResNo() == 1 &&
8944 (Opc == X86ISD::ADD ||
8945 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008946 Opc == X86ISD::ADC ||
8947 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008948 Opc == X86ISD::SMUL ||
8949 Opc == X86ISD::UMUL ||
8950 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008951 Opc == X86ISD::DEC ||
8952 Opc == X86ISD::OR ||
8953 Opc == X86ISD::XOR ||
8954 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008955 return true;
8956
Chris Lattner9637d5b2010-12-05 07:49:54 +00008957 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8958 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008959
Dan Gohman076aee32009-03-04 19:44:21 +00008960 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008961}
8962
Chris Lattnera2b56002010-12-05 01:23:24 +00008963static bool isZero(SDValue V) {
8964 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8965 return C && C->isNullValue();
8966}
8967
Chris Lattner96908b12010-12-05 02:00:51 +00008968static bool isAllOnes(SDValue V) {
8969 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8970 return C && C->isAllOnesValue();
8971}
8972
Evan Chengb64dd5f2012-08-07 22:21:00 +00008973static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8974 if (V.getOpcode() != ISD::TRUNCATE)
8975 return false;
8976
8977 SDValue VOp0 = V.getOperand(0);
8978 unsigned InBits = VOp0.getValueSizeInBits();
8979 unsigned Bits = V.getValueSizeInBits();
8980 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8981}
8982
Dan Gohmand858e902010-04-17 15:26:15 +00008983SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008984 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008985 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008986 SDValue Op1 = Op.getOperand(1);
8987 SDValue Op2 = Op.getOperand(2);
8988 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008989 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008990
Dan Gohman1a492952009-10-20 16:22:37 +00008991 if (Cond.getOpcode() == ISD::SETCC) {
8992 SDValue NewCond = LowerSETCC(Cond, DAG);
8993 if (NewCond.getNode())
8994 Cond = NewCond;
8995 }
Evan Cheng734503b2006-09-11 02:19:56 +00008996
Chris Lattnera2b56002010-12-05 01:23:24 +00008997 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008998 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008999 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009000 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009001 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009002 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9003 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009004 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009005
Chris Lattnera2b56002010-12-05 01:23:24 +00009006 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009007
9008 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009009 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9010 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009011
9012 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009013 // Apply further optimizations for special cases
9014 // (select (x != 0), -1, 0) -> neg & sbb
9015 // (select (x == 0), 0, -1) -> neg & sbb
9016 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009017 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009018 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9019 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009020 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9021 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009022 CmpOp0);
9023 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9024 DAG.getConstant(X86::COND_B, MVT::i8),
9025 SDValue(Neg.getNode(), 1));
9026 return Res;
9027 }
9028
Chris Lattnera2b56002010-12-05 01:23:24 +00009029 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9030 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009031 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009032
Chris Lattner96908b12010-12-05 02:00:51 +00009033 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009034 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9035 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009036
Chris Lattner96908b12010-12-05 02:00:51 +00009037 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9038 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009039
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009040 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009041 if (N2C == 0 || !N2C->isNullValue())
9042 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9043 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009044 }
9045 }
9046
Chris Lattnera2b56002010-12-05 01:23:24 +00009047 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009048 if (Cond.getOpcode() == ISD::AND &&
9049 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9050 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009051 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009052 Cond = Cond.getOperand(0);
9053 }
9054
Evan Cheng3f41d662007-10-08 22:16:29 +00009055 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9056 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009057 unsigned CondOpcode = Cond.getOpcode();
9058 if (CondOpcode == X86ISD::SETCC ||
9059 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009060 CC = Cond.getOperand(0);
9061
Dan Gohman475871a2008-07-27 21:46:04 +00009062 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009063 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009064 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009065
Evan Cheng3f41d662007-10-08 22:16:29 +00009066 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009067 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009068 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009069 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009070
Chris Lattnerd1980a52009-03-12 06:52:53 +00009071 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9072 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009073 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009074 addTest = false;
9075 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009076 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9077 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9078 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9079 Cond.getOperand(0).getValueType() != MVT::i8)) {
9080 SDValue LHS = Cond.getOperand(0);
9081 SDValue RHS = Cond.getOperand(1);
9082 unsigned X86Opcode;
9083 unsigned X86Cond;
9084 SDVTList VTs;
9085 switch (CondOpcode) {
9086 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9087 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9088 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9089 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9090 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9091 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9092 default: llvm_unreachable("unexpected overflowing operator");
9093 }
9094 if (CondOpcode == ISD::UMULO)
9095 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9096 MVT::i32);
9097 else
9098 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9099
9100 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9101
9102 if (CondOpcode == ISD::UMULO)
9103 Cond = X86Op.getValue(2);
9104 else
9105 Cond = X86Op.getValue(1);
9106
9107 CC = DAG.getConstant(X86Cond, MVT::i8);
9108 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009109 }
9110
9111 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009112 // Look pass the truncate if the high bits are known zero.
9113 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9114 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009115
9116 // We know the result of AND is compared against zero. Try to match
9117 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009118 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009119 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009120 if (NewSetCC.getNode()) {
9121 CC = NewSetCC.getOperand(0);
9122 Cond = NewSetCC.getOperand(1);
9123 addTest = false;
9124 }
9125 }
9126 }
9127
9128 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009129 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009130 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009131 }
9132
Benjamin Kramere915ff32010-12-22 23:09:28 +00009133 // a < b ? -1 : 0 -> RES = ~setcc_carry
9134 // a < b ? 0 : -1 -> RES = setcc_carry
9135 // a >= b ? -1 : 0 -> RES = setcc_carry
9136 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009137 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009138 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009139 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9140
9141 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9142 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9143 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9144 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9145 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9146 return DAG.getNOT(DL, Res, Res.getValueType());
9147 return Res;
9148 }
9149 }
9150
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009151 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9152 // widen the cmov and push the truncate through. This avoids introducing a new
9153 // branch during isel and doesn't add any extensions.
9154 if (Op.getValueType() == MVT::i8 &&
9155 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9156 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9157 if (T1.getValueType() == T2.getValueType() &&
9158 // Blacklist CopyFromReg to avoid partial register stalls.
9159 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9160 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
9161 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T1, T2, CC, Cond);
9162 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9163 }
9164 }
9165
Evan Cheng0488db92007-09-25 01:57:46 +00009166 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9167 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009168 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009169 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009170 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009171}
9172
Evan Cheng370e5342008-12-03 08:38:43 +00009173// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9174// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9175// from the AND / OR.
9176static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9177 Opc = Op.getOpcode();
9178 if (Opc != ISD::OR && Opc != ISD::AND)
9179 return false;
9180 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9181 Op.getOperand(0).hasOneUse() &&
9182 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9183 Op.getOperand(1).hasOneUse());
9184}
9185
Evan Cheng961d6d42009-02-02 08:19:07 +00009186// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9187// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009188static bool isXor1OfSetCC(SDValue Op) {
9189 if (Op.getOpcode() != ISD::XOR)
9190 return false;
9191 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9192 if (N1C && N1C->getAPIntValue() == 1) {
9193 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9194 Op.getOperand(0).hasOneUse();
9195 }
9196 return false;
9197}
9198
Dan Gohmand858e902010-04-17 15:26:15 +00009199SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009200 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009201 SDValue Chain = Op.getOperand(0);
9202 SDValue Cond = Op.getOperand(1);
9203 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009204 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009205 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009206 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009207
Dan Gohman1a492952009-10-20 16:22:37 +00009208 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009209 // Check for setcc([su]{add,sub,mul}o == 0).
9210 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9211 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9212 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9213 Cond.getOperand(0).getResNo() == 1 &&
9214 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9215 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9216 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9217 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9218 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9219 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9220 Inverted = true;
9221 Cond = Cond.getOperand(0);
9222 } else {
9223 SDValue NewCond = LowerSETCC(Cond, DAG);
9224 if (NewCond.getNode())
9225 Cond = NewCond;
9226 }
Dan Gohman1a492952009-10-20 16:22:37 +00009227 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009228#if 0
9229 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009230 else if (Cond.getOpcode() == X86ISD::ADD ||
9231 Cond.getOpcode() == X86ISD::SUB ||
9232 Cond.getOpcode() == X86ISD::SMUL ||
9233 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009234 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009235#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009236
Evan Chengad9c0a32009-12-15 00:53:42 +00009237 // Look pass (and (setcc_carry (cmp ...)), 1).
9238 if (Cond.getOpcode() == ISD::AND &&
9239 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9240 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009241 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009242 Cond = Cond.getOperand(0);
9243 }
9244
Evan Cheng3f41d662007-10-08 22:16:29 +00009245 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9246 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009247 unsigned CondOpcode = Cond.getOpcode();
9248 if (CondOpcode == X86ISD::SETCC ||
9249 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009250 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009251
Dan Gohman475871a2008-07-27 21:46:04 +00009252 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009253 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009254 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009255 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009256 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009257 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009258 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009259 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009260 default: break;
9261 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009262 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009263 // These can only come from an arithmetic instruction with overflow,
9264 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009265 Cond = Cond.getNode()->getOperand(1);
9266 addTest = false;
9267 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009268 }
Evan Cheng0488db92007-09-25 01:57:46 +00009269 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009270 }
9271 CondOpcode = Cond.getOpcode();
9272 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9273 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9274 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9275 Cond.getOperand(0).getValueType() != MVT::i8)) {
9276 SDValue LHS = Cond.getOperand(0);
9277 SDValue RHS = Cond.getOperand(1);
9278 unsigned X86Opcode;
9279 unsigned X86Cond;
9280 SDVTList VTs;
9281 switch (CondOpcode) {
9282 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9283 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9284 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9285 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9286 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9287 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9288 default: llvm_unreachable("unexpected overflowing operator");
9289 }
9290 if (Inverted)
9291 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9292 if (CondOpcode == ISD::UMULO)
9293 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9294 MVT::i32);
9295 else
9296 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9297
9298 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9299
9300 if (CondOpcode == ISD::UMULO)
9301 Cond = X86Op.getValue(2);
9302 else
9303 Cond = X86Op.getValue(1);
9304
9305 CC = DAG.getConstant(X86Cond, MVT::i8);
9306 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009307 } else {
9308 unsigned CondOpc;
9309 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9310 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009311 if (CondOpc == ISD::OR) {
9312 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9313 // two branches instead of an explicit OR instruction with a
9314 // separate test.
9315 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009316 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009317 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009318 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009319 Chain, Dest, CC, Cmp);
9320 CC = Cond.getOperand(1).getOperand(0);
9321 Cond = Cmp;
9322 addTest = false;
9323 }
9324 } else { // ISD::AND
9325 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9326 // two branches instead of an explicit AND instruction with a
9327 // separate test. However, we only do this if this block doesn't
9328 // have a fall-through edge, because this requires an explicit
9329 // jmp when the condition is false.
9330 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009331 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009332 Op.getNode()->hasOneUse()) {
9333 X86::CondCode CCode =
9334 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9335 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009336 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009337 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009338 // Look for an unconditional branch following this conditional branch.
9339 // We need this because we need to reverse the successors in order
9340 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009341 if (User->getOpcode() == ISD::BR) {
9342 SDValue FalseBB = User->getOperand(1);
9343 SDNode *NewBR =
9344 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009345 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009346 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009347 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009348
Dale Johannesene4d209d2009-02-03 20:21:25 +00009349 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009350 Chain, Dest, CC, Cmp);
9351 X86::CondCode CCode =
9352 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9353 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009354 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009355 Cond = Cmp;
9356 addTest = false;
9357 }
9358 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009359 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009360 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9361 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9362 // It should be transformed during dag combiner except when the condition
9363 // is set by a arithmetics with overflow node.
9364 X86::CondCode CCode =
9365 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9366 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009367 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009368 Cond = Cond.getOperand(0).getOperand(1);
9369 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009370 } else if (Cond.getOpcode() == ISD::SETCC &&
9371 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9372 // For FCMP_OEQ, we can emit
9373 // two branches instead of an explicit AND instruction with a
9374 // separate test. However, we only do this if this block doesn't
9375 // have a fall-through edge, because this requires an explicit
9376 // jmp when the condition is false.
9377 if (Op.getNode()->hasOneUse()) {
9378 SDNode *User = *Op.getNode()->use_begin();
9379 // Look for an unconditional branch following this conditional branch.
9380 // We need this because we need to reverse the successors in order
9381 // to implement FCMP_OEQ.
9382 if (User->getOpcode() == ISD::BR) {
9383 SDValue FalseBB = User->getOperand(1);
9384 SDNode *NewBR =
9385 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9386 assert(NewBR == User);
9387 (void)NewBR;
9388 Dest = FalseBB;
9389
9390 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9391 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009392 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009393 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9394 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9395 Chain, Dest, CC, Cmp);
9396 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9397 Cond = Cmp;
9398 addTest = false;
9399 }
9400 }
9401 } else if (Cond.getOpcode() == ISD::SETCC &&
9402 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9403 // For FCMP_UNE, we can emit
9404 // two branches instead of an explicit AND instruction with a
9405 // separate test. However, we only do this if this block doesn't
9406 // have a fall-through edge, because this requires an explicit
9407 // jmp when the condition is false.
9408 if (Op.getNode()->hasOneUse()) {
9409 SDNode *User = *Op.getNode()->use_begin();
9410 // Look for an unconditional branch following this conditional branch.
9411 // We need this because we need to reverse the successors in order
9412 // to implement FCMP_UNE.
9413 if (User->getOpcode() == ISD::BR) {
9414 SDValue FalseBB = User->getOperand(1);
9415 SDNode *NewBR =
9416 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9417 assert(NewBR == User);
9418 (void)NewBR;
9419
9420 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9421 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009422 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009423 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9424 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9425 Chain, Dest, CC, Cmp);
9426 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9427 Cond = Cmp;
9428 addTest = false;
9429 Dest = FalseBB;
9430 }
9431 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009432 }
Evan Cheng0488db92007-09-25 01:57:46 +00009433 }
9434
9435 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009436 // Look pass the truncate if the high bits are known zero.
9437 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9438 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009439
9440 // We know the result of AND is compared against zero. Try to match
9441 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009442 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009443 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9444 if (NewSetCC.getNode()) {
9445 CC = NewSetCC.getOperand(0);
9446 Cond = NewSetCC.getOperand(1);
9447 addTest = false;
9448 }
9449 }
9450 }
9451
9452 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009453 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009454 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009455 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009456 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009457 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009458 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009459}
9460
Anton Korobeynikove060b532007-04-17 19:34:00 +00009461
9462// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9463// Calls to _alloca is needed to probe the stack when allocating more than 4k
9464// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9465// that the guard pages used by the OS virtual memory manager are allocated in
9466// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009467SDValue
9468X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009469 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009470 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009471 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009472 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009473 "are being used");
9474 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009475 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009476
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009477 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009478 SDValue Chain = Op.getOperand(0);
9479 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009480 // FIXME: Ensure alignment here
9481
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009482 bool Is64Bit = Subtarget->is64Bit();
9483 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009484
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009485 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009486 MachineFunction &MF = DAG.getMachineFunction();
9487 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009488
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009489 if (Is64Bit) {
9490 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009491 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009492 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009493
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009494 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009495 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009496 if (I->hasNestAttr())
9497 report_fatal_error("Cannot use segmented stacks with functions that "
9498 "have nested arguments.");
9499 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009500
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009501 const TargetRegisterClass *AddrRegClass =
9502 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9503 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9504 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9505 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9506 DAG.getRegister(Vreg, SPTy));
9507 SDValue Ops1[2] = { Value, Chain };
9508 return DAG.getMergeValues(Ops1, 2, dl);
9509 } else {
9510 SDValue Flag;
9511 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009512
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009513 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9514 Flag = Chain.getValue(1);
9515 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009516
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009517 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9518 Flag = Chain.getValue(1);
9519
9520 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9521
9522 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9523 return DAG.getMergeValues(Ops1, 2, dl);
9524 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009525}
9526
Dan Gohmand858e902010-04-17 15:26:15 +00009527SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009528 MachineFunction &MF = DAG.getMachineFunction();
9529 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9530
Dan Gohman69de1932008-02-06 22:27:42 +00009531 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009532 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009533
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009534 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009535 // vastart just stores the address of the VarArgsFrameIndex slot into the
9536 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009537 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9538 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009539 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9540 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009541 }
9542
9543 // __va_list_tag:
9544 // gp_offset (0 - 6 * 8)
9545 // fp_offset (48 - 48 + 8 * 16)
9546 // overflow_arg_area (point to parameters coming in memory).
9547 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009548 SmallVector<SDValue, 8> MemOps;
9549 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009550 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009551 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009552 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9553 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009554 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009555 MemOps.push_back(Store);
9556
9557 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009558 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009559 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009560 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009561 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9562 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009563 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009564 MemOps.push_back(Store);
9565
9566 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009567 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009568 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009569 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9570 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009571 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9572 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009573 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009574 MemOps.push_back(Store);
9575
9576 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009577 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009578 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009579 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9580 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009581 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9582 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009583 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009584 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009585 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009586}
9587
Dan Gohmand858e902010-04-17 15:26:15 +00009588SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009589 assert(Subtarget->is64Bit() &&
9590 "LowerVAARG only handles 64-bit va_arg!");
9591 assert((Subtarget->isTargetLinux() ||
9592 Subtarget->isTargetDarwin()) &&
9593 "Unhandled target in LowerVAARG");
9594 assert(Op.getNode()->getNumOperands() == 4);
9595 SDValue Chain = Op.getOperand(0);
9596 SDValue SrcPtr = Op.getOperand(1);
9597 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9598 unsigned Align = Op.getConstantOperandVal(3);
9599 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009600
Dan Gohman320afb82010-10-12 18:00:49 +00009601 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009602 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00009603 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +00009604 uint8_t ArgMode;
9605
9606 // Decide which area this value should be read from.
9607 // TODO: Implement the AMD64 ABI in its entirety. This simple
9608 // selection mechanism works only for the basic types.
9609 if (ArgVT == MVT::f80) {
9610 llvm_unreachable("va_arg for f80 not yet implemented");
9611 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9612 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9613 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9614 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9615 } else {
9616 llvm_unreachable("Unhandled argument type in LowerVAARG");
9617 }
9618
9619 if (ArgMode == 2) {
9620 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009621 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009622 !(DAG.getMachineFunction()
Bill Wendling67658342012-10-09 07:45:08 +00009623 .getFunction()->getFnAttributes()
9624 .hasAttribute(Attributes::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009625 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009626 }
9627
9628 // Insert VAARG_64 node into the DAG
9629 // VAARG_64 returns two values: Variable Argument Address, Chain
9630 SmallVector<SDValue, 11> InstOps;
9631 InstOps.push_back(Chain);
9632 InstOps.push_back(SrcPtr);
9633 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9634 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9635 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9636 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9637 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9638 VTs, &InstOps[0], InstOps.size(),
9639 MVT::i64,
9640 MachinePointerInfo(SV),
9641 /*Align=*/0,
9642 /*Volatile=*/false,
9643 /*ReadMem=*/true,
9644 /*WriteMem=*/true);
9645 Chain = VAARG.getValue(1);
9646
9647 // Load the next argument and return it
9648 return DAG.getLoad(ArgVT, dl,
9649 Chain,
9650 VAARG,
9651 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009652 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009653}
9654
Craig Topper55b24052012-09-11 06:15:32 +00009655static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9656 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009657 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009658 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009659 SDValue Chain = Op.getOperand(0);
9660 SDValue DstPtr = Op.getOperand(1);
9661 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009662 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9663 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009664 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009665
Chris Lattnere72f2022010-09-21 05:40:29 +00009666 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009667 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009668 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009669 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009670}
9671
Craig Topper80e46362012-01-23 06:16:53 +00009672// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9673// may or may not be a constant. Takes immediate version of shift as input.
9674static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9675 SDValue SrcOp, SDValue ShAmt,
9676 SelectionDAG &DAG) {
9677 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9678
9679 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009680 // Constant may be a TargetConstant. Use a regular constant.
9681 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009682 switch (Opc) {
9683 default: llvm_unreachable("Unknown target vector shift node");
9684 case X86ISD::VSHLI:
9685 case X86ISD::VSRLI:
9686 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009687 return DAG.getNode(Opc, dl, VT, SrcOp,
9688 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009689 }
9690 }
9691
9692 // Change opcode to non-immediate version
9693 switch (Opc) {
9694 default: llvm_unreachable("Unknown target vector shift node");
9695 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9696 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9697 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9698 }
9699
9700 // Need to build a vector containing shift amount
9701 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9702 SDValue ShOps[4];
9703 ShOps[0] = ShAmt;
9704 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009705 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009706 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009707
9708 // The return type has to be a 128-bit type with the same element
9709 // type as the input type.
9710 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9711 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9712
9713 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009714 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9715}
9716
Craig Topper55b24052012-09-11 06:15:32 +00009717static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009718 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009719 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009720 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009721 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009722 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009723 case Intrinsic::x86_sse_comieq_ss:
9724 case Intrinsic::x86_sse_comilt_ss:
9725 case Intrinsic::x86_sse_comile_ss:
9726 case Intrinsic::x86_sse_comigt_ss:
9727 case Intrinsic::x86_sse_comige_ss:
9728 case Intrinsic::x86_sse_comineq_ss:
9729 case Intrinsic::x86_sse_ucomieq_ss:
9730 case Intrinsic::x86_sse_ucomilt_ss:
9731 case Intrinsic::x86_sse_ucomile_ss:
9732 case Intrinsic::x86_sse_ucomigt_ss:
9733 case Intrinsic::x86_sse_ucomige_ss:
9734 case Intrinsic::x86_sse_ucomineq_ss:
9735 case Intrinsic::x86_sse2_comieq_sd:
9736 case Intrinsic::x86_sse2_comilt_sd:
9737 case Intrinsic::x86_sse2_comile_sd:
9738 case Intrinsic::x86_sse2_comigt_sd:
9739 case Intrinsic::x86_sse2_comige_sd:
9740 case Intrinsic::x86_sse2_comineq_sd:
9741 case Intrinsic::x86_sse2_ucomieq_sd:
9742 case Intrinsic::x86_sse2_ucomilt_sd:
9743 case Intrinsic::x86_sse2_ucomile_sd:
9744 case Intrinsic::x86_sse2_ucomigt_sd:
9745 case Intrinsic::x86_sse2_ucomige_sd:
9746 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009747 unsigned Opc;
9748 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009749 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009750 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009751 case Intrinsic::x86_sse_comieq_ss:
9752 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009753 Opc = X86ISD::COMI;
9754 CC = ISD::SETEQ;
9755 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009756 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009757 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009758 Opc = X86ISD::COMI;
9759 CC = ISD::SETLT;
9760 break;
9761 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009762 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009763 Opc = X86ISD::COMI;
9764 CC = ISD::SETLE;
9765 break;
9766 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009767 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009768 Opc = X86ISD::COMI;
9769 CC = ISD::SETGT;
9770 break;
9771 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009772 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009773 Opc = X86ISD::COMI;
9774 CC = ISD::SETGE;
9775 break;
9776 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009777 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009778 Opc = X86ISD::COMI;
9779 CC = ISD::SETNE;
9780 break;
9781 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009782 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009783 Opc = X86ISD::UCOMI;
9784 CC = ISD::SETEQ;
9785 break;
9786 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009787 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009788 Opc = X86ISD::UCOMI;
9789 CC = ISD::SETLT;
9790 break;
9791 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009792 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009793 Opc = X86ISD::UCOMI;
9794 CC = ISD::SETLE;
9795 break;
9796 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009797 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009798 Opc = X86ISD::UCOMI;
9799 CC = ISD::SETGT;
9800 break;
9801 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009802 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009803 Opc = X86ISD::UCOMI;
9804 CC = ISD::SETGE;
9805 break;
9806 case Intrinsic::x86_sse_ucomineq_ss:
9807 case Intrinsic::x86_sse2_ucomineq_sd:
9808 Opc = X86ISD::UCOMI;
9809 CC = ISD::SETNE;
9810 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009811 }
Evan Cheng734503b2006-09-11 02:19:56 +00009812
Dan Gohman475871a2008-07-27 21:46:04 +00009813 SDValue LHS = Op.getOperand(1);
9814 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009815 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009816 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009817 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9818 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9819 DAG.getConstant(X86CC, MVT::i8), Cond);
9820 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009821 }
Craig Topper6d688152012-08-14 07:43:25 +00009822
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009823 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009824 case Intrinsic::x86_sse2_pmulu_dq:
9825 case Intrinsic::x86_avx2_pmulu_dq:
9826 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9827 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009828
9829 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009830 case Intrinsic::x86_sse3_hadd_ps:
9831 case Intrinsic::x86_sse3_hadd_pd:
9832 case Intrinsic::x86_avx_hadd_ps_256:
9833 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009834 case Intrinsic::x86_sse3_hsub_ps:
9835 case Intrinsic::x86_sse3_hsub_pd:
9836 case Intrinsic::x86_avx_hsub_ps_256:
9837 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009838 case Intrinsic::x86_ssse3_phadd_w_128:
9839 case Intrinsic::x86_ssse3_phadd_d_128:
9840 case Intrinsic::x86_avx2_phadd_w:
9841 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009842 case Intrinsic::x86_ssse3_phsub_w_128:
9843 case Intrinsic::x86_ssse3_phsub_d_128:
9844 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009845 case Intrinsic::x86_avx2_phsub_d: {
9846 unsigned Opcode;
9847 switch (IntNo) {
9848 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9849 case Intrinsic::x86_sse3_hadd_ps:
9850 case Intrinsic::x86_sse3_hadd_pd:
9851 case Intrinsic::x86_avx_hadd_ps_256:
9852 case Intrinsic::x86_avx_hadd_pd_256:
9853 Opcode = X86ISD::FHADD;
9854 break;
9855 case Intrinsic::x86_sse3_hsub_ps:
9856 case Intrinsic::x86_sse3_hsub_pd:
9857 case Intrinsic::x86_avx_hsub_ps_256:
9858 case Intrinsic::x86_avx_hsub_pd_256:
9859 Opcode = X86ISD::FHSUB;
9860 break;
9861 case Intrinsic::x86_ssse3_phadd_w_128:
9862 case Intrinsic::x86_ssse3_phadd_d_128:
9863 case Intrinsic::x86_avx2_phadd_w:
9864 case Intrinsic::x86_avx2_phadd_d:
9865 Opcode = X86ISD::HADD;
9866 break;
9867 case Intrinsic::x86_ssse3_phsub_w_128:
9868 case Intrinsic::x86_ssse3_phsub_d_128:
9869 case Intrinsic::x86_avx2_phsub_w:
9870 case Intrinsic::x86_avx2_phsub_d:
9871 Opcode = X86ISD::HSUB;
9872 break;
9873 }
9874 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009875 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009876 }
9877
9878 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009879 case Intrinsic::x86_avx2_psllv_d:
9880 case Intrinsic::x86_avx2_psllv_q:
9881 case Intrinsic::x86_avx2_psllv_d_256:
9882 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009883 case Intrinsic::x86_avx2_psrlv_d:
9884 case Intrinsic::x86_avx2_psrlv_q:
9885 case Intrinsic::x86_avx2_psrlv_d_256:
9886 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009887 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009888 case Intrinsic::x86_avx2_psrav_d_256: {
9889 unsigned Opcode;
9890 switch (IntNo) {
9891 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9892 case Intrinsic::x86_avx2_psllv_d:
9893 case Intrinsic::x86_avx2_psllv_q:
9894 case Intrinsic::x86_avx2_psllv_d_256:
9895 case Intrinsic::x86_avx2_psllv_q_256:
9896 Opcode = ISD::SHL;
9897 break;
9898 case Intrinsic::x86_avx2_psrlv_d:
9899 case Intrinsic::x86_avx2_psrlv_q:
9900 case Intrinsic::x86_avx2_psrlv_d_256:
9901 case Intrinsic::x86_avx2_psrlv_q_256:
9902 Opcode = ISD::SRL;
9903 break;
9904 case Intrinsic::x86_avx2_psrav_d:
9905 case Intrinsic::x86_avx2_psrav_d_256:
9906 Opcode = ISD::SRA;
9907 break;
9908 }
9909 return DAG.getNode(Opcode, dl, Op.getValueType(),
9910 Op.getOperand(1), Op.getOperand(2));
9911 }
9912
Craig Topper969ba282012-01-25 06:43:11 +00009913 case Intrinsic::x86_ssse3_pshuf_b_128:
9914 case Intrinsic::x86_avx2_pshuf_b:
9915 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9916 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009917
Craig Topper969ba282012-01-25 06:43:11 +00009918 case Intrinsic::x86_ssse3_psign_b_128:
9919 case Intrinsic::x86_ssse3_psign_w_128:
9920 case Intrinsic::x86_ssse3_psign_d_128:
9921 case Intrinsic::x86_avx2_psign_b:
9922 case Intrinsic::x86_avx2_psign_w:
9923 case Intrinsic::x86_avx2_psign_d:
9924 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9925 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009926
Craig Toppere566cd02012-01-26 07:18:03 +00009927 case Intrinsic::x86_sse41_insertps:
9928 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9929 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009930
Craig Toppere566cd02012-01-26 07:18:03 +00009931 case Intrinsic::x86_avx_vperm2f128_ps_256:
9932 case Intrinsic::x86_avx_vperm2f128_pd_256:
9933 case Intrinsic::x86_avx_vperm2f128_si_256:
9934 case Intrinsic::x86_avx2_vperm2i128:
9935 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9936 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009937
Craig Topperffa6c402012-04-16 07:13:00 +00009938 case Intrinsic::x86_avx2_permd:
9939 case Intrinsic::x86_avx2_permps:
9940 // Operands intentionally swapped. Mask is last operand to intrinsic,
9941 // but second operand for node/intruction.
9942 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9943 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009944
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009945 // ptest and testp intrinsics. The intrinsic these come from are designed to
9946 // return an integer value, not just an instruction so lower it to the ptest
9947 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009948 case Intrinsic::x86_sse41_ptestz:
9949 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009950 case Intrinsic::x86_sse41_ptestnzc:
9951 case Intrinsic::x86_avx_ptestz_256:
9952 case Intrinsic::x86_avx_ptestc_256:
9953 case Intrinsic::x86_avx_ptestnzc_256:
9954 case Intrinsic::x86_avx_vtestz_ps:
9955 case Intrinsic::x86_avx_vtestc_ps:
9956 case Intrinsic::x86_avx_vtestnzc_ps:
9957 case Intrinsic::x86_avx_vtestz_pd:
9958 case Intrinsic::x86_avx_vtestc_pd:
9959 case Intrinsic::x86_avx_vtestnzc_pd:
9960 case Intrinsic::x86_avx_vtestz_ps_256:
9961 case Intrinsic::x86_avx_vtestc_ps_256:
9962 case Intrinsic::x86_avx_vtestnzc_ps_256:
9963 case Intrinsic::x86_avx_vtestz_pd_256:
9964 case Intrinsic::x86_avx_vtestc_pd_256:
9965 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9966 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009967 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009968 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009969 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009970 case Intrinsic::x86_avx_vtestz_ps:
9971 case Intrinsic::x86_avx_vtestz_pd:
9972 case Intrinsic::x86_avx_vtestz_ps_256:
9973 case Intrinsic::x86_avx_vtestz_pd_256:
9974 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009975 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009976 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009977 // ZF = 1
9978 X86CC = X86::COND_E;
9979 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009980 case Intrinsic::x86_avx_vtestc_ps:
9981 case Intrinsic::x86_avx_vtestc_pd:
9982 case Intrinsic::x86_avx_vtestc_ps_256:
9983 case Intrinsic::x86_avx_vtestc_pd_256:
9984 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009985 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009986 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009987 // CF = 1
9988 X86CC = X86::COND_B;
9989 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009990 case Intrinsic::x86_avx_vtestnzc_ps:
9991 case Intrinsic::x86_avx_vtestnzc_pd:
9992 case Intrinsic::x86_avx_vtestnzc_ps_256:
9993 case Intrinsic::x86_avx_vtestnzc_pd_256:
9994 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009995 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009996 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009997 // ZF and CF = 0
9998 X86CC = X86::COND_A;
9999 break;
10000 }
Eric Christopherfd179292009-08-27 18:07:15 +000010001
Eric Christopher71c67532009-07-29 00:28:05 +000010002 SDValue LHS = Op.getOperand(1);
10003 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010004 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10005 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010006 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10007 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10008 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010009 }
Evan Cheng5759f972008-05-04 09:15:50 +000010010
Craig Topper80e46362012-01-23 06:16:53 +000010011 // SSE/AVX shift intrinsics
10012 case Intrinsic::x86_sse2_psll_w:
10013 case Intrinsic::x86_sse2_psll_d:
10014 case Intrinsic::x86_sse2_psll_q:
10015 case Intrinsic::x86_avx2_psll_w:
10016 case Intrinsic::x86_avx2_psll_d:
10017 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010018 case Intrinsic::x86_sse2_psrl_w:
10019 case Intrinsic::x86_sse2_psrl_d:
10020 case Intrinsic::x86_sse2_psrl_q:
10021 case Intrinsic::x86_avx2_psrl_w:
10022 case Intrinsic::x86_avx2_psrl_d:
10023 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010024 case Intrinsic::x86_sse2_psra_w:
10025 case Intrinsic::x86_sse2_psra_d:
10026 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010027 case Intrinsic::x86_avx2_psra_d: {
10028 unsigned Opcode;
10029 switch (IntNo) {
10030 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10031 case Intrinsic::x86_sse2_psll_w:
10032 case Intrinsic::x86_sse2_psll_d:
10033 case Intrinsic::x86_sse2_psll_q:
10034 case Intrinsic::x86_avx2_psll_w:
10035 case Intrinsic::x86_avx2_psll_d:
10036 case Intrinsic::x86_avx2_psll_q:
10037 Opcode = X86ISD::VSHL;
10038 break;
10039 case Intrinsic::x86_sse2_psrl_w:
10040 case Intrinsic::x86_sse2_psrl_d:
10041 case Intrinsic::x86_sse2_psrl_q:
10042 case Intrinsic::x86_avx2_psrl_w:
10043 case Intrinsic::x86_avx2_psrl_d:
10044 case Intrinsic::x86_avx2_psrl_q:
10045 Opcode = X86ISD::VSRL;
10046 break;
10047 case Intrinsic::x86_sse2_psra_w:
10048 case Intrinsic::x86_sse2_psra_d:
10049 case Intrinsic::x86_avx2_psra_w:
10050 case Intrinsic::x86_avx2_psra_d:
10051 Opcode = X86ISD::VSRA;
10052 break;
10053 }
10054 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010055 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010056 }
10057
10058 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010059 case Intrinsic::x86_sse2_pslli_w:
10060 case Intrinsic::x86_sse2_pslli_d:
10061 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010062 case Intrinsic::x86_avx2_pslli_w:
10063 case Intrinsic::x86_avx2_pslli_d:
10064 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010065 case Intrinsic::x86_sse2_psrli_w:
10066 case Intrinsic::x86_sse2_psrli_d:
10067 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010068 case Intrinsic::x86_avx2_psrli_w:
10069 case Intrinsic::x86_avx2_psrli_d:
10070 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010071 case Intrinsic::x86_sse2_psrai_w:
10072 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010073 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010074 case Intrinsic::x86_avx2_psrai_d: {
10075 unsigned Opcode;
10076 switch (IntNo) {
10077 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10078 case Intrinsic::x86_sse2_pslli_w:
10079 case Intrinsic::x86_sse2_pslli_d:
10080 case Intrinsic::x86_sse2_pslli_q:
10081 case Intrinsic::x86_avx2_pslli_w:
10082 case Intrinsic::x86_avx2_pslli_d:
10083 case Intrinsic::x86_avx2_pslli_q:
10084 Opcode = X86ISD::VSHLI;
10085 break;
10086 case Intrinsic::x86_sse2_psrli_w:
10087 case Intrinsic::x86_sse2_psrli_d:
10088 case Intrinsic::x86_sse2_psrli_q:
10089 case Intrinsic::x86_avx2_psrli_w:
10090 case Intrinsic::x86_avx2_psrli_d:
10091 case Intrinsic::x86_avx2_psrli_q:
10092 Opcode = X86ISD::VSRLI;
10093 break;
10094 case Intrinsic::x86_sse2_psrai_w:
10095 case Intrinsic::x86_sse2_psrai_d:
10096 case Intrinsic::x86_avx2_psrai_w:
10097 case Intrinsic::x86_avx2_psrai_d:
10098 Opcode = X86ISD::VSRAI;
10099 break;
10100 }
10101 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010102 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010103 }
10104
Craig Topper4feb6472012-08-06 06:22:36 +000010105 case Intrinsic::x86_sse42_pcmpistria128:
10106 case Intrinsic::x86_sse42_pcmpestria128:
10107 case Intrinsic::x86_sse42_pcmpistric128:
10108 case Intrinsic::x86_sse42_pcmpestric128:
10109 case Intrinsic::x86_sse42_pcmpistrio128:
10110 case Intrinsic::x86_sse42_pcmpestrio128:
10111 case Intrinsic::x86_sse42_pcmpistris128:
10112 case Intrinsic::x86_sse42_pcmpestris128:
10113 case Intrinsic::x86_sse42_pcmpistriz128:
10114 case Intrinsic::x86_sse42_pcmpestriz128: {
10115 unsigned Opcode;
10116 unsigned X86CC;
10117 switch (IntNo) {
10118 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10119 case Intrinsic::x86_sse42_pcmpistria128:
10120 Opcode = X86ISD::PCMPISTRI;
10121 X86CC = X86::COND_A;
10122 break;
10123 case Intrinsic::x86_sse42_pcmpestria128:
10124 Opcode = X86ISD::PCMPESTRI;
10125 X86CC = X86::COND_A;
10126 break;
10127 case Intrinsic::x86_sse42_pcmpistric128:
10128 Opcode = X86ISD::PCMPISTRI;
10129 X86CC = X86::COND_B;
10130 break;
10131 case Intrinsic::x86_sse42_pcmpestric128:
10132 Opcode = X86ISD::PCMPESTRI;
10133 X86CC = X86::COND_B;
10134 break;
10135 case Intrinsic::x86_sse42_pcmpistrio128:
10136 Opcode = X86ISD::PCMPISTRI;
10137 X86CC = X86::COND_O;
10138 break;
10139 case Intrinsic::x86_sse42_pcmpestrio128:
10140 Opcode = X86ISD::PCMPESTRI;
10141 X86CC = X86::COND_O;
10142 break;
10143 case Intrinsic::x86_sse42_pcmpistris128:
10144 Opcode = X86ISD::PCMPISTRI;
10145 X86CC = X86::COND_S;
10146 break;
10147 case Intrinsic::x86_sse42_pcmpestris128:
10148 Opcode = X86ISD::PCMPESTRI;
10149 X86CC = X86::COND_S;
10150 break;
10151 case Intrinsic::x86_sse42_pcmpistriz128:
10152 Opcode = X86ISD::PCMPISTRI;
10153 X86CC = X86::COND_E;
10154 break;
10155 case Intrinsic::x86_sse42_pcmpestriz128:
10156 Opcode = X86ISD::PCMPESTRI;
10157 X86CC = X86::COND_E;
10158 break;
10159 }
10160 SmallVector<SDValue, 5> NewOps;
10161 NewOps.append(Op->op_begin()+1, Op->op_end());
10162 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10163 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10164 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10165 DAG.getConstant(X86CC, MVT::i8),
10166 SDValue(PCMP.getNode(), 1));
10167 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10168 }
Craig Topper6d688152012-08-14 07:43:25 +000010169
Craig Topper4feb6472012-08-06 06:22:36 +000010170 case Intrinsic::x86_sse42_pcmpistri128:
10171 case Intrinsic::x86_sse42_pcmpestri128: {
10172 unsigned Opcode;
10173 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10174 Opcode = X86ISD::PCMPISTRI;
10175 else
10176 Opcode = X86ISD::PCMPESTRI;
10177
10178 SmallVector<SDValue, 5> NewOps;
10179 NewOps.append(Op->op_begin()+1, Op->op_end());
10180 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10181 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10182 }
Craig Topper0e292372012-08-24 04:03:22 +000010183 case Intrinsic::x86_fma_vfmadd_ps:
10184 case Intrinsic::x86_fma_vfmadd_pd:
10185 case Intrinsic::x86_fma_vfmsub_ps:
10186 case Intrinsic::x86_fma_vfmsub_pd:
10187 case Intrinsic::x86_fma_vfnmadd_ps:
10188 case Intrinsic::x86_fma_vfnmadd_pd:
10189 case Intrinsic::x86_fma_vfnmsub_ps:
10190 case Intrinsic::x86_fma_vfnmsub_pd:
10191 case Intrinsic::x86_fma_vfmaddsub_ps:
10192 case Intrinsic::x86_fma_vfmaddsub_pd:
10193 case Intrinsic::x86_fma_vfmsubadd_ps:
10194 case Intrinsic::x86_fma_vfmsubadd_pd:
10195 case Intrinsic::x86_fma_vfmadd_ps_256:
10196 case Intrinsic::x86_fma_vfmadd_pd_256:
10197 case Intrinsic::x86_fma_vfmsub_ps_256:
10198 case Intrinsic::x86_fma_vfmsub_pd_256:
10199 case Intrinsic::x86_fma_vfnmadd_ps_256:
10200 case Intrinsic::x86_fma_vfnmadd_pd_256:
10201 case Intrinsic::x86_fma_vfnmsub_ps_256:
10202 case Intrinsic::x86_fma_vfnmsub_pd_256:
10203 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10204 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10205 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10206 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010207 unsigned Opc;
10208 switch (IntNo) {
10209 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10210 case Intrinsic::x86_fma_vfmadd_ps:
10211 case Intrinsic::x86_fma_vfmadd_pd:
10212 case Intrinsic::x86_fma_vfmadd_ps_256:
10213 case Intrinsic::x86_fma_vfmadd_pd_256:
10214 Opc = X86ISD::FMADD;
10215 break;
10216 case Intrinsic::x86_fma_vfmsub_ps:
10217 case Intrinsic::x86_fma_vfmsub_pd:
10218 case Intrinsic::x86_fma_vfmsub_ps_256:
10219 case Intrinsic::x86_fma_vfmsub_pd_256:
10220 Opc = X86ISD::FMSUB;
10221 break;
10222 case Intrinsic::x86_fma_vfnmadd_ps:
10223 case Intrinsic::x86_fma_vfnmadd_pd:
10224 case Intrinsic::x86_fma_vfnmadd_ps_256:
10225 case Intrinsic::x86_fma_vfnmadd_pd_256:
10226 Opc = X86ISD::FNMADD;
10227 break;
10228 case Intrinsic::x86_fma_vfnmsub_ps:
10229 case Intrinsic::x86_fma_vfnmsub_pd:
10230 case Intrinsic::x86_fma_vfnmsub_ps_256:
10231 case Intrinsic::x86_fma_vfnmsub_pd_256:
10232 Opc = X86ISD::FNMSUB;
10233 break;
10234 case Intrinsic::x86_fma_vfmaddsub_ps:
10235 case Intrinsic::x86_fma_vfmaddsub_pd:
10236 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10237 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10238 Opc = X86ISD::FMADDSUB;
10239 break;
10240 case Intrinsic::x86_fma_vfmsubadd_ps:
10241 case Intrinsic::x86_fma_vfmsubadd_pd:
10242 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10243 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10244 Opc = X86ISD::FMSUBADD;
10245 break;
10246 }
10247
10248 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10249 Op.getOperand(2), Op.getOperand(3));
10250 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010251 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010252}
Evan Cheng72261582005-12-20 06:22:03 +000010253
Craig Topper55b24052012-09-11 06:15:32 +000010254static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010255 DebugLoc dl = Op.getDebugLoc();
10256 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10257 switch (IntNo) {
10258 default: return SDValue(); // Don't custom lower most intrinsics.
10259
10260 // RDRAND intrinsics.
10261 case Intrinsic::x86_rdrand_16:
10262 case Intrinsic::x86_rdrand_32:
10263 case Intrinsic::x86_rdrand_64: {
10264 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010265 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10266 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010267
10268 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10269 // return the value from Rand, which is always 0, casted to i32.
10270 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10271 DAG.getConstant(1, Op->getValueType(1)),
10272 DAG.getConstant(X86::COND_B, MVT::i32),
10273 SDValue(Result.getNode(), 1) };
10274 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10275 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10276 Ops, 4);
10277
10278 // Return { result, isValid, chain }.
10279 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010280 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010281 }
10282 }
10283}
10284
Dan Gohmand858e902010-04-17 15:26:15 +000010285SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10286 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010287 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10288 MFI->setReturnAddressIsTaken(true);
10289
Bill Wendling64e87322009-01-16 19:25:27 +000010290 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010291 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010292
10293 if (Depth > 0) {
10294 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10295 SDValue Offset =
Micah Villmowfb384d62012-10-11 21:27:41 +000010296 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010297 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010298 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010299 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010300 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010301 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010302 }
10303
10304 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010305 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010306 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010307 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010308}
10309
Dan Gohmand858e902010-04-17 15:26:15 +000010310SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010311 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10312 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010313
Owen Andersone50ed302009-08-10 22:56:29 +000010314 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010315 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010316 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10317 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010318 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010319 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010320 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10321 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010322 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010323 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010324}
10325
Dan Gohman475871a2008-07-27 21:46:04 +000010326SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010327 SelectionDAG &DAG) const {
Micah Villmowfb384d62012-10-11 21:27:41 +000010328 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010329}
10330
Dan Gohmand858e902010-04-17 15:26:15 +000010331SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010332 SDValue Chain = Op.getOperand(0);
10333 SDValue Offset = Op.getOperand(1);
10334 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010335 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010336
Dan Gohmand8816272010-08-11 18:14:00 +000010337 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10338 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10339 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010340 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010341
Dan Gohmand8816272010-08-11 18:14:00 +000010342 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Micah Villmowfb384d62012-10-11 21:27:41 +000010343 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010344 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010345 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10346 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010347 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010348
Dale Johannesene4d209d2009-02-03 20:21:25 +000010349 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010350 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010351 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010352}
10353
Craig Topper55b24052012-09-11 06:15:32 +000010354static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010355 return Op.getOperand(0);
10356}
10357
10358SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10359 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010360 SDValue Root = Op.getOperand(0);
10361 SDValue Trmp = Op.getOperand(1); // trampoline
10362 SDValue FPtr = Op.getOperand(2); // nested function
10363 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010364 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010365
Dan Gohman69de1932008-02-06 22:27:42 +000010366 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010367 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010368
10369 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010370 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010371
10372 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010373 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10374 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010375
Michael Liao7abf67a2012-10-04 19:50:43 +000010376 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10377 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010378
10379 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10380
10381 // Load the pointer to the nested function into R11.
10382 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010383 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010384 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010385 Addr, MachinePointerInfo(TrmpAddr),
10386 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010387
Owen Anderson825b72b2009-08-11 20:47:22 +000010388 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10389 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010390 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10391 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010392 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010393
10394 // Load the 'nest' parameter value into R10.
10395 // R10 is specified in X86CallingConv.td
10396 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010397 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10398 DAG.getConstant(10, MVT::i64));
10399 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010400 Addr, MachinePointerInfo(TrmpAddr, 10),
10401 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010402
Owen Anderson825b72b2009-08-11 20:47:22 +000010403 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10404 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010405 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10406 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010407 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010408
10409 // Jump to the nested function.
10410 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010411 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10412 DAG.getConstant(20, MVT::i64));
10413 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010414 Addr, MachinePointerInfo(TrmpAddr, 20),
10415 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010416
10417 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010418 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10419 DAG.getConstant(22, MVT::i64));
10420 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010421 MachinePointerInfo(TrmpAddr, 22),
10422 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010423
Duncan Sands4a544a72011-09-06 13:37:06 +000010424 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010425 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010426 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010427 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010428 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010429 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010430
10431 switch (CC) {
10432 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010433 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010434 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010435 case CallingConv::X86_StdCall: {
10436 // Pass 'nest' parameter in ECX.
10437 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010438 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010439
10440 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010441 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010442 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010443
Chris Lattner58d74912008-03-12 17:45:29 +000010444 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010445 unsigned InRegCount = 0;
10446 unsigned Idx = 1;
10447
10448 for (FunctionType::param_iterator I = FTy->param_begin(),
10449 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling67658342012-10-09 07:45:08 +000010450 if (Attrs.getParamAttributes(Idx).hasAttribute(Attributes::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010451 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010452 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010453
10454 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010455 report_fatal_error("Nest register in use - reduce number of inreg"
10456 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010457 }
10458 }
10459 break;
10460 }
10461 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010462 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010463 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010464 // Pass 'nest' parameter in EAX.
10465 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010466 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010467 break;
10468 }
10469
Dan Gohman475871a2008-07-27 21:46:04 +000010470 SDValue OutChains[4];
10471 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010472
Owen Anderson825b72b2009-08-11 20:47:22 +000010473 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10474 DAG.getConstant(10, MVT::i32));
10475 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010476
Chris Lattnera62fe662010-02-05 19:20:30 +000010477 // This is storing the opcode for MOV32ri.
10478 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000010479 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000010480 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010481 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010482 Trmp, MachinePointerInfo(TrmpAddr),
10483 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010484
Owen Anderson825b72b2009-08-11 20:47:22 +000010485 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10486 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010487 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10488 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010489 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010490
Chris Lattnera62fe662010-02-05 19:20:30 +000010491 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010492 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10493 DAG.getConstant(5, MVT::i32));
10494 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010495 MachinePointerInfo(TrmpAddr, 5),
10496 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010497
Owen Anderson825b72b2009-08-11 20:47:22 +000010498 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10499 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010500 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10501 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010502 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010503
Duncan Sands4a544a72011-09-06 13:37:06 +000010504 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010505 }
10506}
10507
Dan Gohmand858e902010-04-17 15:26:15 +000010508SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10509 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010510 /*
10511 The rounding mode is in bits 11:10 of FPSR, and has the following
10512 settings:
10513 00 Round to nearest
10514 01 Round to -inf
10515 10 Round to +inf
10516 11 Round to 0
10517
10518 FLT_ROUNDS, on the other hand, expects the following:
10519 -1 Undefined
10520 0 Round to 0
10521 1 Round to nearest
10522 2 Round to +inf
10523 3 Round to -inf
10524
10525 To perform the conversion, we do:
10526 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10527 */
10528
10529 MachineFunction &MF = DAG.getMachineFunction();
10530 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010531 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010532 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010533 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010534 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010535
10536 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010537 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010538 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010539
Michael J. Spencerec38de22010-10-10 22:04:20 +000010540
Chris Lattner2156b792010-09-22 01:11:26 +000010541 MachineMemOperand *MMO =
10542 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10543 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010544
Chris Lattner2156b792010-09-22 01:11:26 +000010545 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10546 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10547 DAG.getVTList(MVT::Other),
10548 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010549
10550 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010551 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010552 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010553
10554 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010555 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010556 DAG.getNode(ISD::SRL, DL, MVT::i16,
10557 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010558 CWD, DAG.getConstant(0x800, MVT::i16)),
10559 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010560 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010561 DAG.getNode(ISD::SRL, DL, MVT::i16,
10562 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010563 CWD, DAG.getConstant(0x400, MVT::i16)),
10564 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010565
Dan Gohman475871a2008-07-27 21:46:04 +000010566 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010567 DAG.getNode(ISD::AND, DL, MVT::i16,
10568 DAG.getNode(ISD::ADD, DL, MVT::i16,
10569 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010570 DAG.getConstant(1, MVT::i16)),
10571 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010572
10573
Duncan Sands83ec4b62008-06-06 12:08:01 +000010574 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010575 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010576}
10577
Craig Topper55b24052012-09-11 06:15:32 +000010578static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010579 EVT VT = Op.getValueType();
10580 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010581 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010582 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010583
10584 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010585 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010586 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010587 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010588 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010589 }
Evan Cheng18efe262007-12-14 02:13:44 +000010590
Evan Cheng152804e2007-12-14 08:30:15 +000010591 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010592 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010593 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010594
10595 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010596 SDValue Ops[] = {
10597 Op,
10598 DAG.getConstant(NumBits+NumBits-1, OpVT),
10599 DAG.getConstant(X86::COND_E, MVT::i8),
10600 Op.getValue(1)
10601 };
10602 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010603
10604 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010605 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010606
Owen Anderson825b72b2009-08-11 20:47:22 +000010607 if (VT == MVT::i8)
10608 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010609 return Op;
10610}
10611
Craig Topper55b24052012-09-11 06:15:32 +000010612static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010613 EVT VT = Op.getValueType();
10614 EVT OpVT = VT;
10615 unsigned NumBits = VT.getSizeInBits();
10616 DebugLoc dl = Op.getDebugLoc();
10617
10618 Op = Op.getOperand(0);
10619 if (VT == MVT::i8) {
10620 // Zero extend to i32 since there is not an i8 bsr.
10621 OpVT = MVT::i32;
10622 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10623 }
10624
10625 // Issue a bsr (scan bits in reverse).
10626 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10627 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10628
10629 // And xor with NumBits-1.
10630 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10631
10632 if (VT == MVT::i8)
10633 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10634 return Op;
10635}
10636
Craig Topper55b24052012-09-11 06:15:32 +000010637static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010638 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010639 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010640 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010641 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010642
10643 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010644 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010645 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010646
10647 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010648 SDValue Ops[] = {
10649 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010650 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010651 DAG.getConstant(X86::COND_E, MVT::i8),
10652 Op.getValue(1)
10653 };
Chandler Carruth77821022011-12-24 12:12:34 +000010654 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010655}
10656
Craig Topper13894fa2011-08-24 06:14:18 +000010657// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10658// ones, and then concatenate the result back.
10659static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010660 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010661
Craig Topper7a9a28b2012-08-12 02:23:29 +000010662 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010663 "Unsupported value type for operation");
10664
Craig Topper66ddd152012-04-27 22:54:43 +000010665 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010666 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010667
10668 // Extract the LHS vectors
10669 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010670 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10671 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010672
10673 // Extract the RHS vectors
10674 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010675 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10676 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010677
10678 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10679 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10680
10681 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10682 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10683 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10684}
10685
Craig Topper55b24052012-09-11 06:15:32 +000010686static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010687 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010688 Op.getValueType().isInteger() &&
10689 "Only handle AVX 256-bit vector integer operation");
10690 return Lower256IntArith(Op, DAG);
10691}
10692
Craig Topper55b24052012-09-11 06:15:32 +000010693static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010694 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010695 Op.getValueType().isInteger() &&
10696 "Only handle AVX 256-bit vector integer operation");
10697 return Lower256IntArith(Op, DAG);
10698}
10699
Craig Topper55b24052012-09-11 06:15:32 +000010700static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10701 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010702 EVT VT = Op.getValueType();
10703
10704 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010705 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010706 return Lower256IntArith(Op, DAG);
10707
Craig Topper5b209e82012-02-05 03:14:49 +000010708 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10709 "Only know how to lower V2I64/V4I64 multiply");
10710
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010711 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010712
Craig Topper5b209e82012-02-05 03:14:49 +000010713 // Ahi = psrlqi(a, 32);
10714 // Bhi = psrlqi(b, 32);
10715 //
10716 // AloBlo = pmuludq(a, b);
10717 // AloBhi = pmuludq(a, Bhi);
10718 // AhiBlo = pmuludq(Ahi, b);
10719
10720 // AloBhi = psllqi(AloBhi, 32);
10721 // AhiBlo = psllqi(AhiBlo, 32);
10722 // return AloBlo + AloBhi + AhiBlo;
10723
Craig Topperaaa643c2011-11-09 07:28:55 +000010724 SDValue A = Op.getOperand(0);
10725 SDValue B = Op.getOperand(1);
10726
Craig Topper5b209e82012-02-05 03:14:49 +000010727 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010728
Craig Topper5b209e82012-02-05 03:14:49 +000010729 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10730 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010731
Craig Topper5b209e82012-02-05 03:14:49 +000010732 // Bit cast to 32-bit vectors for MULUDQ
10733 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10734 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10735 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10736 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10737 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010738
Craig Topper5b209e82012-02-05 03:14:49 +000010739 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10740 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10741 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010742
Craig Topper5b209e82012-02-05 03:14:49 +000010743 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10744 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010745
Dale Johannesene4d209d2009-02-03 20:21:25 +000010746 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010747 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010748}
10749
Nadav Rotem43012222011-05-11 08:12:09 +000010750SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10751
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010752 EVT VT = Op.getValueType();
10753 DebugLoc dl = Op.getDebugLoc();
10754 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010755 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010756 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010757
Craig Topper1accb7e2012-01-10 06:54:16 +000010758 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010759 return SDValue();
10760
Nadav Rotem43012222011-05-11 08:12:09 +000010761 // Optimize shl/srl/sra with constant shift amount.
10762 if (isSplatVector(Amt.getNode())) {
10763 SDValue SclrAmt = Amt->getOperand(0);
10764 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10765 uint64_t ShiftAmt = C->getZExtValue();
10766
Craig Toppered2e13d2012-01-22 19:15:14 +000010767 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10768 (Subtarget->hasAVX2() &&
10769 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10770 if (Op.getOpcode() == ISD::SHL)
10771 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10772 DAG.getConstant(ShiftAmt, MVT::i32));
10773 if (Op.getOpcode() == ISD::SRL)
10774 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10775 DAG.getConstant(ShiftAmt, MVT::i32));
10776 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10777 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10778 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010779 }
10780
Craig Toppered2e13d2012-01-22 19:15:14 +000010781 if (VT == MVT::v16i8) {
10782 if (Op.getOpcode() == ISD::SHL) {
10783 // Make a large shift.
10784 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10785 DAG.getConstant(ShiftAmt, MVT::i32));
10786 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10787 // Zero out the rightmost bits.
10788 SmallVector<SDValue, 16> V(16,
10789 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10790 MVT::i8));
10791 return DAG.getNode(ISD::AND, dl, VT, SHL,
10792 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010793 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010794 if (Op.getOpcode() == ISD::SRL) {
10795 // Make a large shift.
10796 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10797 DAG.getConstant(ShiftAmt, MVT::i32));
10798 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10799 // Zero out the leftmost bits.
10800 SmallVector<SDValue, 16> V(16,
10801 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10802 MVT::i8));
10803 return DAG.getNode(ISD::AND, dl, VT, SRL,
10804 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10805 }
10806 if (Op.getOpcode() == ISD::SRA) {
10807 if (ShiftAmt == 7) {
10808 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010809 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010810 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010811 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010812
Craig Toppered2e13d2012-01-22 19:15:14 +000010813 // R s>> a === ((R u>> a) ^ m) - m
10814 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10815 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10816 MVT::i8));
10817 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10818 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10819 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10820 return Res;
10821 }
Craig Topper731dfd02012-04-23 03:42:40 +000010822 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010823 }
Craig Topper46154eb2011-11-11 07:39:23 +000010824
Craig Topper0d86d462011-11-20 00:12:05 +000010825 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10826 if (Op.getOpcode() == ISD::SHL) {
10827 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010828 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10829 DAG.getConstant(ShiftAmt, MVT::i32));
10830 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010831 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010832 SmallVector<SDValue, 32> V(32,
10833 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10834 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010835 return DAG.getNode(ISD::AND, dl, VT, SHL,
10836 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010837 }
Craig Topper0d86d462011-11-20 00:12:05 +000010838 if (Op.getOpcode() == ISD::SRL) {
10839 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010840 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10841 DAG.getConstant(ShiftAmt, MVT::i32));
10842 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010843 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010844 SmallVector<SDValue, 32> V(32,
10845 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10846 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010847 return DAG.getNode(ISD::AND, dl, VT, SRL,
10848 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10849 }
10850 if (Op.getOpcode() == ISD::SRA) {
10851 if (ShiftAmt == 7) {
10852 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010853 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010854 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010855 }
10856
10857 // R s>> a === ((R u>> a) ^ m) - m
10858 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10859 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10860 MVT::i8));
10861 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10862 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10863 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10864 return Res;
10865 }
Craig Topper731dfd02012-04-23 03:42:40 +000010866 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010867 }
Nadav Rotem43012222011-05-11 08:12:09 +000010868 }
10869 }
10870
10871 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010872 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010873 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10874 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010875
Chris Lattner7302d802012-02-06 21:56:39 +000010876 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10877 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010878 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10879 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010880 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010881 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010882
10883 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010884 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010885 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10886 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10887 }
Nadav Rotem43012222011-05-11 08:12:09 +000010888 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010889 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010890
Nate Begeman51409212010-07-28 00:21:48 +000010891 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010892 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10893 DAG.getConstant(5, MVT::i32));
10894 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010895
Lang Hames8b99c1e2011-12-17 01:08:46 +000010896 // Turn 'a' into a mask suitable for VSELECT
10897 SDValue VSelM = DAG.getConstant(0x80, VT);
10898 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010899 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010900
Lang Hames8b99c1e2011-12-17 01:08:46 +000010901 SDValue CM1 = DAG.getConstant(0x0f, VT);
10902 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010903
Lang Hames8b99c1e2011-12-17 01:08:46 +000010904 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10905 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010906 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10907 DAG.getConstant(4, MVT::i32), DAG);
10908 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010909 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10910
Nate Begeman51409212010-07-28 00:21:48 +000010911 // a += a
10912 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010913 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010914 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010915
Lang Hames8b99c1e2011-12-17 01:08:46 +000010916 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10917 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010918 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10919 DAG.getConstant(2, MVT::i32), DAG);
10920 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010921 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10922
Nate Begeman51409212010-07-28 00:21:48 +000010923 // a += a
10924 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010925 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010926 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010927
Lang Hames8b99c1e2011-12-17 01:08:46 +000010928 // return VSELECT(r, r+r, a);
10929 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010930 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010931 return R;
10932 }
Craig Topper46154eb2011-11-11 07:39:23 +000010933
10934 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010935 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010936 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010937 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10938 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10939
10940 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010941 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10942 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010943
10944 // Recreate the shift amount vectors
10945 SDValue Amt1, Amt2;
10946 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10947 // Constant shift amount
10948 SmallVector<SDValue, 4> Amt1Csts;
10949 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010950 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010951 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010952 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010953 Amt2Csts.push_back(Amt->getOperand(i));
10954
10955 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10956 &Amt1Csts[0], NumElems/2);
10957 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10958 &Amt2Csts[0], NumElems/2);
10959 } else {
10960 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010961 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10962 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010963 }
10964
10965 // Issue new vector shifts for the smaller types
10966 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10967 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10968
10969 // Concatenate the result back
10970 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10971 }
10972
Nate Begeman51409212010-07-28 00:21:48 +000010973 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010974}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010975
Craig Topper55b24052012-09-11 06:15:32 +000010976static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000010977 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10978 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010979 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10980 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010981 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010982 SDValue LHS = N->getOperand(0);
10983 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010984 unsigned BaseOp = 0;
10985 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010986 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010987 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010988 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010989 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010990 // A subtract of one will be selected as a INC. Note that INC doesn't
10991 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10993 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010994 BaseOp = X86ISD::INC;
10995 Cond = X86::COND_O;
10996 break;
10997 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010998 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010999 Cond = X86::COND_O;
11000 break;
11001 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011002 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011003 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011004 break;
11005 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011006 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11007 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011008 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11009 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011010 BaseOp = X86ISD::DEC;
11011 Cond = X86::COND_O;
11012 break;
11013 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011014 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011015 Cond = X86::COND_O;
11016 break;
11017 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011018 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011019 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011020 break;
11021 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011022 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011023 Cond = X86::COND_O;
11024 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011025 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11026 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11027 MVT::i32);
11028 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011029
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011030 SDValue SetCC =
11031 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11032 DAG.getConstant(X86::COND_O, MVT::i32),
11033 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011034
Dan Gohman6e5fda22011-07-22 18:45:15 +000011035 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011036 }
Bill Wendling74c37652008-12-09 22:08:41 +000011037 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011038
Bill Wendling61edeb52008-12-02 01:06:39 +000011039 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011040 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011041 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011042
Bill Wendling61edeb52008-12-02 01:06:39 +000011043 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011044 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11045 DAG.getConstant(Cond, MVT::i32),
11046 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011047
Dan Gohman6e5fda22011-07-22 18:45:15 +000011048 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011049}
11050
Chad Rosier30450e82011-12-22 22:35:21 +000011051SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11052 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011053 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011054 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11055 EVT VT = Op.getValueType();
11056
Craig Toppered2e13d2012-01-22 19:15:14 +000011057 if (!Subtarget->hasSSE2() || !VT.isVector())
11058 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011059
Craig Toppered2e13d2012-01-22 19:15:14 +000011060 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11061 ExtraVT.getScalarType().getSizeInBits();
11062 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11063
11064 switch (VT.getSimpleVT().SimpleTy) {
11065 default: return SDValue();
11066 case MVT::v8i32:
11067 case MVT::v16i16:
11068 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011069 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011070 if (!Subtarget->hasAVX2()) {
11071 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011072 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011073
Craig Toppered2e13d2012-01-22 19:15:14 +000011074 // Extract the LHS vectors
11075 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011076 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11077 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011078
Craig Toppered2e13d2012-01-22 19:15:14 +000011079 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11080 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011081
Craig Toppered2e13d2012-01-22 19:15:14 +000011082 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011083 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011084 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11085 ExtraNumElems/2);
11086 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011087
Craig Toppered2e13d2012-01-22 19:15:14 +000011088 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11089 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011090
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011091 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011092 }
11093 // fall through
11094 case MVT::v4i32:
11095 case MVT::v8i16: {
11096 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11097 Op.getOperand(0), ShAmt, DAG);
11098 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011099 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011100 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011101}
11102
11103
Craig Topper55b24052012-09-11 06:15:32 +000011104static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11105 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011106 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011107
Eric Christopher77ed1352011-07-08 00:04:56 +000011108 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11109 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011110 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011111 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011112 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011113 SDValue Ops[] = {
11114 DAG.getRegister(X86::ESP, MVT::i32), // Base
11115 DAG.getTargetConstant(1, MVT::i8), // Scale
11116 DAG.getRegister(0, MVT::i32), // Index
11117 DAG.getTargetConstant(0, MVT::i32), // Disp
11118 DAG.getRegister(0, MVT::i32), // Segment.
11119 Zero,
11120 Chain
11121 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011122 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011123 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11124 array_lengthof(Ops));
11125 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011126 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011127
Eric Christopher9a9d2752010-07-22 02:48:34 +000011128 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011129 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011130 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011131
Chris Lattner132929a2010-08-14 17:26:09 +000011132 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11133 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11134 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11135 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011136
Chris Lattner132929a2010-08-14 17:26:09 +000011137 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11138 if (!Op1 && !Op2 && !Op3 && Op4)
11139 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011140
Chris Lattner132929a2010-08-14 17:26:09 +000011141 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11142 if (Op1 && !Op2 && !Op3 && !Op4)
11143 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011144
11145 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011146 // (MFENCE)>;
11147 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011148}
11149
Craig Topper55b24052012-09-11 06:15:32 +000011150static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11151 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011152 DebugLoc dl = Op.getDebugLoc();
11153 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11154 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11155 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11156 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11157
11158 // The only fence that needs an instruction is a sequentially-consistent
11159 // cross-thread fence.
11160 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11161 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11162 // no-sse2). There isn't any reason to disable it if the target processor
11163 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011164 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011165 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11166
11167 SDValue Chain = Op.getOperand(0);
11168 SDValue Zero = DAG.getConstant(0, MVT::i32);
11169 SDValue Ops[] = {
11170 DAG.getRegister(X86::ESP, MVT::i32), // Base
11171 DAG.getTargetConstant(1, MVT::i8), // Scale
11172 DAG.getRegister(0, MVT::i32), // Index
11173 DAG.getTargetConstant(0, MVT::i32), // Disp
11174 DAG.getRegister(0, MVT::i32), // Segment.
11175 Zero,
11176 Chain
11177 };
11178 SDNode *Res =
11179 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11180 array_lengthof(Ops));
11181 return SDValue(Res, 0);
11182 }
11183
11184 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11185 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11186}
11187
11188
Craig Topper55b24052012-09-11 06:15:32 +000011189static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11190 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011191 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011192 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011193 unsigned Reg = 0;
11194 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011195 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011196 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011197 case MVT::i8: Reg = X86::AL; size = 1; break;
11198 case MVT::i16: Reg = X86::AX; size = 2; break;
11199 case MVT::i32: Reg = X86::EAX; size = 4; break;
11200 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011201 assert(Subtarget->is64Bit() && "Node not type legal!");
11202 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011203 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011204 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011205 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011206 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011207 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011208 Op.getOperand(1),
11209 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011210 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011211 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011212 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011213 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11214 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11215 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011216 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011217 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011218 return cpOut;
11219}
11220
Craig Topper55b24052012-09-11 06:15:32 +000011221static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11222 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011223 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011224 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011225 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011226 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011227 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011228 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11229 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011230 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011231 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11232 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011233 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011234 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011235 rdx.getValue(1)
11236 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011237 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011238}
11239
Craig Topper55b24052012-09-11 06:15:32 +000011240SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011241 EVT SrcVT = Op.getOperand(0).getValueType();
11242 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011243 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011244 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011245 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011246 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011247 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011248 // i64 <=> MMX conversions are Legal.
11249 if (SrcVT==MVT::i64 && DstVT.isVector())
11250 return Op;
11251 if (DstVT==MVT::i64 && SrcVT.isVector())
11252 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011253 // MMX <=> MMX conversions are Legal.
11254 if (SrcVT.isVector() && DstVT.isVector())
11255 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011256 // All other conversions need to be expanded.
11257 return SDValue();
11258}
Chris Lattner5b856542010-12-20 00:59:46 +000011259
Craig Topper55b24052012-09-11 06:15:32 +000011260static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011261 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011262 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011263 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011264 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011265 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011266 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011267 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011268 Node->getOperand(0),
11269 Node->getOperand(1), negOp,
11270 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011271 cast<AtomicSDNode>(Node)->getAlignment(),
11272 cast<AtomicSDNode>(Node)->getOrdering(),
11273 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011274}
11275
Eli Friedman327236c2011-08-24 20:50:09 +000011276static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11277 SDNode *Node = Op.getNode();
11278 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011279 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011280
11281 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011282 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11283 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11284 // (The only way to get a 16-byte store is cmpxchg16b)
11285 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11286 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11287 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011288 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11289 cast<AtomicSDNode>(Node)->getMemoryVT(),
11290 Node->getOperand(0),
11291 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011292 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011293 cast<AtomicSDNode>(Node)->getOrdering(),
11294 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011295 return Swap.getValue(1);
11296 }
11297 // Other atomic stores have a simple pattern.
11298 return Op;
11299}
11300
Chris Lattner5b856542010-12-20 00:59:46 +000011301static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11302 EVT VT = Op.getNode()->getValueType(0);
11303
11304 // Let legalize expand this if it isn't a legal type yet.
11305 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11306 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011307
Chris Lattner5b856542010-12-20 00:59:46 +000011308 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011309
Chris Lattner5b856542010-12-20 00:59:46 +000011310 unsigned Opc;
11311 bool ExtraOp = false;
11312 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011313 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011314 case ISD::ADDC: Opc = X86ISD::ADD; break;
11315 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11316 case ISD::SUBC: Opc = X86ISD::SUB; break;
11317 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11318 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011319
Chris Lattner5b856542010-12-20 00:59:46 +000011320 if (!ExtraOp)
11321 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11322 Op.getOperand(1));
11323 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11324 Op.getOperand(1), Op.getOperand(2));
11325}
11326
Evan Cheng0db9fe62006-04-25 20:13:52 +000011327/// LowerOperation - Provide custom lowering hooks for some operations.
11328///
Dan Gohmand858e902010-04-17 15:26:15 +000011329SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011330 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011331 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011332 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011333 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11334 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11335 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011336 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011337 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011338 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011339 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011340 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11341 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11342 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011343 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11344 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011345 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11346 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11347 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011348 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011349 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011350 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011351 case ISD::SHL_PARTS:
11352 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011353 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011354 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011355 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011356 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011357 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011358 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011359 case ISD::FABS: return LowerFABS(Op, DAG);
11360 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011361 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011362 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011363 case ISD::SETCC: return LowerSETCC(Op, DAG);
11364 case ISD::SELECT: return LowerSELECT(Op, DAG);
11365 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011366 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011367 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011368 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011369 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011370 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011371 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011372 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11373 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011374 case ISD::FRAME_TO_ARGS_OFFSET:
11375 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011376 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011377 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011378 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11379 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011380 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011381 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011382 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011383 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011384 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011385 case ISD::SRA:
11386 case ISD::SRL:
11387 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011388 case ISD::SADDO:
11389 case ISD::UADDO:
11390 case ISD::SSUBO:
11391 case ISD::USUBO:
11392 case ISD::SMULO:
11393 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011394 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011395 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011396 case ISD::ADDC:
11397 case ISD::ADDE:
11398 case ISD::SUBC:
11399 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011400 case ISD::ADD: return LowerADD(Op, DAG);
11401 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011402 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011403}
11404
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011405static void ReplaceATOMIC_LOAD(SDNode *Node,
11406 SmallVectorImpl<SDValue> &Results,
11407 SelectionDAG &DAG) {
11408 DebugLoc dl = Node->getDebugLoc();
11409 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11410
11411 // Convert wide load -> cmpxchg8b/cmpxchg16b
11412 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11413 // (The only way to get a 16-byte load is cmpxchg16b)
11414 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011415 SDValue Zero = DAG.getConstant(0, VT);
11416 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011417 Node->getOperand(0),
11418 Node->getOperand(1), Zero, Zero,
11419 cast<AtomicSDNode>(Node)->getMemOperand(),
11420 cast<AtomicSDNode>(Node)->getOrdering(),
11421 cast<AtomicSDNode>(Node)->getSynchScope());
11422 Results.push_back(Swap.getValue(0));
11423 Results.push_back(Swap.getValue(1));
11424}
11425
Craig Topperc0878702012-08-17 06:55:11 +000011426static void
Duncan Sands1607f052008-12-01 11:39:25 +000011427ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011428 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011429 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011430 assert (Node->getValueType(0) == MVT::i64 &&
11431 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011432
11433 SDValue Chain = Node->getOperand(0);
11434 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011435 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011436 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011437 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011438 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011439 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011440 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011441 SDValue Result =
11442 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11443 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011444 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011445 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011446 Results.push_back(Result.getValue(2));
11447}
11448
Duncan Sands126d9072008-07-04 11:47:58 +000011449/// ReplaceNodeResults - Replace a node with an illegal result type
11450/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011451void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11452 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011453 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011454 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011455 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011456 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011457 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011458 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011459 case ISD::ADDC:
11460 case ISD::ADDE:
11461 case ISD::SUBC:
11462 case ISD::SUBE:
11463 // We don't want to expand or promote these.
11464 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011465 case ISD::FP_TO_SINT:
11466 case ISD::FP_TO_UINT: {
11467 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11468
11469 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11470 return;
11471
Eli Friedman948e95a2009-05-23 09:59:16 +000011472 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011473 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011474 SDValue FIST = Vals.first, StackSlot = Vals.second;
11475 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011476 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011477 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011478 if (StackSlot.getNode() != 0)
11479 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11480 MachinePointerInfo(),
11481 false, false, false, 0));
11482 else
11483 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011484 }
11485 return;
11486 }
Michael Liao44c2d612012-10-10 16:53:28 +000011487 case ISD::FP_ROUND: {
11488 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
11489 Results.push_back(V);
11490 return;
11491 }
Duncan Sands1607f052008-12-01 11:39:25 +000011492 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011493 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011494 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011495 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011497 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011499 eax.getValue(2));
11500 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11501 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011503 Results.push_back(edx.getValue(1));
11504 return;
11505 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011506 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011507 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011508 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011509 bool Regs64bit = T == MVT::i128;
11510 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011511 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011512 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11513 DAG.getConstant(0, HalfT));
11514 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11515 DAG.getConstant(1, HalfT));
11516 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11517 Regs64bit ? X86::RAX : X86::EAX,
11518 cpInL, SDValue());
11519 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11520 Regs64bit ? X86::RDX : X86::EDX,
11521 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011522 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011523 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11524 DAG.getConstant(0, HalfT));
11525 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11526 DAG.getConstant(1, HalfT));
11527 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11528 Regs64bit ? X86::RBX : X86::EBX,
11529 swapInL, cpInH.getValue(1));
11530 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011531 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011532 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011533 SDValue Ops[] = { swapInH.getValue(0),
11534 N->getOperand(1),
11535 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011536 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011537 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011538 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11539 X86ISD::LCMPXCHG8_DAG;
11540 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011541 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011542 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11543 Regs64bit ? X86::RAX : X86::EAX,
11544 HalfT, Result.getValue(1));
11545 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11546 Regs64bit ? X86::RDX : X86::EDX,
11547 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011548 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011549 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011550 Results.push_back(cpOutH.getValue(1));
11551 return;
11552 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011553 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011554 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011555 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011556 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011557 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011558 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000011559 case ISD::ATOMIC_LOAD_MAX:
11560 case ISD::ATOMIC_LOAD_MIN:
11561 case ISD::ATOMIC_LOAD_UMAX:
11562 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000011563 case ISD::ATOMIC_SWAP: {
11564 unsigned Opc;
11565 switch (N->getOpcode()) {
11566 default: llvm_unreachable("Unexpected opcode");
11567 case ISD::ATOMIC_LOAD_ADD:
11568 Opc = X86ISD::ATOMADD64_DAG;
11569 break;
11570 case ISD::ATOMIC_LOAD_AND:
11571 Opc = X86ISD::ATOMAND64_DAG;
11572 break;
11573 case ISD::ATOMIC_LOAD_NAND:
11574 Opc = X86ISD::ATOMNAND64_DAG;
11575 break;
11576 case ISD::ATOMIC_LOAD_OR:
11577 Opc = X86ISD::ATOMOR64_DAG;
11578 break;
11579 case ISD::ATOMIC_LOAD_SUB:
11580 Opc = X86ISD::ATOMSUB64_DAG;
11581 break;
11582 case ISD::ATOMIC_LOAD_XOR:
11583 Opc = X86ISD::ATOMXOR64_DAG;
11584 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011585 case ISD::ATOMIC_LOAD_MAX:
11586 Opc = X86ISD::ATOMMAX64_DAG;
11587 break;
11588 case ISD::ATOMIC_LOAD_MIN:
11589 Opc = X86ISD::ATOMMIN64_DAG;
11590 break;
11591 case ISD::ATOMIC_LOAD_UMAX:
11592 Opc = X86ISD::ATOMUMAX64_DAG;
11593 break;
11594 case ISD::ATOMIC_LOAD_UMIN:
11595 Opc = X86ISD::ATOMUMIN64_DAG;
11596 break;
Craig Topperc0878702012-08-17 06:55:11 +000011597 case ISD::ATOMIC_SWAP:
11598 Opc = X86ISD::ATOMSWAP64_DAG;
11599 break;
11600 }
11601 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011602 return;
Craig Topperc0878702012-08-17 06:55:11 +000011603 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011604 case ISD::ATOMIC_LOAD:
11605 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011606 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011607}
11608
Evan Cheng72261582005-12-20 06:22:03 +000011609const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11610 switch (Opcode) {
11611 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011612 case X86ISD::BSF: return "X86ISD::BSF";
11613 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011614 case X86ISD::SHLD: return "X86ISD::SHLD";
11615 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011616 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011617 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011618 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011619 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011620 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011621 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011622 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11623 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11624 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011625 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011626 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011627 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011628 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011629 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011630 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011631 case X86ISD::COMI: return "X86ISD::COMI";
11632 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011633 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011634 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011635 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11636 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011637 case X86ISD::CMOV: return "X86ISD::CMOV";
11638 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011639 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011640 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11641 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011642 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011643 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011644 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011645 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011646 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011647 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11648 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011649 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011650 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011651 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011652 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011653 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011654 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11655 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11656 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011657 case X86ISD::HADD: return "X86ISD::HADD";
11658 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011659 case X86ISD::FHADD: return "X86ISD::FHADD";
11660 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011661 case X86ISD::FMAX: return "X86ISD::FMAX";
11662 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011663 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11664 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011665 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11666 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011667 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011668 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011669 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011670 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011671 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011672 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011673 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011674 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11675 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011676 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11677 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11678 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11679 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11680 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11681 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011682 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011683 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011684 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011685 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000011686 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000011687 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11688 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011689 case X86ISD::VSHL: return "X86ISD::VSHL";
11690 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011691 case X86ISD::VSRA: return "X86ISD::VSRA";
11692 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11693 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11694 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011695 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011696 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11697 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011698 case X86ISD::ADD: return "X86ISD::ADD";
11699 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011700 case X86ISD::ADC: return "X86ISD::ADC";
11701 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011702 case X86ISD::SMUL: return "X86ISD::SMUL";
11703 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011704 case X86ISD::INC: return "X86ISD::INC";
11705 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011706 case X86ISD::OR: return "X86ISD::OR";
11707 case X86ISD::XOR: return "X86ISD::XOR";
11708 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011709 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011710 case X86ISD::BLSI: return "X86ISD::BLSI";
11711 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11712 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011713 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011714 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011715 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011716 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11717 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11718 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011719 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011720 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011721 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011722 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011723 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011724 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11725 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011726 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11727 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11728 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011729 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11730 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011731 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11732 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011733 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011734 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011735 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011736 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11737 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011738 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011739 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011740 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011741 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011742 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011743 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011744 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011745 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011746 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011747 case X86ISD::FMADD: return "X86ISD::FMADD";
11748 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11749 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11750 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11751 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11752 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011753 }
11754}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011755
Chris Lattnerc9addb72007-03-30 23:15:24 +000011756// isLegalAddressingMode - Return true if the addressing mode represented
11757// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011758bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011759 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011760 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011761 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011762 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011763
Chris Lattnerc9addb72007-03-30 23:15:24 +000011764 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011765 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011766 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011767
Chris Lattnerc9addb72007-03-30 23:15:24 +000011768 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011769 unsigned GVFlags =
11770 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011771
Chris Lattnerdfed4132009-07-10 07:38:24 +000011772 // If a reference to this global requires an extra load, we can't fold it.
11773 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011774 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011775
Chris Lattnerdfed4132009-07-10 07:38:24 +000011776 // If BaseGV requires a register for the PIC base, we cannot also have a
11777 // BaseReg specified.
11778 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011779 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011780
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011781 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011782 if ((M != CodeModel::Small || R != Reloc::Static) &&
11783 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011784 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011785 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011786
Chris Lattnerc9addb72007-03-30 23:15:24 +000011787 switch (AM.Scale) {
11788 case 0:
11789 case 1:
11790 case 2:
11791 case 4:
11792 case 8:
11793 // These scales always work.
11794 break;
11795 case 3:
11796 case 5:
11797 case 9:
11798 // These scales are formed with basereg+scalereg. Only accept if there is
11799 // no basereg yet.
11800 if (AM.HasBaseReg)
11801 return false;
11802 break;
11803 default: // Other stuff never works.
11804 return false;
11805 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011806
Chris Lattnerc9addb72007-03-30 23:15:24 +000011807 return true;
11808}
11809
11810
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011811bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011812 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011813 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011814 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11815 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011816 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011817 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011818 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011819}
11820
Evan Cheng70e10d32012-07-17 06:53:39 +000011821bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11822 return Imm == (int32_t)Imm;
11823}
11824
11825bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011826 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011827 return Imm == (int32_t)Imm;
11828}
11829
Owen Andersone50ed302009-08-10 22:56:29 +000011830bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011831 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011832 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011833 unsigned NumBits1 = VT1.getSizeInBits();
11834 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011835 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011836 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011837 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011838}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011839
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011840bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011841 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011842 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011843}
11844
Owen Andersone50ed302009-08-10 22:56:29 +000011845bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011846 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011847 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011848}
11849
Owen Andersone50ed302009-08-10 22:56:29 +000011850bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011851 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011852 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011853}
11854
Evan Cheng60c07e12006-07-05 22:17:51 +000011855/// isShuffleMaskLegal - Targets can use this to indicate that they only
11856/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11857/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11858/// are assumed to be legal.
11859bool
Eric Christopherfd179292009-08-27 18:07:15 +000011860X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011861 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011862 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011863 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011864 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011865
Nate Begemana09008b2009-10-19 02:17:23 +000011866 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011867 return (VT.getVectorNumElements() == 2 ||
11868 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11869 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011870 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011871 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011872 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11873 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011874 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011875 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11876 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011877 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11878 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011879}
11880
Dan Gohman7d8143f2008-04-09 20:09:42 +000011881bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011882X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011883 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011884 unsigned NumElts = VT.getVectorNumElements();
11885 // FIXME: This collection of masks seems suspect.
11886 if (NumElts == 2)
11887 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011888 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011889 return (isMOVLMask(Mask, VT) ||
11890 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011891 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11892 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011893 }
11894 return false;
11895}
11896
11897//===----------------------------------------------------------------------===//
11898// X86 Scheduler Hooks
11899//===----------------------------------------------------------------------===//
11900
Mon P Wang63307c32008-05-05 19:05:59 +000011901// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000011902
Michael Liaob118a072012-09-20 03:06:15 +000011903// Get CMPXCHG opcode for the specified data type.
11904static unsigned getCmpXChgOpcode(EVT VT) {
11905 switch (VT.getSimpleVT().SimpleTy) {
11906 case MVT::i8: return X86::LCMPXCHG8;
11907 case MVT::i16: return X86::LCMPXCHG16;
11908 case MVT::i32: return X86::LCMPXCHG32;
11909 case MVT::i64: return X86::LCMPXCHG64;
11910 default:
11911 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000011912 }
Michael Liaob118a072012-09-20 03:06:15 +000011913 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000011914}
11915
Michael Liaob118a072012-09-20 03:06:15 +000011916// Get LOAD opcode for the specified data type.
11917static unsigned getLoadOpcode(EVT VT) {
11918 switch (VT.getSimpleVT().SimpleTy) {
11919 case MVT::i8: return X86::MOV8rm;
11920 case MVT::i16: return X86::MOV16rm;
11921 case MVT::i32: return X86::MOV32rm;
11922 case MVT::i64: return X86::MOV64rm;
11923 default:
11924 break;
11925 }
11926 llvm_unreachable("Invalid operand size!");
11927}
11928
11929// Get opcode of the non-atomic one from the specified atomic instruction.
11930static unsigned getNonAtomicOpcode(unsigned Opc) {
11931 switch (Opc) {
11932 case X86::ATOMAND8: return X86::AND8rr;
11933 case X86::ATOMAND16: return X86::AND16rr;
11934 case X86::ATOMAND32: return X86::AND32rr;
11935 case X86::ATOMAND64: return X86::AND64rr;
11936 case X86::ATOMOR8: return X86::OR8rr;
11937 case X86::ATOMOR16: return X86::OR16rr;
11938 case X86::ATOMOR32: return X86::OR32rr;
11939 case X86::ATOMOR64: return X86::OR64rr;
11940 case X86::ATOMXOR8: return X86::XOR8rr;
11941 case X86::ATOMXOR16: return X86::XOR16rr;
11942 case X86::ATOMXOR32: return X86::XOR32rr;
11943 case X86::ATOMXOR64: return X86::XOR64rr;
11944 }
11945 llvm_unreachable("Unhandled atomic-load-op opcode!");
11946}
11947
11948// Get opcode of the non-atomic one from the specified atomic instruction with
11949// extra opcode.
11950static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
11951 unsigned &ExtraOpc) {
11952 switch (Opc) {
11953 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
11954 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
11955 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
11956 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011957 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011958 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
11959 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
11960 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011961 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011962 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
11963 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
11964 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011965 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011966 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
11967 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
11968 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011969 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011970 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
11971 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
11972 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
11973 }
11974 llvm_unreachable("Unhandled atomic-load-op opcode!");
11975}
11976
11977// Get opcode of the non-atomic one from the specified atomic instruction for
11978// 64-bit data type on 32-bit target.
11979static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
11980 switch (Opc) {
11981 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
11982 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
11983 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
11984 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
11985 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
11986 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000011987 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
11988 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
11989 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
11990 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000011991 }
11992 llvm_unreachable("Unhandled atomic-load-op opcode!");
11993}
11994
11995// Get opcode of the non-atomic one from the specified atomic instruction for
11996// 64-bit data type on 32-bit target with extra opcode.
11997static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
11998 unsigned &HiOpc,
11999 unsigned &ExtraOpc) {
12000 switch (Opc) {
12001 case X86::ATOMNAND6432:
12002 ExtraOpc = X86::NOT32r;
12003 HiOpc = X86::AND32rr;
12004 return X86::AND32rr;
12005 }
12006 llvm_unreachable("Unhandled atomic-load-op opcode!");
12007}
12008
12009// Get pseudo CMOV opcode from the specified data type.
12010static unsigned getPseudoCMOVOpc(EVT VT) {
12011 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012012 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012013 case MVT::i16: return X86::CMOV_GR16;
12014 case MVT::i32: return X86::CMOV_GR32;
12015 default:
12016 break;
12017 }
12018 llvm_unreachable("Unknown CMOV opcode!");
12019}
12020
12021// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12022// They will be translated into a spin-loop or compare-exchange loop from
12023//
12024// ...
12025// dst = atomic-fetch-op MI.addr, MI.val
12026// ...
12027//
12028// to
12029//
12030// ...
12031// EAX = LOAD MI.addr
12032// loop:
12033// t1 = OP MI.val, EAX
12034// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12035// JNE loop
12036// sink:
12037// dst = EAX
12038// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012039MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012040X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12041 MachineBasicBlock *MBB) const {
12042 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12043 DebugLoc DL = MI->getDebugLoc();
12044
12045 MachineFunction *MF = MBB->getParent();
12046 MachineRegisterInfo &MRI = MF->getRegInfo();
12047
12048 const BasicBlock *BB = MBB->getBasicBlock();
12049 MachineFunction::iterator I = MBB;
12050 ++I;
12051
12052 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12053 "Unexpected number of operands");
12054
12055 assert(MI->hasOneMemOperand() &&
12056 "Expected atomic-load-op to have one memoperand");
12057
12058 // Memory Reference
12059 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12060 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12061
12062 unsigned DstReg, SrcReg;
12063 unsigned MemOpndSlot;
12064
12065 unsigned CurOp = 0;
12066
12067 DstReg = MI->getOperand(CurOp++).getReg();
12068 MemOpndSlot = CurOp;
12069 CurOp += X86::AddrNumOperands;
12070 SrcReg = MI->getOperand(CurOp++).getReg();
12071
12072 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012073 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012074 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12075
12076 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12077 unsigned LOADOpc = getLoadOpcode(VT);
12078
12079 // For the atomic load-arith operator, we generate
12080 //
12081 // thisMBB:
12082 // EAX = LOAD [MI.addr]
12083 // mainMBB:
12084 // t1 = OP MI.val, EAX
12085 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12086 // JNE mainMBB
12087 // sinkMBB:
12088
12089 MachineBasicBlock *thisMBB = MBB;
12090 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12091 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12092 MF->insert(I, mainMBB);
12093 MF->insert(I, sinkMBB);
12094
12095 MachineInstrBuilder MIB;
12096
12097 // Transfer the remainder of BB and its successor edges to sinkMBB.
12098 sinkMBB->splice(sinkMBB->begin(), MBB,
12099 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12100 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12101
12102 // thisMBB:
12103 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12104 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12105 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12106 MIB.setMemRefs(MMOBegin, MMOEnd);
12107
12108 thisMBB->addSuccessor(mainMBB);
12109
12110 // mainMBB:
12111 MachineBasicBlock *origMainMBB = mainMBB;
12112 mainMBB->addLiveIn(AccPhyReg);
12113
12114 // Copy AccPhyReg as it is used more than once.
12115 unsigned AccReg = MRI.createVirtualRegister(RC);
12116 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12117 .addReg(AccPhyReg);
12118
12119 unsigned t1 = MRI.createVirtualRegister(RC);
12120 unsigned Opc = MI->getOpcode();
12121 switch (Opc) {
12122 default:
12123 llvm_unreachable("Unhandled atomic-load-op opcode!");
12124 case X86::ATOMAND8:
12125 case X86::ATOMAND16:
12126 case X86::ATOMAND32:
12127 case X86::ATOMAND64:
12128 case X86::ATOMOR8:
12129 case X86::ATOMOR16:
12130 case X86::ATOMOR32:
12131 case X86::ATOMOR64:
12132 case X86::ATOMXOR8:
12133 case X86::ATOMXOR16:
12134 case X86::ATOMXOR32:
12135 case X86::ATOMXOR64: {
12136 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12137 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12138 .addReg(AccReg);
12139 break;
12140 }
12141 case X86::ATOMNAND8:
12142 case X86::ATOMNAND16:
12143 case X86::ATOMNAND32:
12144 case X86::ATOMNAND64: {
12145 unsigned t2 = MRI.createVirtualRegister(RC);
12146 unsigned NOTOpc;
12147 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12148 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12149 .addReg(AccReg);
12150 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12151 break;
12152 }
Michael Liao08382492012-09-21 03:00:17 +000012153 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012154 case X86::ATOMMAX16:
12155 case X86::ATOMMAX32:
12156 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012157 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012158 case X86::ATOMMIN16:
12159 case X86::ATOMMIN32:
12160 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012161 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012162 case X86::ATOMUMAX16:
12163 case X86::ATOMUMAX32:
12164 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012165 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012166 case X86::ATOMUMIN16:
12167 case X86::ATOMUMIN32:
12168 case X86::ATOMUMIN64: {
12169 unsigned CMPOpc;
12170 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12171
12172 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12173 .addReg(SrcReg)
12174 .addReg(AccReg);
12175
12176 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012177 if (VT != MVT::i8) {
12178 // Native support
12179 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12180 .addReg(SrcReg)
12181 .addReg(AccReg);
12182 } else {
12183 // Promote i8 to i32 to use CMOV32
12184 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12185 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12186 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12187 unsigned t2 = MRI.createVirtualRegister(RC32);
12188
12189 unsigned Undef = MRI.createVirtualRegister(RC32);
12190 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12191
12192 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12193 .addReg(Undef)
12194 .addReg(SrcReg)
12195 .addImm(X86::sub_8bit);
12196 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12197 .addReg(Undef)
12198 .addReg(AccReg)
12199 .addImm(X86::sub_8bit);
12200
12201 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12202 .addReg(SrcReg32)
12203 .addReg(AccReg32);
12204
12205 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12206 .addReg(t2, 0, X86::sub_8bit);
12207 }
Michael Liaob118a072012-09-20 03:06:15 +000012208 } else {
12209 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012210 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012211 "Invalid atomic-load-op transformation!");
12212 unsigned SelOpc = getPseudoCMOVOpc(VT);
12213 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12214 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12215 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12216 .addReg(SrcReg).addReg(AccReg)
12217 .addImm(CC);
12218 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12219 }
12220 break;
12221 }
12222 }
12223
12224 // Copy AccPhyReg back from virtual register.
12225 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12226 .addReg(AccReg);
12227
12228 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12229 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12230 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12231 MIB.addReg(t1);
12232 MIB.setMemRefs(MMOBegin, MMOEnd);
12233
12234 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12235
12236 mainMBB->addSuccessor(origMainMBB);
12237 mainMBB->addSuccessor(sinkMBB);
12238
12239 // sinkMBB:
12240 sinkMBB->addLiveIn(AccPhyReg);
12241
12242 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12243 TII->get(TargetOpcode::COPY), DstReg)
12244 .addReg(AccPhyReg);
12245
12246 MI->eraseFromParent();
12247 return sinkMBB;
12248}
12249
12250// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12251// instructions. They will be translated into a spin-loop or compare-exchange
12252// loop from
12253//
12254// ...
12255// dst = atomic-fetch-op MI.addr, MI.val
12256// ...
12257//
12258// to
12259//
12260// ...
12261// EAX = LOAD [MI.addr + 0]
12262// EDX = LOAD [MI.addr + 4]
12263// loop:
12264// EBX = OP MI.val.lo, EAX
12265// ECX = OP MI.val.hi, EDX
12266// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12267// JNE loop
12268// sink:
12269// dst = EDX:EAX
12270// ...
12271MachineBasicBlock *
12272X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12273 MachineBasicBlock *MBB) const {
12274 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12275 DebugLoc DL = MI->getDebugLoc();
12276
12277 MachineFunction *MF = MBB->getParent();
12278 MachineRegisterInfo &MRI = MF->getRegInfo();
12279
12280 const BasicBlock *BB = MBB->getBasicBlock();
12281 MachineFunction::iterator I = MBB;
12282 ++I;
12283
12284 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12285 "Unexpected number of operands");
12286
12287 assert(MI->hasOneMemOperand() &&
12288 "Expected atomic-load-op32 to have one memoperand");
12289
12290 // Memory Reference
12291 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12292 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12293
12294 unsigned DstLoReg, DstHiReg;
12295 unsigned SrcLoReg, SrcHiReg;
12296 unsigned MemOpndSlot;
12297
12298 unsigned CurOp = 0;
12299
12300 DstLoReg = MI->getOperand(CurOp++).getReg();
12301 DstHiReg = MI->getOperand(CurOp++).getReg();
12302 MemOpndSlot = CurOp;
12303 CurOp += X86::AddrNumOperands;
12304 SrcLoReg = MI->getOperand(CurOp++).getReg();
12305 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012306
Craig Topperc9099502012-04-20 06:31:50 +000012307 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012308 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012309
Michael Liaob118a072012-09-20 03:06:15 +000012310 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12311 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012312
Michael Liaob118a072012-09-20 03:06:15 +000012313 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012314 //
Michael Liaob118a072012-09-20 03:06:15 +000012315 // thisMBB:
12316 // EAX = LOAD [MI.addr + 0]
12317 // EDX = LOAD [MI.addr + 4]
12318 // mainMBB:
12319 // EBX = OP MI.vallo, EAX
12320 // ECX = OP MI.valhi, EDX
12321 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12322 // JNE mainMBB
12323 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012324
Mon P Wang63307c32008-05-05 19:05:59 +000012325 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012326 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12327 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12328 MF->insert(I, mainMBB);
12329 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012330
Michael Liaob118a072012-09-20 03:06:15 +000012331 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012332
Michael Liaob118a072012-09-20 03:06:15 +000012333 // Transfer the remainder of BB and its successor edges to sinkMBB.
12334 sinkMBB->splice(sinkMBB->begin(), MBB,
12335 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12336 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012337
Michael Liaob118a072012-09-20 03:06:15 +000012338 // thisMBB:
12339 // Lo
12340 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12341 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12342 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12343 MIB.setMemRefs(MMOBegin, MMOEnd);
12344 // Hi
12345 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12346 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000012347 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000012348 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000012349 else
Michael Liaob118a072012-09-20 03:06:15 +000012350 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12351 }
12352 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012353
Michael Liaob118a072012-09-20 03:06:15 +000012354 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012355
Michael Liaob118a072012-09-20 03:06:15 +000012356 // mainMBB:
12357 MachineBasicBlock *origMainMBB = mainMBB;
12358 mainMBB->addLiveIn(X86::EAX);
12359 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012360
Michael Liaob118a072012-09-20 03:06:15 +000012361 // Copy EDX:EAX as they are used more than once.
12362 unsigned LoReg = MRI.createVirtualRegister(RC);
12363 unsigned HiReg = MRI.createVirtualRegister(RC);
12364 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12365 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012366
Michael Liaob118a072012-09-20 03:06:15 +000012367 unsigned t1L = MRI.createVirtualRegister(RC);
12368 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012369
Michael Liaob118a072012-09-20 03:06:15 +000012370 unsigned Opc = MI->getOpcode();
12371 switch (Opc) {
12372 default:
12373 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12374 case X86::ATOMAND6432:
12375 case X86::ATOMOR6432:
12376 case X86::ATOMXOR6432:
12377 case X86::ATOMADD6432:
12378 case X86::ATOMSUB6432: {
12379 unsigned HiOpc;
12380 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12381 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12382 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12383 break;
12384 }
12385 case X86::ATOMNAND6432: {
12386 unsigned HiOpc, NOTOpc;
12387 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12388 unsigned t2L = MRI.createVirtualRegister(RC);
12389 unsigned t2H = MRI.createVirtualRegister(RC);
12390 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12391 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12392 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12393 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12394 break;
12395 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000012396 case X86::ATOMMAX6432:
12397 case X86::ATOMMIN6432:
12398 case X86::ATOMUMAX6432:
12399 case X86::ATOMUMIN6432: {
12400 unsigned HiOpc;
12401 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12402 unsigned cL = MRI.createVirtualRegister(RC8);
12403 unsigned cH = MRI.createVirtualRegister(RC8);
12404 unsigned cL32 = MRI.createVirtualRegister(RC);
12405 unsigned cH32 = MRI.createVirtualRegister(RC);
12406 unsigned cc = MRI.createVirtualRegister(RC);
12407 // cl := cmp src_lo, lo
12408 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12409 .addReg(SrcLoReg).addReg(LoReg);
12410 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
12411 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
12412 // ch := cmp src_hi, hi
12413 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
12414 .addReg(SrcHiReg).addReg(HiReg);
12415 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
12416 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
12417 // cc := if (src_hi == hi) ? cl : ch;
12418 if (Subtarget->hasCMov()) {
12419 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
12420 .addReg(cH32).addReg(cL32);
12421 } else {
12422 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
12423 .addReg(cH32).addReg(cL32)
12424 .addImm(X86::COND_E);
12425 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12426 }
12427 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
12428 if (Subtarget->hasCMov()) {
12429 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
12430 .addReg(SrcLoReg).addReg(LoReg);
12431 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
12432 .addReg(SrcHiReg).addReg(HiReg);
12433 } else {
12434 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
12435 .addReg(SrcLoReg).addReg(LoReg)
12436 .addImm(X86::COND_NE);
12437 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12438 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
12439 .addReg(SrcHiReg).addReg(HiReg)
12440 .addImm(X86::COND_NE);
12441 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12442 }
12443 break;
12444 }
Michael Liaob118a072012-09-20 03:06:15 +000012445 case X86::ATOMSWAP6432: {
12446 unsigned HiOpc;
12447 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12448 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12449 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12450 break;
12451 }
12452 }
Mon P Wang63307c32008-05-05 19:05:59 +000012453
Michael Liaob118a072012-09-20 03:06:15 +000012454 // Copy EDX:EAX back from HiReg:LoReg
12455 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12456 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12457 // Copy ECX:EBX from t1H:t1L
12458 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12459 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012460
Michael Liaob118a072012-09-20 03:06:15 +000012461 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12462 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12463 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12464 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012465
Michael Liaob118a072012-09-20 03:06:15 +000012466 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012467
Michael Liaob118a072012-09-20 03:06:15 +000012468 mainMBB->addSuccessor(origMainMBB);
12469 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012470
Michael Liaob118a072012-09-20 03:06:15 +000012471 // sinkMBB:
12472 sinkMBB->addLiveIn(X86::EAX);
12473 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012474
Michael Liaob118a072012-09-20 03:06:15 +000012475 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12476 TII->get(TargetOpcode::COPY), DstLoReg)
12477 .addReg(X86::EAX);
12478 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12479 TII->get(TargetOpcode::COPY), DstHiReg)
12480 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012481
Michael Liaob118a072012-09-20 03:06:15 +000012482 MI->eraseFromParent();
12483 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012484}
12485
Eric Christopherf83a5de2009-08-27 18:08:16 +000012486// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012487// or XMM0_V32I8 in AVX all of this code can be replaced with that
12488// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012489MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012490X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012491 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012492 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012493 "Target must have SSE4.2 or AVX features enabled");
12494
Eric Christopherb120ab42009-08-18 22:50:32 +000012495 DebugLoc dl = MI->getDebugLoc();
12496 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012497 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012498 if (!Subtarget->hasAVX()) {
12499 if (memArg)
12500 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12501 else
12502 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12503 } else {
12504 if (memArg)
12505 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12506 else
12507 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12508 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012509
Eric Christopher41c902f2010-11-30 08:20:21 +000012510 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012511 for (unsigned i = 0; i < numArgs; ++i) {
12512 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012513 if (!(Op.isReg() && Op.isImplicit()))
12514 MIB.addOperand(Op);
12515 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012516 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012517 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012518 .addReg(X86::XMM0);
12519
Dan Gohman14152b42010-07-06 20:24:04 +000012520 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012521 return BB;
12522}
12523
12524MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012525X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012526 DebugLoc dl = MI->getDebugLoc();
12527 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012528
Eric Christopher228232b2010-11-30 07:20:12 +000012529 // Address into RAX/EAX, other two args into ECX, EDX.
12530 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12531 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12532 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12533 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012534 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012535
Eric Christopher228232b2010-11-30 07:20:12 +000012536 unsigned ValOps = X86::AddrNumOperands;
12537 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12538 .addReg(MI->getOperand(ValOps).getReg());
12539 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12540 .addReg(MI->getOperand(ValOps+1).getReg());
12541
12542 // The instruction doesn't actually take any operands though.
12543 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012544
Eric Christopher228232b2010-11-30 07:20:12 +000012545 MI->eraseFromParent(); // The pseudo is gone now.
12546 return BB;
12547}
12548
12549MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012550X86TargetLowering::EmitVAARG64WithCustomInserter(
12551 MachineInstr *MI,
12552 MachineBasicBlock *MBB) const {
12553 // Emit va_arg instruction on X86-64.
12554
12555 // Operands to this pseudo-instruction:
12556 // 0 ) Output : destination address (reg)
12557 // 1-5) Input : va_list address (addr, i64mem)
12558 // 6 ) ArgSize : Size (in bytes) of vararg type
12559 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12560 // 8 ) Align : Alignment of type
12561 // 9 ) EFLAGS (implicit-def)
12562
12563 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12564 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12565
12566 unsigned DestReg = MI->getOperand(0).getReg();
12567 MachineOperand &Base = MI->getOperand(1);
12568 MachineOperand &Scale = MI->getOperand(2);
12569 MachineOperand &Index = MI->getOperand(3);
12570 MachineOperand &Disp = MI->getOperand(4);
12571 MachineOperand &Segment = MI->getOperand(5);
12572 unsigned ArgSize = MI->getOperand(6).getImm();
12573 unsigned ArgMode = MI->getOperand(7).getImm();
12574 unsigned Align = MI->getOperand(8).getImm();
12575
12576 // Memory Reference
12577 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12578 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12579 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12580
12581 // Machine Information
12582 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12583 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12584 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12585 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12586 DebugLoc DL = MI->getDebugLoc();
12587
12588 // struct va_list {
12589 // i32 gp_offset
12590 // i32 fp_offset
12591 // i64 overflow_area (address)
12592 // i64 reg_save_area (address)
12593 // }
12594 // sizeof(va_list) = 24
12595 // alignment(va_list) = 8
12596
12597 unsigned TotalNumIntRegs = 6;
12598 unsigned TotalNumXMMRegs = 8;
12599 bool UseGPOffset = (ArgMode == 1);
12600 bool UseFPOffset = (ArgMode == 2);
12601 unsigned MaxOffset = TotalNumIntRegs * 8 +
12602 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12603
12604 /* Align ArgSize to a multiple of 8 */
12605 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12606 bool NeedsAlign = (Align > 8);
12607
12608 MachineBasicBlock *thisMBB = MBB;
12609 MachineBasicBlock *overflowMBB;
12610 MachineBasicBlock *offsetMBB;
12611 MachineBasicBlock *endMBB;
12612
12613 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12614 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12615 unsigned OffsetReg = 0;
12616
12617 if (!UseGPOffset && !UseFPOffset) {
12618 // If we only pull from the overflow region, we don't create a branch.
12619 // We don't need to alter control flow.
12620 OffsetDestReg = 0; // unused
12621 OverflowDestReg = DestReg;
12622
12623 offsetMBB = NULL;
12624 overflowMBB = thisMBB;
12625 endMBB = thisMBB;
12626 } else {
12627 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12628 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12629 // If not, pull from overflow_area. (branch to overflowMBB)
12630 //
12631 // thisMBB
12632 // | .
12633 // | .
12634 // offsetMBB overflowMBB
12635 // | .
12636 // | .
12637 // endMBB
12638
12639 // Registers for the PHI in endMBB
12640 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12641 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12642
12643 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12644 MachineFunction *MF = MBB->getParent();
12645 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12646 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12647 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12648
12649 MachineFunction::iterator MBBIter = MBB;
12650 ++MBBIter;
12651
12652 // Insert the new basic blocks
12653 MF->insert(MBBIter, offsetMBB);
12654 MF->insert(MBBIter, overflowMBB);
12655 MF->insert(MBBIter, endMBB);
12656
12657 // Transfer the remainder of MBB and its successor edges to endMBB.
12658 endMBB->splice(endMBB->begin(), thisMBB,
12659 llvm::next(MachineBasicBlock::iterator(MI)),
12660 thisMBB->end());
12661 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12662
12663 // Make offsetMBB and overflowMBB successors of thisMBB
12664 thisMBB->addSuccessor(offsetMBB);
12665 thisMBB->addSuccessor(overflowMBB);
12666
12667 // endMBB is a successor of both offsetMBB and overflowMBB
12668 offsetMBB->addSuccessor(endMBB);
12669 overflowMBB->addSuccessor(endMBB);
12670
12671 // Load the offset value into a register
12672 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12673 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12674 .addOperand(Base)
12675 .addOperand(Scale)
12676 .addOperand(Index)
12677 .addDisp(Disp, UseFPOffset ? 4 : 0)
12678 .addOperand(Segment)
12679 .setMemRefs(MMOBegin, MMOEnd);
12680
12681 // Check if there is enough room left to pull this argument.
12682 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12683 .addReg(OffsetReg)
12684 .addImm(MaxOffset + 8 - ArgSizeA8);
12685
12686 // Branch to "overflowMBB" if offset >= max
12687 // Fall through to "offsetMBB" otherwise
12688 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12689 .addMBB(overflowMBB);
12690 }
12691
12692 // In offsetMBB, emit code to use the reg_save_area.
12693 if (offsetMBB) {
12694 assert(OffsetReg != 0);
12695
12696 // Read the reg_save_area address.
12697 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12698 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12699 .addOperand(Base)
12700 .addOperand(Scale)
12701 .addOperand(Index)
12702 .addDisp(Disp, 16)
12703 .addOperand(Segment)
12704 .setMemRefs(MMOBegin, MMOEnd);
12705
12706 // Zero-extend the offset
12707 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12708 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12709 .addImm(0)
12710 .addReg(OffsetReg)
12711 .addImm(X86::sub_32bit);
12712
12713 // Add the offset to the reg_save_area to get the final address.
12714 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12715 .addReg(OffsetReg64)
12716 .addReg(RegSaveReg);
12717
12718 // Compute the offset for the next argument
12719 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12720 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12721 .addReg(OffsetReg)
12722 .addImm(UseFPOffset ? 16 : 8);
12723
12724 // Store it back into the va_list.
12725 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12726 .addOperand(Base)
12727 .addOperand(Scale)
12728 .addOperand(Index)
12729 .addDisp(Disp, UseFPOffset ? 4 : 0)
12730 .addOperand(Segment)
12731 .addReg(NextOffsetReg)
12732 .setMemRefs(MMOBegin, MMOEnd);
12733
12734 // Jump to endMBB
12735 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12736 .addMBB(endMBB);
12737 }
12738
12739 //
12740 // Emit code to use overflow area
12741 //
12742
12743 // Load the overflow_area address into a register.
12744 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12745 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12746 .addOperand(Base)
12747 .addOperand(Scale)
12748 .addOperand(Index)
12749 .addDisp(Disp, 8)
12750 .addOperand(Segment)
12751 .setMemRefs(MMOBegin, MMOEnd);
12752
12753 // If we need to align it, do so. Otherwise, just copy the address
12754 // to OverflowDestReg.
12755 if (NeedsAlign) {
12756 // Align the overflow address
12757 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12758 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12759
12760 // aligned_addr = (addr + (align-1)) & ~(align-1)
12761 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12762 .addReg(OverflowAddrReg)
12763 .addImm(Align-1);
12764
12765 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12766 .addReg(TmpReg)
12767 .addImm(~(uint64_t)(Align-1));
12768 } else {
12769 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12770 .addReg(OverflowAddrReg);
12771 }
12772
12773 // Compute the next overflow address after this argument.
12774 // (the overflow address should be kept 8-byte aligned)
12775 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12776 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12777 .addReg(OverflowDestReg)
12778 .addImm(ArgSizeA8);
12779
12780 // Store the new overflow address.
12781 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12782 .addOperand(Base)
12783 .addOperand(Scale)
12784 .addOperand(Index)
12785 .addDisp(Disp, 8)
12786 .addOperand(Segment)
12787 .addReg(NextAddrReg)
12788 .setMemRefs(MMOBegin, MMOEnd);
12789
12790 // If we branched, emit the PHI to the front of endMBB.
12791 if (offsetMBB) {
12792 BuildMI(*endMBB, endMBB->begin(), DL,
12793 TII->get(X86::PHI), DestReg)
12794 .addReg(OffsetDestReg).addMBB(offsetMBB)
12795 .addReg(OverflowDestReg).addMBB(overflowMBB);
12796 }
12797
12798 // Erase the pseudo instruction
12799 MI->eraseFromParent();
12800
12801 return endMBB;
12802}
12803
12804MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012805X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12806 MachineInstr *MI,
12807 MachineBasicBlock *MBB) const {
12808 // Emit code to save XMM registers to the stack. The ABI says that the
12809 // number of registers to save is given in %al, so it's theoretically
12810 // possible to do an indirect jump trick to avoid saving all of them,
12811 // however this code takes a simpler approach and just executes all
12812 // of the stores if %al is non-zero. It's less code, and it's probably
12813 // easier on the hardware branch predictor, and stores aren't all that
12814 // expensive anyway.
12815
12816 // Create the new basic blocks. One block contains all the XMM stores,
12817 // and one block is the final destination regardless of whether any
12818 // stores were performed.
12819 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12820 MachineFunction *F = MBB->getParent();
12821 MachineFunction::iterator MBBIter = MBB;
12822 ++MBBIter;
12823 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12824 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12825 F->insert(MBBIter, XMMSaveMBB);
12826 F->insert(MBBIter, EndMBB);
12827
Dan Gohman14152b42010-07-06 20:24:04 +000012828 // Transfer the remainder of MBB and its successor edges to EndMBB.
12829 EndMBB->splice(EndMBB->begin(), MBB,
12830 llvm::next(MachineBasicBlock::iterator(MI)),
12831 MBB->end());
12832 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12833
Dan Gohmand6708ea2009-08-15 01:38:56 +000012834 // The original block will now fall through to the XMM save block.
12835 MBB->addSuccessor(XMMSaveMBB);
12836 // The XMMSaveMBB will fall through to the end block.
12837 XMMSaveMBB->addSuccessor(EndMBB);
12838
12839 // Now add the instructions.
12840 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12841 DebugLoc DL = MI->getDebugLoc();
12842
12843 unsigned CountReg = MI->getOperand(0).getReg();
12844 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12845 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12846
12847 if (!Subtarget->isTargetWin64()) {
12848 // If %al is 0, branch around the XMM save block.
12849 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012850 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012851 MBB->addSuccessor(EndMBB);
12852 }
12853
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012854 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012855 // In the XMM save block, save all the XMM argument registers.
12856 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12857 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012858 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012859 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012860 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012861 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012862 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012863 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012864 .addFrameIndex(RegSaveFrameIndex)
12865 .addImm(/*Scale=*/1)
12866 .addReg(/*IndexReg=*/0)
12867 .addImm(/*Disp=*/Offset)
12868 .addReg(/*Segment=*/0)
12869 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012870 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012871 }
12872
Dan Gohman14152b42010-07-06 20:24:04 +000012873 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012874
12875 return EndMBB;
12876}
Mon P Wang63307c32008-05-05 19:05:59 +000012877
Lang Hames6e3f7e42012-02-03 01:13:49 +000012878// The EFLAGS operand of SelectItr might be missing a kill marker
12879// because there were multiple uses of EFLAGS, and ISel didn't know
12880// which to mark. Figure out whether SelectItr should have had a
12881// kill marker, and set it if it should. Returns the correct kill
12882// marker value.
12883static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12884 MachineBasicBlock* BB,
12885 const TargetRegisterInfo* TRI) {
12886 // Scan forward through BB for a use/def of EFLAGS.
12887 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12888 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012889 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012890 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012891 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012892 if (mi.definesRegister(X86::EFLAGS))
12893 break; // Should have kill-flag - update below.
12894 }
12895
12896 // If we hit the end of the block, check whether EFLAGS is live into a
12897 // successor.
12898 if (miI == BB->end()) {
12899 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12900 sEnd = BB->succ_end();
12901 sItr != sEnd; ++sItr) {
12902 MachineBasicBlock* succ = *sItr;
12903 if (succ->isLiveIn(X86::EFLAGS))
12904 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012905 }
12906 }
12907
Lang Hames6e3f7e42012-02-03 01:13:49 +000012908 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12909 // out. SelectMI should have a kill flag on EFLAGS.
12910 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012911 return true;
12912}
12913
Evan Cheng60c07e12006-07-05 22:17:51 +000012914MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012915X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012916 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012917 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12918 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012919
Chris Lattner52600972009-09-02 05:57:00 +000012920 // To "insert" a SELECT_CC instruction, we actually have to insert the
12921 // diamond control-flow pattern. The incoming instruction knows the
12922 // destination vreg to set, the condition code register to branch on, the
12923 // true/false values to select between, and a branch opcode to use.
12924 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12925 MachineFunction::iterator It = BB;
12926 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012927
Chris Lattner52600972009-09-02 05:57:00 +000012928 // thisMBB:
12929 // ...
12930 // TrueVal = ...
12931 // cmpTY ccX, r1, r2
12932 // bCC copy1MBB
12933 // fallthrough --> copy0MBB
12934 MachineBasicBlock *thisMBB = BB;
12935 MachineFunction *F = BB->getParent();
12936 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12937 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012938 F->insert(It, copy0MBB);
12939 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012940
Bill Wendling730c07e2010-06-25 20:48:10 +000012941 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12942 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012943 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12944 if (!MI->killsRegister(X86::EFLAGS) &&
12945 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12946 copy0MBB->addLiveIn(X86::EFLAGS);
12947 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012948 }
12949
Dan Gohman14152b42010-07-06 20:24:04 +000012950 // Transfer the remainder of BB and its successor edges to sinkMBB.
12951 sinkMBB->splice(sinkMBB->begin(), BB,
12952 llvm::next(MachineBasicBlock::iterator(MI)),
12953 BB->end());
12954 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12955
12956 // Add the true and fallthrough blocks as its successors.
12957 BB->addSuccessor(copy0MBB);
12958 BB->addSuccessor(sinkMBB);
12959
12960 // Create the conditional branch instruction.
12961 unsigned Opc =
12962 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12963 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12964
Chris Lattner52600972009-09-02 05:57:00 +000012965 // copy0MBB:
12966 // %FalseValue = ...
12967 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012968 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012969
Chris Lattner52600972009-09-02 05:57:00 +000012970 // sinkMBB:
12971 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12972 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012973 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12974 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012975 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12976 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12977
Dan Gohman14152b42010-07-06 20:24:04 +000012978 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012979 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012980}
12981
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012982MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012983X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12984 bool Is64Bit) const {
12985 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12986 DebugLoc DL = MI->getDebugLoc();
12987 MachineFunction *MF = BB->getParent();
12988 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12989
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012990 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012991
12992 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12993 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12994
12995 // BB:
12996 // ... [Till the alloca]
12997 // If stacklet is not large enough, jump to mallocMBB
12998 //
12999 // bumpMBB:
13000 // Allocate by subtracting from RSP
13001 // Jump to continueMBB
13002 //
13003 // mallocMBB:
13004 // Allocate by call to runtime
13005 //
13006 // continueMBB:
13007 // ...
13008 // [rest of original BB]
13009 //
13010
13011 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13012 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13013 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13014
13015 MachineRegisterInfo &MRI = MF->getRegInfo();
13016 const TargetRegisterClass *AddrRegClass =
13017 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13018
13019 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13020 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13021 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013022 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013023 sizeVReg = MI->getOperand(1).getReg(),
13024 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13025
13026 MachineFunction::iterator MBBIter = BB;
13027 ++MBBIter;
13028
13029 MF->insert(MBBIter, bumpMBB);
13030 MF->insert(MBBIter, mallocMBB);
13031 MF->insert(MBBIter, continueMBB);
13032
13033 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13034 (MachineBasicBlock::iterator(MI)), BB->end());
13035 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13036
13037 // Add code to the main basic block to check if the stack limit has been hit,
13038 // and if so, jump to mallocMBB otherwise to bumpMBB.
13039 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013040 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013041 .addReg(tmpSPVReg).addReg(sizeVReg);
13042 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013043 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013044 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013045 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13046
13047 // bumpMBB simply decreases the stack pointer, since we know the current
13048 // stacklet has enough space.
13049 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013050 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013051 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013052 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013053 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13054
13055 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013056 const uint32_t *RegMask =
13057 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013058 if (Is64Bit) {
13059 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13060 .addReg(sizeVReg);
13061 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013062 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013063 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013064 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013065 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013066 } else {
13067 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13068 .addImm(12);
13069 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13070 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013071 .addExternalSymbol("__morestack_allocate_stack_space")
13072 .addRegMask(RegMask)
13073 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013074 }
13075
13076 if (!Is64Bit)
13077 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13078 .addImm(16);
13079
13080 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13081 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13082 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13083
13084 // Set up the CFG correctly.
13085 BB->addSuccessor(bumpMBB);
13086 BB->addSuccessor(mallocMBB);
13087 mallocMBB->addSuccessor(continueMBB);
13088 bumpMBB->addSuccessor(continueMBB);
13089
13090 // Take care of the PHI nodes.
13091 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13092 MI->getOperand(0).getReg())
13093 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13094 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13095
13096 // Delete the original pseudo instruction.
13097 MI->eraseFromParent();
13098
13099 // And we're done.
13100 return continueMBB;
13101}
13102
13103MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013104X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013105 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013106 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13107 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013108
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013109 assert(!Subtarget->isTargetEnvMacho());
13110
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013111 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13112 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013113
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013114 if (Subtarget->isTargetWin64()) {
13115 if (Subtarget->isTargetCygMing()) {
13116 // ___chkstk(Mingw64):
13117 // Clobbers R10, R11, RAX and EFLAGS.
13118 // Updates RSP.
13119 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13120 .addExternalSymbol("___chkstk")
13121 .addReg(X86::RAX, RegState::Implicit)
13122 .addReg(X86::RSP, RegState::Implicit)
13123 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13124 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13125 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13126 } else {
13127 // __chkstk(MSVCRT): does not update stack pointer.
13128 // Clobbers R10, R11 and EFLAGS.
13129 // FIXME: RAX(allocated size) might be reused and not killed.
13130 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13131 .addExternalSymbol("__chkstk")
13132 .addReg(X86::RAX, RegState::Implicit)
13133 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13134 // RAX has the offset to subtracted from RSP.
13135 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13136 .addReg(X86::RSP)
13137 .addReg(X86::RAX);
13138 }
13139 } else {
13140 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013141 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13142
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013143 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13144 .addExternalSymbol(StackProbeSymbol)
13145 .addReg(X86::EAX, RegState::Implicit)
13146 .addReg(X86::ESP, RegState::Implicit)
13147 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13148 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13149 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13150 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013151
Dan Gohman14152b42010-07-06 20:24:04 +000013152 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013153 return BB;
13154}
Chris Lattner52600972009-09-02 05:57:00 +000013155
13156MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013157X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13158 MachineBasicBlock *BB) const {
13159 // This is pretty easy. We're taking the value that we received from
13160 // our load from the relocation, sticking it in either RDI (x86-64)
13161 // or EAX and doing an indirect call. The return value will then
13162 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013163 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013164 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013165 DebugLoc DL = MI->getDebugLoc();
13166 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013167
13168 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013169 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013170
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013171 // Get a register mask for the lowered call.
13172 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13173 // proper register mask.
13174 const uint32_t *RegMask =
13175 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013176 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013177 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13178 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013179 .addReg(X86::RIP)
13180 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013181 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013182 MI->getOperand(3).getTargetFlags())
13183 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013184 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013185 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013186 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013187 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013188 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13189 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013190 .addReg(0)
13191 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013192 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013193 MI->getOperand(3).getTargetFlags())
13194 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013195 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013196 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013197 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013198 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013199 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13200 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013201 .addReg(TII->getGlobalBaseReg(F))
13202 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013203 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013204 MI->getOperand(3).getTargetFlags())
13205 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013206 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013207 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013208 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013209 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013210
Dan Gohman14152b42010-07-06 20:24:04 +000013211 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013212 return BB;
13213}
13214
13215MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013216X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013217 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013218 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013219 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013220 case X86::TAILJMPd64:
13221 case X86::TAILJMPr64:
13222 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013223 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013224 case X86::TCRETURNdi64:
13225 case X86::TCRETURNri64:
13226 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013227 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013228 case X86::WIN_ALLOCA:
13229 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013230 case X86::SEG_ALLOCA_32:
13231 return EmitLoweredSegAlloca(MI, BB, false);
13232 case X86::SEG_ALLOCA_64:
13233 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013234 case X86::TLSCall_32:
13235 case X86::TLSCall_64:
13236 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013237 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013238 case X86::CMOV_FR32:
13239 case X86::CMOV_FR64:
13240 case X86::CMOV_V4F32:
13241 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013242 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013243 case X86::CMOV_V8F32:
13244 case X86::CMOV_V4F64:
13245 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013246 case X86::CMOV_GR16:
13247 case X86::CMOV_GR32:
13248 case X86::CMOV_RFP32:
13249 case X86::CMOV_RFP64:
13250 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013251 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013252
Dale Johannesen849f2142007-07-03 00:53:03 +000013253 case X86::FP32_TO_INT16_IN_MEM:
13254 case X86::FP32_TO_INT32_IN_MEM:
13255 case X86::FP32_TO_INT64_IN_MEM:
13256 case X86::FP64_TO_INT16_IN_MEM:
13257 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013258 case X86::FP64_TO_INT64_IN_MEM:
13259 case X86::FP80_TO_INT16_IN_MEM:
13260 case X86::FP80_TO_INT32_IN_MEM:
13261 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13263 DebugLoc DL = MI->getDebugLoc();
13264
Evan Cheng60c07e12006-07-05 22:17:51 +000013265 // Change the floating point control register to use "round towards zero"
13266 // mode when truncating to an integer value.
13267 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013268 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013269 addFrameReference(BuildMI(*BB, MI, DL,
13270 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013271
13272 // Load the old value of the high byte of the control word...
13273 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013274 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013275 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013276 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013277
13278 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013279 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013280 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013281
13282 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013283 addFrameReference(BuildMI(*BB, MI, DL,
13284 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013285
13286 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013287 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013288 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013289
13290 // Get the X86 opcode to use.
13291 unsigned Opc;
13292 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013293 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013294 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13295 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13296 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13297 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13298 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13299 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013300 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13301 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13302 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013303 }
13304
13305 X86AddressMode AM;
13306 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013307 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013308 AM.BaseType = X86AddressMode::RegBase;
13309 AM.Base.Reg = Op.getReg();
13310 } else {
13311 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013312 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013313 }
13314 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013315 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013316 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013317 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013318 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013319 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013320 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013321 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013322 AM.GV = Op.getGlobal();
13323 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013324 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013325 }
Dan Gohman14152b42010-07-06 20:24:04 +000013326 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013327 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013328
13329 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013330 addFrameReference(BuildMI(*BB, MI, DL,
13331 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013332
Dan Gohman14152b42010-07-06 20:24:04 +000013333 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013334 return BB;
13335 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013336 // String/text processing lowering.
13337 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013338 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013339 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013340 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013341 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013342 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013343 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013344 case X86::VPCMPESTRM128MEM: {
13345 unsigned NumArgs;
13346 bool MemArg;
13347 switch (MI->getOpcode()) {
13348 default: llvm_unreachable("illegal opcode!");
13349 case X86::PCMPISTRM128REG:
13350 case X86::VPCMPISTRM128REG:
13351 NumArgs = 3; MemArg = false; break;
13352 case X86::PCMPISTRM128MEM:
13353 case X86::VPCMPISTRM128MEM:
13354 NumArgs = 3; MemArg = true; break;
13355 case X86::PCMPESTRM128REG:
13356 case X86::VPCMPESTRM128REG:
13357 NumArgs = 5; MemArg = false; break;
13358 case X86::PCMPESTRM128MEM:
13359 case X86::VPCMPESTRM128MEM:
13360 NumArgs = 5; MemArg = true; break;
13361 }
13362 return EmitPCMP(MI, BB, NumArgs, MemArg);
13363 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013364
Eric Christopher228232b2010-11-30 07:20:12 +000013365 // Thread synchronization.
13366 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013367 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013368
Eric Christopherb120ab42009-08-18 22:50:32 +000013369 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013370 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013371 case X86::ATOMAND16:
13372 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013373 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013374 // Fall through
13375 case X86::ATOMOR8:
13376 case X86::ATOMOR16:
13377 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013378 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013379 // Fall through
13380 case X86::ATOMXOR16:
13381 case X86::ATOMXOR8:
13382 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013383 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013384 // Fall through
13385 case X86::ATOMNAND8:
13386 case X86::ATOMNAND16:
13387 case X86::ATOMNAND32:
13388 case X86::ATOMNAND64:
13389 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013390 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013391 case X86::ATOMMAX16:
13392 case X86::ATOMMAX32:
13393 case X86::ATOMMAX64:
13394 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013395 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013396 case X86::ATOMMIN16:
13397 case X86::ATOMMIN32:
13398 case X86::ATOMMIN64:
13399 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013400 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013401 case X86::ATOMUMAX16:
13402 case X86::ATOMUMAX32:
13403 case X86::ATOMUMAX64:
13404 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013405 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013406 case X86::ATOMUMIN16:
13407 case X86::ATOMUMIN32:
13408 case X86::ATOMUMIN64:
13409 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013410
13411 // This group does 64-bit operations on a 32-bit host.
13412 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013413 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013414 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013415 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013416 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013417 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000013418 case X86::ATOMMAX6432:
13419 case X86::ATOMMIN6432:
13420 case X86::ATOMUMAX6432:
13421 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000013422 case X86::ATOMSWAP6432:
13423 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013424
Dan Gohmand6708ea2009-08-15 01:38:56 +000013425 case X86::VASTART_SAVE_XMM_REGS:
13426 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013427
13428 case X86::VAARG_64:
13429 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013430 }
13431}
13432
13433//===----------------------------------------------------------------------===//
13434// X86 Optimization Hooks
13435//===----------------------------------------------------------------------===//
13436
Dan Gohman475871a2008-07-27 21:46:04 +000013437void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013438 APInt &KnownZero,
13439 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013440 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013441 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013442 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013443 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013444 assert((Opc >= ISD::BUILTIN_OP_END ||
13445 Opc == ISD::INTRINSIC_WO_CHAIN ||
13446 Opc == ISD::INTRINSIC_W_CHAIN ||
13447 Opc == ISD::INTRINSIC_VOID) &&
13448 "Should use MaskedValueIsZero if you don't know whether Op"
13449 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013450
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013451 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013452 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013453 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013454 case X86ISD::ADD:
13455 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013456 case X86ISD::ADC:
13457 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013458 case X86ISD::SMUL:
13459 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013460 case X86ISD::INC:
13461 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013462 case X86ISD::OR:
13463 case X86ISD::XOR:
13464 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013465 // These nodes' second result is a boolean.
13466 if (Op.getResNo() == 0)
13467 break;
13468 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013469 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013470 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013471 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013472 case ISD::INTRINSIC_WO_CHAIN: {
13473 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13474 unsigned NumLoBits = 0;
13475 switch (IntId) {
13476 default: break;
13477 case Intrinsic::x86_sse_movmsk_ps:
13478 case Intrinsic::x86_avx_movmsk_ps_256:
13479 case Intrinsic::x86_sse2_movmsk_pd:
13480 case Intrinsic::x86_avx_movmsk_pd_256:
13481 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013482 case Intrinsic::x86_sse2_pmovmskb_128:
13483 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013484 // High bits of movmskp{s|d}, pmovmskb are known zero.
13485 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013486 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013487 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13488 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13489 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13490 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13491 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13492 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013493 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013494 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013495 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013496 break;
13497 }
13498 }
13499 break;
13500 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013501 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013502}
Chris Lattner259e97c2006-01-31 19:43:35 +000013503
Owen Andersonbc146b02010-09-21 20:42:50 +000013504unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13505 unsigned Depth) const {
13506 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13507 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13508 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013509
Owen Andersonbc146b02010-09-21 20:42:50 +000013510 // Fallback case.
13511 return 1;
13512}
13513
Evan Cheng206ee9d2006-07-07 08:33:52 +000013514/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013515/// node is a GlobalAddress + offset.
13516bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013517 const GlobalValue* &GA,
13518 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013519 if (N->getOpcode() == X86ISD::Wrapper) {
13520 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013521 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013522 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013523 return true;
13524 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013525 }
Evan Chengad4196b2008-05-12 19:56:52 +000013526 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013527}
13528
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013529/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13530/// same as extracting the high 128-bit part of 256-bit vector and then
13531/// inserting the result into the low part of a new 256-bit vector
13532static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13533 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013534 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013535
13536 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013537 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013538 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13539 SVOp->getMaskElt(j) >= 0)
13540 return false;
13541
13542 return true;
13543}
13544
13545/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13546/// same as extracting the low 128-bit part of 256-bit vector and then
13547/// inserting the result into the high part of a new 256-bit vector
13548static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13549 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013550 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013551
13552 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013553 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013554 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13555 SVOp->getMaskElt(j) >= 0)
13556 return false;
13557
13558 return true;
13559}
13560
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013561/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13562static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013563 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013564 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013565 DebugLoc dl = N->getDebugLoc();
13566 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13567 SDValue V1 = SVOp->getOperand(0);
13568 SDValue V2 = SVOp->getOperand(1);
13569 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013570 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013571
13572 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13573 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13574 //
13575 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013576 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013577 // V UNDEF BUILD_VECTOR UNDEF
13578 // \ / \ /
13579 // CONCAT_VECTOR CONCAT_VECTOR
13580 // \ /
13581 // \ /
13582 // RESULT: V + zero extended
13583 //
13584 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13585 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13586 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13587 return SDValue();
13588
13589 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13590 return SDValue();
13591
13592 // To match the shuffle mask, the first half of the mask should
13593 // be exactly the first vector, and all the rest a splat with the
13594 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013595 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013596 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13597 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13598 return SDValue();
13599
Chad Rosier3d1161e2012-01-03 21:05:52 +000013600 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13601 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013602 if (Ld->hasNUsesOfValue(1, 0)) {
13603 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13604 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13605 SDValue ResNode =
13606 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13607 Ld->getMemoryVT(),
13608 Ld->getPointerInfo(),
13609 Ld->getAlignment(),
13610 false/*isVolatile*/, true/*ReadMem*/,
13611 false/*WriteMem*/);
13612 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13613 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013614 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013615
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013616 // Emit a zeroed vector and insert the desired subvector on its
13617 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013618 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013619 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013620 return DCI.CombineTo(N, InsV);
13621 }
13622
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013623 //===--------------------------------------------------------------------===//
13624 // Combine some shuffles into subvector extracts and inserts:
13625 //
13626
13627 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13628 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013629 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13630 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013631 return DCI.CombineTo(N, InsV);
13632 }
13633
13634 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13635 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013636 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13637 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013638 return DCI.CombineTo(N, InsV);
13639 }
13640
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013641 return SDValue();
13642}
13643
13644/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013645static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013646 TargetLowering::DAGCombinerInfo &DCI,
13647 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013648 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013649 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013650
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013651 // Don't create instructions with illegal types after legalize types has run.
13652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13653 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13654 return SDValue();
13655
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013656 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013657 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013658 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013659 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013660
13661 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013662 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013663 return SDValue();
13664
13665 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13666 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13667 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013668 SmallVector<SDValue, 16> Elts;
13669 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013670 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013671
Nate Begemanfdea31a2010-03-24 20:49:50 +000013672 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013673}
Evan Chengd880b972008-05-09 21:53:03 +000013674
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013675
Craig Topper55b24052012-09-11 06:15:32 +000013676/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013677/// a sequence of vector shuffle operations.
13678/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013679static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13680 TargetLowering::DAGCombinerInfo &DCI,
13681 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013682 if (!DCI.isBeforeLegalizeOps())
13683 return SDValue();
13684
Craig Topper3ef43cf2012-04-24 06:36:35 +000013685 if (!Subtarget->hasAVX())
13686 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013687
13688 EVT VT = N->getValueType(0);
13689 SDValue Op = N->getOperand(0);
13690 EVT OpVT = Op.getValueType();
13691 DebugLoc dl = N->getDebugLoc();
13692
13693 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13694
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013695 if (Subtarget->hasAVX2()) {
13696 // AVX2: v4i64 -> v4i32
13697
13698 // VPERMD
13699 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13700
13701 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13702 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13703 ShufMask);
13704
Craig Topperd63fa652012-04-22 18:51:37 +000013705 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13706 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013707 }
13708
13709 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013710 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013711 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013712
13713 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013714 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013715
13716 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13717 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13718
13719 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013720 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013721
Craig Toppercacafd42012-08-14 08:18:43 +000013722 SDValue Undef = DAG.getUNDEF(VT);
13723 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13724 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013725
13726 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013727 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013728
Elena Demikhovsky73252572012-02-01 10:33:05 +000013729 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013730 }
Craig Topperd63fa652012-04-22 18:51:37 +000013731
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013732 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13733
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013734 if (Subtarget->hasAVX2()) {
13735 // AVX2: v8i32 -> v8i16
13736
13737 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013738
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013739 // PSHUFB
13740 SmallVector<SDValue,32> pshufbMask;
13741 for (unsigned i = 0; i < 2; ++i) {
13742 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13743 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13744 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13745 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13746 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13747 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13748 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13749 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13750 for (unsigned j = 0; j < 8; ++j)
13751 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13752 }
Craig Topperd63fa652012-04-22 18:51:37 +000013753 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13754 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013755 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13756
13757 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13758
13759 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013760 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013761 &ShufMask[0]);
13762
Craig Topperd63fa652012-04-22 18:51:37 +000013763 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13764 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013765
13766 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13767 }
13768
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013769 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013770 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013771
13772 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013773 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013774
13775 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13776 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13777
13778 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013779 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13780 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013781
Craig Toppercacafd42012-08-14 08:18:43 +000013782 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13783 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13784 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013785
13786 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13787 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13788
13789 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013790 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013791
Elena Demikhovsky73252572012-02-01 10:33:05 +000013792 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013793 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013794 }
13795
13796 return SDValue();
13797}
13798
Craig Topper89f4e662012-03-20 07:17:59 +000013799/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13800/// specific shuffle of a load can be folded into a single element load.
13801/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13802/// shuffles have been customed lowered so we need to handle those here.
13803static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13804 TargetLowering::DAGCombinerInfo &DCI) {
13805 if (DCI.isBeforeLegalizeOps())
13806 return SDValue();
13807
13808 SDValue InVec = N->getOperand(0);
13809 SDValue EltNo = N->getOperand(1);
13810
13811 if (!isa<ConstantSDNode>(EltNo))
13812 return SDValue();
13813
13814 EVT VT = InVec.getValueType();
13815
13816 bool HasShuffleIntoBitcast = false;
13817 if (InVec.getOpcode() == ISD::BITCAST) {
13818 // Don't duplicate a load with other uses.
13819 if (!InVec.hasOneUse())
13820 return SDValue();
13821 EVT BCVT = InVec.getOperand(0).getValueType();
13822 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13823 return SDValue();
13824 InVec = InVec.getOperand(0);
13825 HasShuffleIntoBitcast = true;
13826 }
13827
13828 if (!isTargetShuffle(InVec.getOpcode()))
13829 return SDValue();
13830
13831 // Don't duplicate a load with other uses.
13832 if (!InVec.hasOneUse())
13833 return SDValue();
13834
13835 SmallVector<int, 16> ShuffleMask;
13836 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013837 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13838 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013839 return SDValue();
13840
13841 // Select the input vector, guarding against out of range extract vector.
13842 unsigned NumElems = VT.getVectorNumElements();
13843 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13844 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13845 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13846 : InVec.getOperand(1);
13847
13848 // If inputs to shuffle are the same for both ops, then allow 2 uses
13849 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13850
13851 if (LdNode.getOpcode() == ISD::BITCAST) {
13852 // Don't duplicate a load with other uses.
13853 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13854 return SDValue();
13855
13856 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13857 LdNode = LdNode.getOperand(0);
13858 }
13859
13860 if (!ISD::isNormalLoad(LdNode.getNode()))
13861 return SDValue();
13862
13863 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13864
13865 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13866 return SDValue();
13867
13868 if (HasShuffleIntoBitcast) {
13869 // If there's a bitcast before the shuffle, check if the load type and
13870 // alignment is valid.
13871 unsigned Align = LN0->getAlignment();
13872 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000013873 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000013874 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13875
13876 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13877 return SDValue();
13878 }
13879
13880 // All checks match so transform back to vector_shuffle so that DAG combiner
13881 // can finish the job
13882 DebugLoc dl = N->getDebugLoc();
13883
13884 // Create shuffle node taking into account the case that its a unary shuffle
13885 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13886 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13887 InVec.getOperand(0), Shuffle,
13888 &ShuffleMask[0]);
13889 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13890 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13891 EltNo);
13892}
13893
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013894/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13895/// generation and convert it from being a bunch of shuffles and extracts
13896/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013897static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013898 TargetLowering::DAGCombinerInfo &DCI) {
13899 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13900 if (NewOp.getNode())
13901 return NewOp;
13902
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013903 SDValue InputVector = N->getOperand(0);
13904
13905 // Only operate on vectors of 4 elements, where the alternative shuffling
13906 // gets to be more expensive.
13907 if (InputVector.getValueType() != MVT::v4i32)
13908 return SDValue();
13909
13910 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13911 // single use which is a sign-extend or zero-extend, and all elements are
13912 // used.
13913 SmallVector<SDNode *, 4> Uses;
13914 unsigned ExtractedElements = 0;
13915 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13916 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13917 if (UI.getUse().getResNo() != InputVector.getResNo())
13918 return SDValue();
13919
13920 SDNode *Extract = *UI;
13921 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13922 return SDValue();
13923
13924 if (Extract->getValueType(0) != MVT::i32)
13925 return SDValue();
13926 if (!Extract->hasOneUse())
13927 return SDValue();
13928 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13929 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13930 return SDValue();
13931 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13932 return SDValue();
13933
13934 // Record which element was extracted.
13935 ExtractedElements |=
13936 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13937
13938 Uses.push_back(Extract);
13939 }
13940
13941 // If not all the elements were used, this may not be worthwhile.
13942 if (ExtractedElements != 15)
13943 return SDValue();
13944
13945 // Ok, we've now decided to do the transformation.
13946 DebugLoc dl = InputVector.getDebugLoc();
13947
13948 // Store the value to a temporary stack slot.
13949 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013950 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13951 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013952
13953 // Replace each use (extract) with a load of the appropriate element.
13954 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13955 UE = Uses.end(); UI != UE; ++UI) {
13956 SDNode *Extract = *UI;
13957
Nadav Rotem86694292011-05-17 08:31:57 +000013958 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013959 SDValue Idx = Extract->getOperand(1);
13960 unsigned EltSize =
13961 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13962 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013963 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013964 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13965
Nadav Rotem86694292011-05-17 08:31:57 +000013966 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013967 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013968
13969 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013970 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013971 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013972 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013973
13974 // Replace the exact with the load.
13975 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13976 }
13977
13978 // The replacement was made in place; don't return anything.
13979 return SDValue();
13980}
13981
Duncan Sands6bcd2192011-09-17 16:49:39 +000013982/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13983/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013984static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013985 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013986 const X86Subtarget *Subtarget) {
13987 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013988 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013989 // Get the LHS/RHS of the select.
13990 SDValue LHS = N->getOperand(1);
13991 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013992 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013993
Dan Gohman670e5392009-09-21 18:03:22 +000013994 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013995 // instructions match the semantics of the common C idiom x<y?x:y but not
13996 // x<=y?x:y, because of how they handle negative zero (which can be
13997 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013998 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13999 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014000 (Subtarget->hasSSE2() ||
14001 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014002 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014003
Chris Lattner47b4ce82009-03-11 05:48:52 +000014004 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014005 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014006 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14007 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014008 switch (CC) {
14009 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014010 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014011 // Converting this to a min would handle NaNs incorrectly, and swapping
14012 // the operands would cause it to handle comparisons between positive
14013 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014014 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014015 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014016 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14017 break;
14018 std::swap(LHS, RHS);
14019 }
Dan Gohman670e5392009-09-21 18:03:22 +000014020 Opcode = X86ISD::FMIN;
14021 break;
14022 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014023 // Converting this to a min would handle comparisons between positive
14024 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014025 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014026 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14027 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014028 Opcode = X86ISD::FMIN;
14029 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014030 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014031 // Converting this to a min would handle both negative zeros and NaNs
14032 // incorrectly, but we can swap the operands to fix both.
14033 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014034 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014035 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014036 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014037 Opcode = X86ISD::FMIN;
14038 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014039
Dan Gohman670e5392009-09-21 18:03:22 +000014040 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014041 // Converting this to a max would handle comparisons between positive
14042 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014043 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014044 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014045 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014046 Opcode = X86ISD::FMAX;
14047 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014048 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014049 // Converting this to a max would handle NaNs incorrectly, and swapping
14050 // the operands would cause it to handle comparisons between positive
14051 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014052 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014053 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014054 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14055 break;
14056 std::swap(LHS, RHS);
14057 }
Dan Gohman670e5392009-09-21 18:03:22 +000014058 Opcode = X86ISD::FMAX;
14059 break;
14060 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014061 // Converting this to a max would handle both negative zeros and NaNs
14062 // incorrectly, but we can swap the operands to fix both.
14063 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014064 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014065 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014066 case ISD::SETGE:
14067 Opcode = X86ISD::FMAX;
14068 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014069 }
Dan Gohman670e5392009-09-21 18:03:22 +000014070 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014071 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14072 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014073 switch (CC) {
14074 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014075 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014076 // Converting this to a min would handle comparisons between positive
14077 // and negative zero incorrectly, and swapping the operands would
14078 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014079 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014080 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014081 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014082 break;
14083 std::swap(LHS, RHS);
14084 }
Dan Gohman670e5392009-09-21 18:03:22 +000014085 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014086 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014087 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014088 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014089 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014090 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14091 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014092 Opcode = X86ISD::FMIN;
14093 break;
14094 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014095 // Converting this to a min would handle both negative zeros and NaNs
14096 // incorrectly, but we can swap the operands to fix both.
14097 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014098 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014099 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014100 case ISD::SETGE:
14101 Opcode = X86ISD::FMIN;
14102 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014103
Dan Gohman670e5392009-09-21 18:03:22 +000014104 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014105 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014106 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014107 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014108 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014109 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014110 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014111 // Converting this to a max would handle comparisons between positive
14112 // and negative zero incorrectly, and swapping the operands would
14113 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014114 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014115 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014116 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014117 break;
14118 std::swap(LHS, RHS);
14119 }
Dan Gohman670e5392009-09-21 18:03:22 +000014120 Opcode = X86ISD::FMAX;
14121 break;
14122 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014123 // Converting this to a max would handle both negative zeros and NaNs
14124 // incorrectly, but we can swap the operands to fix both.
14125 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014126 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014127 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014128 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014129 Opcode = X86ISD::FMAX;
14130 break;
14131 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014132 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014133
Chris Lattner47b4ce82009-03-11 05:48:52 +000014134 if (Opcode)
14135 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014136 }
Eric Christopherfd179292009-08-27 18:07:15 +000014137
Chris Lattnerd1980a52009-03-12 06:52:53 +000014138 // If this is a select between two integer constants, try to do some
14139 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014140 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14141 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014142 // Don't do this for crazy integer types.
14143 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14144 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014145 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014146 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014147
Chris Lattnercee56e72009-03-13 05:53:31 +000014148 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014149 // Efficiently invertible.
14150 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14151 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14152 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14153 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014154 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014155 }
Eric Christopherfd179292009-08-27 18:07:15 +000014156
Chris Lattnerd1980a52009-03-12 06:52:53 +000014157 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014158 if (FalseC->getAPIntValue() == 0 &&
14159 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014160 if (NeedsCondInvert) // Invert the condition if needed.
14161 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14162 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014163
Chris Lattnerd1980a52009-03-12 06:52:53 +000014164 // Zero extend the condition if needed.
14165 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014166
Chris Lattnercee56e72009-03-13 05:53:31 +000014167 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014168 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014169 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014170 }
Eric Christopherfd179292009-08-27 18:07:15 +000014171
Chris Lattner97a29a52009-03-13 05:22:11 +000014172 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014173 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014174 if (NeedsCondInvert) // Invert the condition if needed.
14175 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14176 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014177
Chris Lattner97a29a52009-03-13 05:22:11 +000014178 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014179 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14180 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014181 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014182 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014183 }
Eric Christopherfd179292009-08-27 18:07:15 +000014184
Chris Lattnercee56e72009-03-13 05:53:31 +000014185 // Optimize cases that will turn into an LEA instruction. This requires
14186 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014187 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014188 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014189 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014190
Chris Lattnercee56e72009-03-13 05:53:31 +000014191 bool isFastMultiplier = false;
14192 if (Diff < 10) {
14193 switch ((unsigned char)Diff) {
14194 default: break;
14195 case 1: // result = add base, cond
14196 case 2: // result = lea base( , cond*2)
14197 case 3: // result = lea base(cond, cond*2)
14198 case 4: // result = lea base( , cond*4)
14199 case 5: // result = lea base(cond, cond*4)
14200 case 8: // result = lea base( , cond*8)
14201 case 9: // result = lea base(cond, cond*8)
14202 isFastMultiplier = true;
14203 break;
14204 }
14205 }
Eric Christopherfd179292009-08-27 18:07:15 +000014206
Chris Lattnercee56e72009-03-13 05:53:31 +000014207 if (isFastMultiplier) {
14208 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14209 if (NeedsCondInvert) // Invert the condition if needed.
14210 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14211 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014212
Chris Lattnercee56e72009-03-13 05:53:31 +000014213 // Zero extend the condition if needed.
14214 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14215 Cond);
14216 // Scale the condition by the difference.
14217 if (Diff != 1)
14218 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14219 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014220
Chris Lattnercee56e72009-03-13 05:53:31 +000014221 // Add the base if non-zero.
14222 if (FalseC->getAPIntValue() != 0)
14223 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14224 SDValue(FalseC, 0));
14225 return Cond;
14226 }
Eric Christopherfd179292009-08-27 18:07:15 +000014227 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014228 }
14229 }
Eric Christopherfd179292009-08-27 18:07:15 +000014230
Evan Cheng56f582d2012-01-04 01:41:39 +000014231 // Canonicalize max and min:
14232 // (x > y) ? x : y -> (x >= y) ? x : y
14233 // (x < y) ? x : y -> (x <= y) ? x : y
14234 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14235 // the need for an extra compare
14236 // against zero. e.g.
14237 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14238 // subl %esi, %edi
14239 // testl %edi, %edi
14240 // movl $0, %eax
14241 // cmovgl %edi, %eax
14242 // =>
14243 // xorl %eax, %eax
14244 // subl %esi, $edi
14245 // cmovsl %eax, %edi
14246 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14247 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14248 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14249 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14250 switch (CC) {
14251 default: break;
14252 case ISD::SETLT:
14253 case ISD::SETGT: {
14254 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14255 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14256 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14257 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14258 }
14259 }
14260 }
14261
Nadav Rotemcc616562012-01-15 19:27:55 +000014262 // If we know that this node is legal then we know that it is going to be
14263 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14264 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14265 // to simplify previous instructions.
14266 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14267 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014268 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014269 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014270
14271 // Don't optimize vector selects that map to mask-registers.
14272 if (BitWidth == 1)
14273 return SDValue();
14274
Nadav Rotemcc616562012-01-15 19:27:55 +000014275 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14276 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14277
14278 APInt KnownZero, KnownOne;
14279 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14280 DCI.isBeforeLegalizeOps());
14281 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14282 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14283 DCI.CommitTargetLoweringOpt(TLO);
14284 }
14285
Dan Gohman475871a2008-07-27 21:46:04 +000014286 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014287}
14288
Michael Liao2a33cec2012-08-10 19:58:13 +000014289// Check whether a boolean test is testing a boolean value generated by
14290// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14291// code.
14292//
14293// Simplify the following patterns:
14294// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14295// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14296// to (Op EFLAGS Cond)
14297//
14298// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14299// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14300// to (Op EFLAGS !Cond)
14301//
14302// where Op could be BRCOND or CMOV.
14303//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014304static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014305 // Quit if not CMP and SUB with its value result used.
14306 if (Cmp.getOpcode() != X86ISD::CMP &&
14307 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14308 return SDValue();
14309
14310 // Quit if not used as a boolean value.
14311 if (CC != X86::COND_E && CC != X86::COND_NE)
14312 return SDValue();
14313
14314 // Check CMP operands. One of them should be 0 or 1 and the other should be
14315 // an SetCC or extended from it.
14316 SDValue Op1 = Cmp.getOperand(0);
14317 SDValue Op2 = Cmp.getOperand(1);
14318
14319 SDValue SetCC;
14320 const ConstantSDNode* C = 0;
14321 bool needOppositeCond = (CC == X86::COND_E);
14322
14323 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14324 SetCC = Op2;
14325 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14326 SetCC = Op1;
14327 else // Quit if all operands are not constants.
14328 return SDValue();
14329
14330 if (C->getZExtValue() == 1)
14331 needOppositeCond = !needOppositeCond;
14332 else if (C->getZExtValue() != 0)
14333 // Quit if the constant is neither 0 or 1.
14334 return SDValue();
14335
14336 // Skip 'zext' node.
14337 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14338 SetCC = SetCC.getOperand(0);
14339
Michael Liao7fdc66b2012-09-10 16:36:16 +000014340 switch (SetCC.getOpcode()) {
14341 case X86ISD::SETCC:
14342 // Set the condition code or opposite one if necessary.
14343 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14344 if (needOppositeCond)
14345 CC = X86::GetOppositeBranchCondition(CC);
14346 return SetCC.getOperand(1);
14347 case X86ISD::CMOV: {
14348 // Check whether false/true value has canonical one, i.e. 0 or 1.
14349 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14350 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14351 // Quit if true value is not a constant.
14352 if (!TVal)
14353 return SDValue();
14354 // Quit if false value is not a constant.
14355 if (!FVal) {
14356 // A special case for rdrand, where 0 is set if false cond is found.
14357 SDValue Op = SetCC.getOperand(0);
14358 if (Op.getOpcode() != X86ISD::RDRAND)
14359 return SDValue();
14360 }
14361 // Quit if false value is not the constant 0 or 1.
14362 bool FValIsFalse = true;
14363 if (FVal && FVal->getZExtValue() != 0) {
14364 if (FVal->getZExtValue() != 1)
14365 return SDValue();
14366 // If FVal is 1, opposite cond is needed.
14367 needOppositeCond = !needOppositeCond;
14368 FValIsFalse = false;
14369 }
14370 // Quit if TVal is not the constant opposite of FVal.
14371 if (FValIsFalse && TVal->getZExtValue() != 1)
14372 return SDValue();
14373 if (!FValIsFalse && TVal->getZExtValue() != 0)
14374 return SDValue();
14375 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14376 if (needOppositeCond)
14377 CC = X86::GetOppositeBranchCondition(CC);
14378 return SetCC.getOperand(3);
14379 }
14380 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014381
Michael Liao7fdc66b2012-09-10 16:36:16 +000014382 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014383}
14384
Chris Lattnerd1980a52009-03-12 06:52:53 +000014385/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14386static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014387 TargetLowering::DAGCombinerInfo &DCI,
14388 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014389 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014390
Chris Lattnerd1980a52009-03-12 06:52:53 +000014391 // If the flag operand isn't dead, don't touch this CMOV.
14392 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14393 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014394
Evan Chengb5a55d92011-05-24 01:48:22 +000014395 SDValue FalseOp = N->getOperand(0);
14396 SDValue TrueOp = N->getOperand(1);
14397 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14398 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014399
Evan Chengb5a55d92011-05-24 01:48:22 +000014400 if (CC == X86::COND_E || CC == X86::COND_NE) {
14401 switch (Cond.getOpcode()) {
14402 default: break;
14403 case X86ISD::BSR:
14404 case X86ISD::BSF:
14405 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14406 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14407 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14408 }
14409 }
14410
Michael Liao2a33cec2012-08-10 19:58:13 +000014411 SDValue Flags;
14412
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014413 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014414 if (Flags.getNode() &&
14415 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014416 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014417 SDValue Ops[] = { FalseOp, TrueOp,
14418 DAG.getConstant(CC, MVT::i8), Flags };
14419 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14420 Ops, array_lengthof(Ops));
14421 }
14422
Chris Lattnerd1980a52009-03-12 06:52:53 +000014423 // If this is a select between two integer constants, try to do some
14424 // optimizations. Note that the operands are ordered the opposite of SELECT
14425 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014426 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14427 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014428 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14429 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014430 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14431 CC = X86::GetOppositeBranchCondition(CC);
14432 std::swap(TrueC, FalseC);
14433 }
Eric Christopherfd179292009-08-27 18:07:15 +000014434
Chris Lattnerd1980a52009-03-12 06:52:53 +000014435 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014436 // This is efficient for any integer data type (including i8/i16) and
14437 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014438 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014439 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14440 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014441
Chris Lattnerd1980a52009-03-12 06:52:53 +000014442 // Zero extend the condition if needed.
14443 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014444
Chris Lattnerd1980a52009-03-12 06:52:53 +000014445 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14446 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014447 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014448 if (N->getNumValues() == 2) // Dead flag value?
14449 return DCI.CombineTo(N, Cond, SDValue());
14450 return Cond;
14451 }
Eric Christopherfd179292009-08-27 18:07:15 +000014452
Chris Lattnercee56e72009-03-13 05:53:31 +000014453 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14454 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014455 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014456 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14457 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014458
Chris Lattner97a29a52009-03-13 05:22:11 +000014459 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014460 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14461 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014462 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14463 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014464
Chris Lattner97a29a52009-03-13 05:22:11 +000014465 if (N->getNumValues() == 2) // Dead flag value?
14466 return DCI.CombineTo(N, Cond, SDValue());
14467 return Cond;
14468 }
Eric Christopherfd179292009-08-27 18:07:15 +000014469
Chris Lattnercee56e72009-03-13 05:53:31 +000014470 // Optimize cases that will turn into an LEA instruction. This requires
14471 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014472 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014473 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014474 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014475
Chris Lattnercee56e72009-03-13 05:53:31 +000014476 bool isFastMultiplier = false;
14477 if (Diff < 10) {
14478 switch ((unsigned char)Diff) {
14479 default: break;
14480 case 1: // result = add base, cond
14481 case 2: // result = lea base( , cond*2)
14482 case 3: // result = lea base(cond, cond*2)
14483 case 4: // result = lea base( , cond*4)
14484 case 5: // result = lea base(cond, cond*4)
14485 case 8: // result = lea base( , cond*8)
14486 case 9: // result = lea base(cond, cond*8)
14487 isFastMultiplier = true;
14488 break;
14489 }
14490 }
Eric Christopherfd179292009-08-27 18:07:15 +000014491
Chris Lattnercee56e72009-03-13 05:53:31 +000014492 if (isFastMultiplier) {
14493 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014494 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14495 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014496 // Zero extend the condition if needed.
14497 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14498 Cond);
14499 // Scale the condition by the difference.
14500 if (Diff != 1)
14501 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14502 DAG.getConstant(Diff, Cond.getValueType()));
14503
14504 // Add the base if non-zero.
14505 if (FalseC->getAPIntValue() != 0)
14506 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14507 SDValue(FalseC, 0));
14508 if (N->getNumValues() == 2) // Dead flag value?
14509 return DCI.CombineTo(N, Cond, SDValue());
14510 return Cond;
14511 }
Eric Christopherfd179292009-08-27 18:07:15 +000014512 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014513 }
14514 }
14515 return SDValue();
14516}
14517
14518
Evan Cheng0b0cd912009-03-28 05:57:29 +000014519/// PerformMulCombine - Optimize a single multiply with constant into two
14520/// in order to implement it with two cheaper instructions, e.g.
14521/// LEA + SHL, LEA + LEA.
14522static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14523 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014524 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14525 return SDValue();
14526
Owen Andersone50ed302009-08-10 22:56:29 +000014527 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014528 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014529 return SDValue();
14530
14531 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14532 if (!C)
14533 return SDValue();
14534 uint64_t MulAmt = C->getZExtValue();
14535 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14536 return SDValue();
14537
14538 uint64_t MulAmt1 = 0;
14539 uint64_t MulAmt2 = 0;
14540 if ((MulAmt % 9) == 0) {
14541 MulAmt1 = 9;
14542 MulAmt2 = MulAmt / 9;
14543 } else if ((MulAmt % 5) == 0) {
14544 MulAmt1 = 5;
14545 MulAmt2 = MulAmt / 5;
14546 } else if ((MulAmt % 3) == 0) {
14547 MulAmt1 = 3;
14548 MulAmt2 = MulAmt / 3;
14549 }
14550 if (MulAmt2 &&
14551 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14552 DebugLoc DL = N->getDebugLoc();
14553
14554 if (isPowerOf2_64(MulAmt2) &&
14555 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14556 // If second multiplifer is pow2, issue it first. We want the multiply by
14557 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14558 // is an add.
14559 std::swap(MulAmt1, MulAmt2);
14560
14561 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014562 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014563 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014564 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014565 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014566 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014567 DAG.getConstant(MulAmt1, VT));
14568
Eric Christopherfd179292009-08-27 18:07:15 +000014569 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014570 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014571 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014572 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014573 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014574 DAG.getConstant(MulAmt2, VT));
14575
14576 // Do not add new nodes to DAG combiner worklist.
14577 DCI.CombineTo(N, NewMul, false);
14578 }
14579 return SDValue();
14580}
14581
Evan Chengad9c0a32009-12-15 00:53:42 +000014582static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14583 SDValue N0 = N->getOperand(0);
14584 SDValue N1 = N->getOperand(1);
14585 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14586 EVT VT = N0.getValueType();
14587
14588 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14589 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014590 if (VT.isInteger() && !VT.isVector() &&
14591 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014592 N0.getOperand(1).getOpcode() == ISD::Constant) {
14593 SDValue N00 = N0.getOperand(0);
14594 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14595 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14596 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14597 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14598 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14599 APInt ShAmt = N1C->getAPIntValue();
14600 Mask = Mask.shl(ShAmt);
14601 if (Mask != 0)
14602 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14603 N00, DAG.getConstant(Mask, VT));
14604 }
14605 }
14606
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014607
14608 // Hardware support for vector shifts is sparse which makes us scalarize the
14609 // vector operations in many cases. Also, on sandybridge ADD is faster than
14610 // shl.
14611 // (shl V, 1) -> add V,V
14612 if (isSplatVector(N1.getNode())) {
14613 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14614 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14615 // We shift all of the values by one. In many cases we do not have
14616 // hardware support for this operation. This is better expressed as an ADD
14617 // of two values.
14618 if (N1C && (1 == N1C->getZExtValue())) {
14619 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14620 }
14621 }
14622
Evan Chengad9c0a32009-12-15 00:53:42 +000014623 return SDValue();
14624}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014625
Nate Begeman740ab032009-01-26 00:52:55 +000014626/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14627/// when possible.
14628static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014629 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014630 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014631 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014632 if (N->getOpcode() == ISD::SHL) {
14633 SDValue V = PerformSHLCombine(N, DAG);
14634 if (V.getNode()) return V;
14635 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014636
Nate Begeman740ab032009-01-26 00:52:55 +000014637 // On X86 with SSE2 support, we can transform this to a vector shift if
14638 // all elements are shifted by the same amount. We can't do this in legalize
14639 // because the a constant vector is typically transformed to a constant pool
14640 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014641 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014642 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014643
Craig Topper7be5dfd2011-11-12 09:58:49 +000014644 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14645 (!Subtarget->hasAVX2() ||
14646 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014647 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014648
Mon P Wang3becd092009-01-28 08:12:05 +000014649 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014650 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014651 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014652 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014653 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14654 unsigned NumElts = VT.getVectorNumElements();
14655 unsigned i = 0;
14656 for (; i != NumElts; ++i) {
14657 SDValue Arg = ShAmtOp.getOperand(i);
14658 if (Arg.getOpcode() == ISD::UNDEF) continue;
14659 BaseShAmt = Arg;
14660 break;
14661 }
Craig Topper37c26772012-01-17 04:44:50 +000014662 // Handle the case where the build_vector is all undef
14663 // FIXME: Should DAG allow this?
14664 if (i == NumElts)
14665 return SDValue();
14666
Mon P Wang3becd092009-01-28 08:12:05 +000014667 for (; i != NumElts; ++i) {
14668 SDValue Arg = ShAmtOp.getOperand(i);
14669 if (Arg.getOpcode() == ISD::UNDEF) continue;
14670 if (Arg != BaseShAmt) {
14671 return SDValue();
14672 }
14673 }
14674 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014675 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014676 SDValue InVec = ShAmtOp.getOperand(0);
14677 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14678 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14679 unsigned i = 0;
14680 for (; i != NumElts; ++i) {
14681 SDValue Arg = InVec.getOperand(i);
14682 if (Arg.getOpcode() == ISD::UNDEF) continue;
14683 BaseShAmt = Arg;
14684 break;
14685 }
14686 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14687 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014688 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014689 if (C->getZExtValue() == SplatIdx)
14690 BaseShAmt = InVec.getOperand(1);
14691 }
14692 }
Mon P Wang845b1892012-02-01 22:15:20 +000014693 if (BaseShAmt.getNode() == 0) {
14694 // Don't create instructions with illegal types after legalize
14695 // types has run.
14696 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14697 !DCI.isBeforeLegalize())
14698 return SDValue();
14699
Mon P Wangefa42202009-09-03 19:56:25 +000014700 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14701 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014702 }
Mon P Wang3becd092009-01-28 08:12:05 +000014703 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014704 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014705
Mon P Wangefa42202009-09-03 19:56:25 +000014706 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014707 if (EltVT.bitsGT(MVT::i32))
14708 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14709 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014710 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014711
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014712 // The shift amount is identical so we can do a vector shift.
14713 SDValue ValOp = N->getOperand(0);
14714 switch (N->getOpcode()) {
14715 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014716 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014717 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014718 switch (VT.getSimpleVT().SimpleTy) {
14719 default: return SDValue();
14720 case MVT::v2i64:
14721 case MVT::v4i32:
14722 case MVT::v8i16:
14723 case MVT::v4i64:
14724 case MVT::v8i32:
14725 case MVT::v16i16:
14726 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14727 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014728 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014729 switch (VT.getSimpleVT().SimpleTy) {
14730 default: return SDValue();
14731 case MVT::v4i32:
14732 case MVT::v8i16:
14733 case MVT::v8i32:
14734 case MVT::v16i16:
14735 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14736 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014737 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014738 switch (VT.getSimpleVT().SimpleTy) {
14739 default: return SDValue();
14740 case MVT::v2i64:
14741 case MVT::v4i32:
14742 case MVT::v8i16:
14743 case MVT::v4i64:
14744 case MVT::v8i32:
14745 case MVT::v16i16:
14746 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14747 }
Nate Begeman740ab032009-01-26 00:52:55 +000014748 }
Nate Begeman740ab032009-01-26 00:52:55 +000014749}
14750
Nate Begemanb65c1752010-12-17 22:55:37 +000014751
Stuart Hastings865f0932011-06-03 23:53:54 +000014752// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14753// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14754// and friends. Likewise for OR -> CMPNEQSS.
14755static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14756 TargetLowering::DAGCombinerInfo &DCI,
14757 const X86Subtarget *Subtarget) {
14758 unsigned opcode;
14759
14760 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14761 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014762 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014763 SDValue N0 = N->getOperand(0);
14764 SDValue N1 = N->getOperand(1);
14765 SDValue CMP0 = N0->getOperand(1);
14766 SDValue CMP1 = N1->getOperand(1);
14767 DebugLoc DL = N->getDebugLoc();
14768
14769 // The SETCCs should both refer to the same CMP.
14770 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14771 return SDValue();
14772
14773 SDValue CMP00 = CMP0->getOperand(0);
14774 SDValue CMP01 = CMP0->getOperand(1);
14775 EVT VT = CMP00.getValueType();
14776
14777 if (VT == MVT::f32 || VT == MVT::f64) {
14778 bool ExpectingFlags = false;
14779 // Check for any users that want flags:
14780 for (SDNode::use_iterator UI = N->use_begin(),
14781 UE = N->use_end();
14782 !ExpectingFlags && UI != UE; ++UI)
14783 switch (UI->getOpcode()) {
14784 default:
14785 case ISD::BR_CC:
14786 case ISD::BRCOND:
14787 case ISD::SELECT:
14788 ExpectingFlags = true;
14789 break;
14790 case ISD::CopyToReg:
14791 case ISD::SIGN_EXTEND:
14792 case ISD::ZERO_EXTEND:
14793 case ISD::ANY_EXTEND:
14794 break;
14795 }
14796
14797 if (!ExpectingFlags) {
14798 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14799 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14800
14801 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14802 X86::CondCode tmp = cc0;
14803 cc0 = cc1;
14804 cc1 = tmp;
14805 }
14806
14807 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14808 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14809 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14810 X86ISD::NodeType NTOperator = is64BitFP ?
14811 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14812 // FIXME: need symbolic constants for these magic numbers.
14813 // See X86ATTInstPrinter.cpp:printSSECC().
14814 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14815 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14816 DAG.getConstant(x86cc, MVT::i8));
14817 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14818 OnesOrZeroesF);
14819 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14820 DAG.getConstant(1, MVT::i32));
14821 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14822 return OneBitOfTruth;
14823 }
14824 }
14825 }
14826 }
14827 return SDValue();
14828}
14829
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014830/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14831/// so it can be folded inside ANDNP.
14832static bool CanFoldXORWithAllOnes(const SDNode *N) {
14833 EVT VT = N->getValueType(0);
14834
14835 // Match direct AllOnes for 128 and 256-bit vectors
14836 if (ISD::isBuildVectorAllOnes(N))
14837 return true;
14838
14839 // Look through a bit convert.
14840 if (N->getOpcode() == ISD::BITCAST)
14841 N = N->getOperand(0).getNode();
14842
14843 // Sometimes the operand may come from a insert_subvector building a 256-bit
14844 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014845 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014846 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14847 SDValue V1 = N->getOperand(0);
14848 SDValue V2 = N->getOperand(1);
14849
14850 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14851 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14852 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14853 ISD::isBuildVectorAllOnes(V2.getNode()))
14854 return true;
14855 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014856
14857 return false;
14858}
14859
Nate Begemanb65c1752010-12-17 22:55:37 +000014860static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14861 TargetLowering::DAGCombinerInfo &DCI,
14862 const X86Subtarget *Subtarget) {
14863 if (DCI.isBeforeLegalizeOps())
14864 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014865
Stuart Hastings865f0932011-06-03 23:53:54 +000014866 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14867 if (R.getNode())
14868 return R;
14869
Craig Topper54a11172011-10-14 07:06:56 +000014870 EVT VT = N->getValueType(0);
14871
Craig Topperb4c94572011-10-21 06:55:01 +000014872 // Create ANDN, BLSI, and BLSR instructions
14873 // BLSI is X & (-X)
14874 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014875 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14876 SDValue N0 = N->getOperand(0);
14877 SDValue N1 = N->getOperand(1);
14878 DebugLoc DL = N->getDebugLoc();
14879
14880 // Check LHS for not
14881 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14882 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14883 // Check RHS for not
14884 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14885 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14886
Craig Topperb4c94572011-10-21 06:55:01 +000014887 // Check LHS for neg
14888 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14889 isZero(N0.getOperand(0)))
14890 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14891
14892 // Check RHS for neg
14893 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14894 isZero(N1.getOperand(0)))
14895 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14896
14897 // Check LHS for X-1
14898 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14899 isAllOnes(N0.getOperand(1)))
14900 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14901
14902 // Check RHS for X-1
14903 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14904 isAllOnes(N1.getOperand(1)))
14905 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14906
Craig Topper54a11172011-10-14 07:06:56 +000014907 return SDValue();
14908 }
14909
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014910 // Want to form ANDNP nodes:
14911 // 1) In the hopes of then easily combining them with OR and AND nodes
14912 // to form PBLEND/PSIGN.
14913 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014914 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014915 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014916
Nate Begemanb65c1752010-12-17 22:55:37 +000014917 SDValue N0 = N->getOperand(0);
14918 SDValue N1 = N->getOperand(1);
14919 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014920
Nate Begemanb65c1752010-12-17 22:55:37 +000014921 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014922 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014923 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14924 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014925 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014926
14927 // Check RHS for vnot
14928 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014929 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14930 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014931 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014932
Nate Begemanb65c1752010-12-17 22:55:37 +000014933 return SDValue();
14934}
14935
Evan Cheng760d1942010-01-04 21:22:48 +000014936static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014937 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014938 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014939 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014940 return SDValue();
14941
Stuart Hastings865f0932011-06-03 23:53:54 +000014942 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14943 if (R.getNode())
14944 return R;
14945
Evan Cheng760d1942010-01-04 21:22:48 +000014946 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014947
Evan Cheng760d1942010-01-04 21:22:48 +000014948 SDValue N0 = N->getOperand(0);
14949 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014950
Nate Begemanb65c1752010-12-17 22:55:37 +000014951 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014952 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014953 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014954 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14955 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014956
Craig Topper1666cb62011-11-19 07:07:26 +000014957 // Canonicalize pandn to RHS
14958 if (N0.getOpcode() == X86ISD::ANDNP)
14959 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014960 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014961 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14962 SDValue Mask = N1.getOperand(0);
14963 SDValue X = N1.getOperand(1);
14964 SDValue Y;
14965 if (N0.getOperand(0) == Mask)
14966 Y = N0.getOperand(1);
14967 if (N0.getOperand(1) == Mask)
14968 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014969
Craig Topper1666cb62011-11-19 07:07:26 +000014970 // Check to see if the mask appeared in both the AND and ANDNP and
14971 if (!Y.getNode())
14972 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014973
Craig Topper1666cb62011-11-19 07:07:26 +000014974 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014975 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014976 if (Mask.getOpcode() == ISD::BITCAST)
14977 Mask = Mask.getOperand(0);
14978 if (X.getOpcode() == ISD::BITCAST)
14979 X = X.getOperand(0);
14980 if (Y.getOpcode() == ISD::BITCAST)
14981 Y = Y.getOperand(0);
14982
Craig Topper1666cb62011-11-19 07:07:26 +000014983 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014984
Craig Toppered2e13d2012-01-22 19:15:14 +000014985 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014986 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14987 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014988 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014989 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014990
14991 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014992 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014993 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14994 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14995 if ((SraAmt + 1) != EltBits)
14996 return SDValue();
14997
14998 DebugLoc DL = N->getDebugLoc();
14999
15000 // Now we know we at least have a plendvb with the mask val. See if
15001 // we can form a psignb/w/d.
15002 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000015003 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
15004 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000015005 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
15006 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
15007 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000015008 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000015009 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000015010 }
15011 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000015012 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000015013 return SDValue();
15014
15015 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
15016
15017 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
15018 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
15019 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000015020 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000015021 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000015022 }
15023 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015024
Craig Topper1666cb62011-11-19 07:07:26 +000015025 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
15026 return SDValue();
15027
Nate Begemanb65c1752010-12-17 22:55:37 +000015028 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000015029 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
15030 std::swap(N0, N1);
15031 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
15032 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000015033 if (!N0.hasOneUse() || !N1.hasOneUse())
15034 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000015035
15036 SDValue ShAmt0 = N0.getOperand(1);
15037 if (ShAmt0.getValueType() != MVT::i8)
15038 return SDValue();
15039 SDValue ShAmt1 = N1.getOperand(1);
15040 if (ShAmt1.getValueType() != MVT::i8)
15041 return SDValue();
15042 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15043 ShAmt0 = ShAmt0.getOperand(0);
15044 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15045 ShAmt1 = ShAmt1.getOperand(0);
15046
15047 DebugLoc DL = N->getDebugLoc();
15048 unsigned Opc = X86ISD::SHLD;
15049 SDValue Op0 = N0.getOperand(0);
15050 SDValue Op1 = N1.getOperand(0);
15051 if (ShAmt0.getOpcode() == ISD::SUB) {
15052 Opc = X86ISD::SHRD;
15053 std::swap(Op0, Op1);
15054 std::swap(ShAmt0, ShAmt1);
15055 }
15056
Evan Cheng8b1190a2010-04-28 01:18:01 +000015057 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015058 if (ShAmt1.getOpcode() == ISD::SUB) {
15059 SDValue Sum = ShAmt1.getOperand(0);
15060 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015061 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15062 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15063 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15064 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015065 return DAG.getNode(Opc, DL, VT,
15066 Op0, Op1,
15067 DAG.getNode(ISD::TRUNCATE, DL,
15068 MVT::i8, ShAmt0));
15069 }
15070 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15071 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15072 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015073 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015074 return DAG.getNode(Opc, DL, VT,
15075 N0.getOperand(0), N1.getOperand(0),
15076 DAG.getNode(ISD::TRUNCATE, DL,
15077 MVT::i8, ShAmt0));
15078 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015079
Evan Cheng760d1942010-01-04 21:22:48 +000015080 return SDValue();
15081}
15082
Manman Ren92363622012-06-07 22:39:10 +000015083// Generate NEG and CMOV for integer abs.
15084static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15085 EVT VT = N->getValueType(0);
15086
15087 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15088 // 8-bit integer abs to NEG and CMOV.
15089 if (VT.isInteger() && VT.getSizeInBits() == 8)
15090 return SDValue();
15091
15092 SDValue N0 = N->getOperand(0);
15093 SDValue N1 = N->getOperand(1);
15094 DebugLoc DL = N->getDebugLoc();
15095
15096 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15097 // and change it to SUB and CMOV.
15098 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15099 N0.getOpcode() == ISD::ADD &&
15100 N0.getOperand(1) == N1 &&
15101 N1.getOpcode() == ISD::SRA &&
15102 N1.getOperand(0) == N0.getOperand(0))
15103 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15104 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15105 // Generate SUB & CMOV.
15106 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15107 DAG.getConstant(0, VT), N0.getOperand(0));
15108
15109 SDValue Ops[] = { N0.getOperand(0), Neg,
15110 DAG.getConstant(X86::COND_GE, MVT::i8),
15111 SDValue(Neg.getNode(), 1) };
15112 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15113 Ops, array_lengthof(Ops));
15114 }
15115 return SDValue();
15116}
15117
Craig Topper3738ccd2011-12-27 06:27:23 +000015118// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015119static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15120 TargetLowering::DAGCombinerInfo &DCI,
15121 const X86Subtarget *Subtarget) {
15122 if (DCI.isBeforeLegalizeOps())
15123 return SDValue();
15124
Manman Ren45d53b82012-06-08 18:58:26 +000015125 if (Subtarget->hasCMov()) {
15126 SDValue RV = performIntegerAbsCombine(N, DAG);
15127 if (RV.getNode())
15128 return RV;
15129 }
Manman Ren92363622012-06-07 22:39:10 +000015130
15131 // Try forming BMI if it is available.
15132 if (!Subtarget->hasBMI())
15133 return SDValue();
15134
Craig Topperb4c94572011-10-21 06:55:01 +000015135 EVT VT = N->getValueType(0);
15136
15137 if (VT != MVT::i32 && VT != MVT::i64)
15138 return SDValue();
15139
Craig Topper3738ccd2011-12-27 06:27:23 +000015140 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15141
Craig Topperb4c94572011-10-21 06:55:01 +000015142 // Create BLSMSK instructions by finding X ^ (X-1)
15143 SDValue N0 = N->getOperand(0);
15144 SDValue N1 = N->getOperand(1);
15145 DebugLoc DL = N->getDebugLoc();
15146
15147 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15148 isAllOnes(N0.getOperand(1)))
15149 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15150
15151 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15152 isAllOnes(N1.getOperand(1)))
15153 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15154
15155 return SDValue();
15156}
15157
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015158/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15159static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015160 TargetLowering::DAGCombinerInfo &DCI,
15161 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015162 LoadSDNode *Ld = cast<LoadSDNode>(N);
15163 EVT RegVT = Ld->getValueType(0);
15164 EVT MemVT = Ld->getMemoryVT();
15165 DebugLoc dl = Ld->getDebugLoc();
15166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15167
15168 ISD::LoadExtType Ext = Ld->getExtensionType();
15169
Nadav Rotemca6f2962011-09-18 19:00:23 +000015170 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015171 // shuffle. We need SSE4 for the shuffles.
15172 // TODO: It is possible to support ZExt by zeroing the undef values
15173 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015174 if (RegVT.isVector() && RegVT.isInteger() &&
15175 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015176 assert(MemVT != RegVT && "Cannot extend to the same type");
15177 assert(MemVT.isVector() && "Must load a vector from memory");
15178
15179 unsigned NumElems = RegVT.getVectorNumElements();
15180 unsigned RegSz = RegVT.getSizeInBits();
15181 unsigned MemSz = MemVT.getSizeInBits();
15182 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015183
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015184 // All sizes must be a power of two.
15185 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15186 return SDValue();
15187
15188 // Attempt to load the original value using scalar loads.
15189 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015190 MVT SclrLoadTy = MVT::i8;
15191 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15192 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15193 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015194 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015195 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015196 }
15197 }
15198
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015199 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15200 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15201 (64 <= MemSz))
15202 SclrLoadTy = MVT::f64;
15203
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015204 // Calculate the number of scalar loads that we need to perform
15205 // in order to load our vector from memory.
15206 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015207
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015208 // Represent our vector as a sequence of elements which are the
15209 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015210 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15211 RegSz/SclrLoadTy.getSizeInBits());
15212
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015213 // Represent the data using the same element type that is stored in
15214 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015215 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15216 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015217
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015218 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15219 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015220
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015221 // We can't shuffle using an illegal type.
15222 if (!TLI.isTypeLegal(WideVecVT))
15223 return SDValue();
15224
15225 SmallVector<SDValue, 8> Chains;
15226 SDValue Ptr = Ld->getBasePtr();
15227 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15228 TLI.getPointerTy());
15229 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15230
15231 for (unsigned i = 0; i < NumLoads; ++i) {
15232 // Perform a single load.
15233 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15234 Ptr, Ld->getPointerInfo(),
15235 Ld->isVolatile(), Ld->isNonTemporal(),
15236 Ld->isInvariant(), Ld->getAlignment());
15237 Chains.push_back(ScalarLoad.getValue(1));
15238 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15239 // another round of DAGCombining.
15240 if (i == 0)
15241 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15242 else
15243 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15244 ScalarLoad, DAG.getIntPtrConstant(i));
15245
15246 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15247 }
15248
15249 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15250 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015251
15252 // Bitcast the loaded value to a vector of the original element type, in
15253 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015254 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015255 unsigned SizeRatio = RegSz/MemSz;
15256
15257 // Redistribute the loaded elements into the different locations.
15258 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015259 for (unsigned i = 0; i != NumElems; ++i)
15260 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015261
15262 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015263 DAG.getUNDEF(WideVecVT),
15264 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015265
15266 // Bitcast to the requested type.
15267 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15268 // Replace the original load with the new sequence
15269 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015270 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015271 }
15272
15273 return SDValue();
15274}
15275
Chris Lattner149a4e52008-02-22 02:09:43 +000015276/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015277static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015278 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015279 StoreSDNode *St = cast<StoreSDNode>(N);
15280 EVT VT = St->getValue().getValueType();
15281 EVT StVT = St->getMemoryVT();
15282 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015283 SDValue StoredVal = St->getOperand(1);
15284 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15285
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015286 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015287 // On Sandy Bridge, 256-bit memory operations are executed by two
15288 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15289 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015290 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015291 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15292 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015293 SDValue Value0 = StoredVal.getOperand(0);
15294 SDValue Value1 = StoredVal.getOperand(1);
15295
15296 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15297 SDValue Ptr0 = St->getBasePtr();
15298 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15299
15300 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15301 St->getPointerInfo(), St->isVolatile(),
15302 St->isNonTemporal(), St->getAlignment());
15303 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15304 St->getPointerInfo(), St->isVolatile(),
15305 St->isNonTemporal(), St->getAlignment());
15306 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15307 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015308
15309 // Optimize trunc store (of multiple scalars) to shuffle and store.
15310 // First, pack all of the elements in one place. Next, store to memory
15311 // in fewer chunks.
15312 if (St->isTruncatingStore() && VT.isVector()) {
15313 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15314 unsigned NumElems = VT.getVectorNumElements();
15315 assert(StVT != VT && "Cannot truncate to the same type");
15316 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15317 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15318
15319 // From, To sizes and ElemCount must be pow of two
15320 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015321 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015322 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015323 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015324
Nadav Rotem614061b2011-08-10 19:30:14 +000015325 unsigned SizeRatio = FromSz / ToSz;
15326
15327 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15328
15329 // Create a type on which we perform the shuffle
15330 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15331 StVT.getScalarType(), NumElems*SizeRatio);
15332
15333 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15334
15335 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15336 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015337 for (unsigned i = 0; i != NumElems; ++i)
15338 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015339
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015340 // Can't shuffle using an illegal type.
15341 if (!TLI.isTypeLegal(WideVecVT))
15342 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015343
15344 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015345 DAG.getUNDEF(WideVecVT),
15346 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015347 // At this point all of the data is stored at the bottom of the
15348 // register. We now need to save it to mem.
15349
15350 // Find the largest store unit
15351 MVT StoreType = MVT::i8;
15352 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15353 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15354 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015355 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015356 StoreType = Tp;
15357 }
15358
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015359 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15360 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15361 (64 <= NumElems * ToSz))
15362 StoreType = MVT::f64;
15363
Nadav Rotem614061b2011-08-10 19:30:14 +000015364 // Bitcast the original vector into a vector of store-size units
15365 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015366 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015367 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15368 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15369 SmallVector<SDValue, 8> Chains;
15370 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15371 TLI.getPointerTy());
15372 SDValue Ptr = St->getBasePtr();
15373
15374 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015375 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015376 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15377 StoreType, ShuffWide,
15378 DAG.getIntPtrConstant(i));
15379 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15380 St->getPointerInfo(), St->isVolatile(),
15381 St->isNonTemporal(), St->getAlignment());
15382 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15383 Chains.push_back(Ch);
15384 }
15385
15386 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15387 Chains.size());
15388 }
15389
15390
Chris Lattner149a4e52008-02-22 02:09:43 +000015391 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15392 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015393 // A preferable solution to the general problem is to figure out the right
15394 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015395
15396 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015397 if (VT.getSizeInBits() != 64)
15398 return SDValue();
15399
Devang Patel578efa92009-06-05 21:57:13 +000015400 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling67658342012-10-09 07:45:08 +000015401 bool NoImplicitFloatOps = F->getFnAttributes().
15402 hasAttribute(Attributes::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015403 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015404 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015405 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015406 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015407 isa<LoadSDNode>(St->getValue()) &&
15408 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15409 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015410 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015411 LoadSDNode *Ld = 0;
15412 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015413 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015414 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015415 // Must be a store of a load. We currently handle two cases: the load
15416 // is a direct child, and it's under an intervening TokenFactor. It is
15417 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015418 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015419 Ld = cast<LoadSDNode>(St->getChain());
15420 else if (St->getValue().hasOneUse() &&
15421 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015422 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015423 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015424 TokenFactorIndex = i;
15425 Ld = cast<LoadSDNode>(St->getValue());
15426 } else
15427 Ops.push_back(ChainVal->getOperand(i));
15428 }
15429 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015430
Evan Cheng536e6672009-03-12 05:59:15 +000015431 if (!Ld || !ISD::isNormalLoad(Ld))
15432 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015433
Evan Cheng536e6672009-03-12 05:59:15 +000015434 // If this is not the MMX case, i.e. we are just turning i64 load/store
15435 // into f64 load/store, avoid the transformation if there are multiple
15436 // uses of the loaded value.
15437 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15438 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015439
Evan Cheng536e6672009-03-12 05:59:15 +000015440 DebugLoc LdDL = Ld->getDebugLoc();
15441 DebugLoc StDL = N->getDebugLoc();
15442 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15443 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15444 // pair instead.
15445 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015446 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015447 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15448 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015449 Ld->isNonTemporal(), Ld->isInvariant(),
15450 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015451 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015452 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015453 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015454 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015455 Ops.size());
15456 }
Evan Cheng536e6672009-03-12 05:59:15 +000015457 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015458 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015459 St->isVolatile(), St->isNonTemporal(),
15460 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015461 }
Evan Cheng536e6672009-03-12 05:59:15 +000015462
15463 // Otherwise, lower to two pairs of 32-bit loads / stores.
15464 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015465 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15466 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015467
Owen Anderson825b72b2009-08-11 20:47:22 +000015468 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015469 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015470 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015471 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015472 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015473 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015474 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015475 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015476 MinAlign(Ld->getAlignment(), 4));
15477
15478 SDValue NewChain = LoLd.getValue(1);
15479 if (TokenFactorIndex != -1) {
15480 Ops.push_back(LoLd);
15481 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015482 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015483 Ops.size());
15484 }
15485
15486 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015487 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15488 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015489
15490 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015491 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015492 St->isVolatile(), St->isNonTemporal(),
15493 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015494 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015495 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015496 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015497 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015498 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015499 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015500 }
Dan Gohman475871a2008-07-27 21:46:04 +000015501 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015502}
15503
Duncan Sands17470be2011-09-22 20:15:48 +000015504/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15505/// and return the operands for the horizontal operation in LHS and RHS. A
15506/// horizontal operation performs the binary operation on successive elements
15507/// of its first operand, then on successive elements of its second operand,
15508/// returning the resulting values in a vector. For example, if
15509/// A = < float a0, float a1, float a2, float a3 >
15510/// and
15511/// B = < float b0, float b1, float b2, float b3 >
15512/// then the result of doing a horizontal operation on A and B is
15513/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15514/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15515/// A horizontal-op B, for some already available A and B, and if so then LHS is
15516/// set to A, RHS to B, and the routine returns 'true'.
15517/// Note that the binary operation should have the property that if one of the
15518/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015519static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015520 // Look for the following pattern: if
15521 // A = < float a0, float a1, float a2, float a3 >
15522 // B = < float b0, float b1, float b2, float b3 >
15523 // and
15524 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15525 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15526 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15527 // which is A horizontal-op B.
15528
15529 // At least one of the operands should be a vector shuffle.
15530 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15531 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15532 return false;
15533
15534 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015535
15536 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15537 "Unsupported vector type for horizontal add/sub");
15538
15539 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15540 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015541 unsigned NumElts = VT.getVectorNumElements();
15542 unsigned NumLanes = VT.getSizeInBits()/128;
15543 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015544 assert((NumLaneElts % 2 == 0) &&
15545 "Vector type should have an even number of elements in each lane");
15546 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015547
15548 // View LHS in the form
15549 // LHS = VECTOR_SHUFFLE A, B, LMask
15550 // If LHS is not a shuffle then pretend it is the shuffle
15551 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15552 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15553 // type VT.
15554 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015555 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015556 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15557 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15558 A = LHS.getOperand(0);
15559 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15560 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015561 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15562 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015563 } else {
15564 if (LHS.getOpcode() != ISD::UNDEF)
15565 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015566 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015567 LMask[i] = i;
15568 }
15569
15570 // Likewise, view RHS in the form
15571 // RHS = VECTOR_SHUFFLE C, D, RMask
15572 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015573 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015574 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15575 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15576 C = RHS.getOperand(0);
15577 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15578 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015579 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15580 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015581 } else {
15582 if (RHS.getOpcode() != ISD::UNDEF)
15583 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015584 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015585 RMask[i] = i;
15586 }
15587
15588 // Check that the shuffles are both shuffling the same vectors.
15589 if (!(A == C && B == D) && !(A == D && B == C))
15590 return false;
15591
15592 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15593 if (!A.getNode() && !B.getNode())
15594 return false;
15595
15596 // If A and B occur in reverse order in RHS, then "swap" them (which means
15597 // rewriting the mask).
15598 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015599 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015600
15601 // At this point LHS and RHS are equivalent to
15602 // LHS = VECTOR_SHUFFLE A, B, LMask
15603 // RHS = VECTOR_SHUFFLE A, B, RMask
15604 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015605 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015606 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015607
Craig Topperf8363302011-12-02 08:18:41 +000015608 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015609 if (LIdx < 0 || RIdx < 0 ||
15610 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15611 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015612 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015613
Craig Topperf8363302011-12-02 08:18:41 +000015614 // Check that successive elements are being operated on. If not, this is
15615 // not a horizontal operation.
15616 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15617 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015618 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015619 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015620 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015621 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015622 }
15623
15624 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15625 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15626 return true;
15627}
15628
15629/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15630static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15631 const X86Subtarget *Subtarget) {
15632 EVT VT = N->getValueType(0);
15633 SDValue LHS = N->getOperand(0);
15634 SDValue RHS = N->getOperand(1);
15635
15636 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015637 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015638 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015639 isHorizontalBinOp(LHS, RHS, true))
15640 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15641 return SDValue();
15642}
15643
15644/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15645static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15646 const X86Subtarget *Subtarget) {
15647 EVT VT = N->getValueType(0);
15648 SDValue LHS = N->getOperand(0);
15649 SDValue RHS = N->getOperand(1);
15650
15651 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015652 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015653 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015654 isHorizontalBinOp(LHS, RHS, false))
15655 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15656 return SDValue();
15657}
15658
Chris Lattner6cf73262008-01-25 06:14:17 +000015659/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15660/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015661static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015662 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15663 // F[X]OR(0.0, x) -> x
15664 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015665 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15666 if (C->getValueAPF().isPosZero())
15667 return N->getOperand(1);
15668 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15669 if (C->getValueAPF().isPosZero())
15670 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015671 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015672}
15673
Nadav Rotemd60cb112012-08-19 13:06:16 +000015674/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15675/// X86ISD::FMAX nodes.
15676static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15677 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15678
15679 // Only perform optimizations if UnsafeMath is used.
15680 if (!DAG.getTarget().Options.UnsafeFPMath)
15681 return SDValue();
15682
15683 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015684 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015685 unsigned NewOp = 0;
15686 switch (N->getOpcode()) {
15687 default: llvm_unreachable("unknown opcode");
15688 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15689 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15690 }
15691
15692 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15693 N->getOperand(0), N->getOperand(1));
15694}
15695
15696
Chris Lattneraf723b92008-01-25 05:46:26 +000015697/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015698static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015699 // FAND(0.0, x) -> 0.0
15700 // FAND(x, 0.0) -> 0.0
15701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15702 if (C->getValueAPF().isPosZero())
15703 return N->getOperand(0);
15704 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15705 if (C->getValueAPF().isPosZero())
15706 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015707 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015708}
15709
Dan Gohmane5af2d32009-01-29 01:59:02 +000015710static SDValue PerformBTCombine(SDNode *N,
15711 SelectionDAG &DAG,
15712 TargetLowering::DAGCombinerInfo &DCI) {
15713 // BT ignores high bits in the bit index operand.
15714 SDValue Op1 = N->getOperand(1);
15715 if (Op1.hasOneUse()) {
15716 unsigned BitWidth = Op1.getValueSizeInBits();
15717 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15718 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015719 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15720 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015721 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015722 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15723 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15724 DCI.CommitTargetLoweringOpt(TLO);
15725 }
15726 return SDValue();
15727}
Chris Lattner83e6c992006-10-04 06:57:07 +000015728
Eli Friedman7a5e5552009-06-07 06:52:44 +000015729static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15730 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015731 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015732 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015733 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015734 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015735 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015736 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015737 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015738 }
15739 return SDValue();
15740}
15741
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015742static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15743 TargetLowering::DAGCombinerInfo &DCI,
15744 const X86Subtarget *Subtarget) {
15745 if (!DCI.isBeforeLegalizeOps())
15746 return SDValue();
15747
Craig Topper3ef43cf2012-04-24 06:36:35 +000015748 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015749 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015750
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015751 EVT VT = N->getValueType(0);
15752 SDValue Op = N->getOperand(0);
15753 EVT OpVT = Op.getValueType();
15754 DebugLoc dl = N->getDebugLoc();
15755
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015756 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15757 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015758
Craig Topper3ef43cf2012-04-24 06:36:35 +000015759 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015760 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015761
15762 // Optimize vectors in AVX mode
15763 // Sign extend v8i16 to v8i32 and
15764 // v4i32 to v4i64
15765 //
15766 // Divide input vector into two parts
15767 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15768 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15769 // concat the vectors to original VT
15770
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015771 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015772 SDValue Undef = DAG.getUNDEF(OpVT);
15773
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015774 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015775 for (unsigned i = 0; i != NumElems/2; ++i)
15776 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015777
Craig Toppercacafd42012-08-14 08:18:43 +000015778 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015779
15780 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015781 for (unsigned i = 0; i != NumElems/2; ++i)
15782 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015783
Craig Toppercacafd42012-08-14 08:18:43 +000015784 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015785
Craig Topper3ef43cf2012-04-24 06:36:35 +000015786 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015787 VT.getVectorNumElements()/2);
15788
Craig Topper3ef43cf2012-04-24 06:36:35 +000015789 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015790 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15791
15792 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15793 }
15794 return SDValue();
15795}
15796
Michael Liaof6c24ee2012-08-10 14:39:24 +000015797static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015798 const X86Subtarget* Subtarget) {
15799 DebugLoc dl = N->getDebugLoc();
15800 EVT VT = N->getValueType(0);
15801
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015802 // Let legalize expand this if it isn't a legal type yet.
15803 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15804 return SDValue();
15805
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015806 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015807 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15808 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015809 return SDValue();
15810
15811 SDValue A = N->getOperand(0);
15812 SDValue B = N->getOperand(1);
15813 SDValue C = N->getOperand(2);
15814
15815 bool NegA = (A.getOpcode() == ISD::FNEG);
15816 bool NegB = (B.getOpcode() == ISD::FNEG);
15817 bool NegC = (C.getOpcode() == ISD::FNEG);
15818
Michael Liaof6c24ee2012-08-10 14:39:24 +000015819 // Negative multiplication when NegA xor NegB
15820 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015821 if (NegA)
15822 A = A.getOperand(0);
15823 if (NegB)
15824 B = B.getOperand(0);
15825 if (NegC)
15826 C = C.getOperand(0);
15827
15828 unsigned Opcode;
15829 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015830 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015831 else
Craig Topperbf404372012-08-31 15:40:30 +000015832 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15833
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015834 return DAG.getNode(Opcode, dl, VT, A, B, C);
15835}
15836
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015837static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015838 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015839 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015840 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15841 // (and (i32 x86isd::setcc_carry), 1)
15842 // This eliminates the zext. This transformation is necessary because
15843 // ISD::SETCC is always legalized to i8.
15844 DebugLoc dl = N->getDebugLoc();
15845 SDValue N0 = N->getOperand(0);
15846 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015847 EVT OpVT = N0.getValueType();
15848
Evan Cheng2e489c42009-12-16 00:53:11 +000015849 if (N0.getOpcode() == ISD::AND &&
15850 N0.hasOneUse() &&
15851 N0.getOperand(0).hasOneUse()) {
15852 SDValue N00 = N0.getOperand(0);
15853 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15854 return SDValue();
15855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15856 if (!C || C->getZExtValue() != 1)
15857 return SDValue();
15858 return DAG.getNode(ISD::AND, dl, VT,
15859 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15860 N00.getOperand(0), N00.getOperand(1)),
15861 DAG.getConstant(1, VT));
15862 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015863
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015864 // Optimize vectors in AVX mode:
15865 //
15866 // v8i16 -> v8i32
15867 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15868 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15869 // Concat upper and lower parts.
15870 //
15871 // v4i32 -> v4i64
15872 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15873 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15874 // Concat upper and lower parts.
15875 //
Craig Topperc16f8512012-04-25 06:39:39 +000015876 if (!DCI.isBeforeLegalizeOps())
15877 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015878
Craig Topperc16f8512012-04-25 06:39:39 +000015879 if (!Subtarget->hasAVX())
15880 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015881
Craig Topperc16f8512012-04-25 06:39:39 +000015882 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15883 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015884
Craig Topperc16f8512012-04-25 06:39:39 +000015885 if (Subtarget->hasAVX2())
15886 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015887
Craig Topperc16f8512012-04-25 06:39:39 +000015888 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15889 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15890 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015891
Craig Topperc16f8512012-04-25 06:39:39 +000015892 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15893 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015894
Craig Topperc16f8512012-04-25 06:39:39 +000015895 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15896 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15897
15898 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015899 }
15900
Evan Cheng2e489c42009-12-16 00:53:11 +000015901 return SDValue();
15902}
15903
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015904// Optimize x == -y --> x+y == 0
15905// x != -y --> x+y != 0
15906static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15907 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15908 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015909 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015910
15911 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15912 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15913 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15914 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15915 LHS.getValueType(), RHS, LHS.getOperand(1));
15916 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15917 addV, DAG.getConstant(0, addV.getValueType()), CC);
15918 }
15919 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15920 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15921 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15922 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15923 RHS.getValueType(), LHS, RHS.getOperand(1));
15924 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15925 addV, DAG.getConstant(0, addV.getValueType()), CC);
15926 }
15927 return SDValue();
15928}
15929
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015930// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015931static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15932 TargetLowering::DAGCombinerInfo &DCI,
15933 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015934 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015935 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15936 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015937
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015938 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15939 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15940 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015941 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015942 return DAG.getNode(ISD::AND, DL, MVT::i8,
15943 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015944 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015945 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015946
Michael Liao2a33cec2012-08-10 19:58:13 +000015947 SDValue Flags;
15948
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015949 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15950 if (Flags.getNode()) {
15951 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15952 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15953 }
15954
Michael Liao2a33cec2012-08-10 19:58:13 +000015955 return SDValue();
15956}
15957
15958// Optimize branch condition evaluation.
15959//
15960static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15961 TargetLowering::DAGCombinerInfo &DCI,
15962 const X86Subtarget *Subtarget) {
15963 DebugLoc DL = N->getDebugLoc();
15964 SDValue Chain = N->getOperand(0);
15965 SDValue Dest = N->getOperand(1);
15966 SDValue EFLAGS = N->getOperand(3);
15967 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15968
15969 SDValue Flags;
15970
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015971 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15972 if (Flags.getNode()) {
15973 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15974 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15975 Flags);
15976 }
15977
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015978 return SDValue();
15979}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015980
Craig Topper7fd5e162012-04-24 06:02:29 +000015981static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015982 SDValue Op0 = N->getOperand(0);
15983 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015984
15985 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015986 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015987 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015988 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015989 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15990 // Notice that we use SINT_TO_FP because we know that the high bits
15991 // are zero and SINT_TO_FP is better supported by the hardware.
15992 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15993 }
15994
15995 return SDValue();
15996}
15997
Benjamin Kramer1396c402011-06-18 11:09:41 +000015998static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15999 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016000 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016001 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016002
16003 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000016004 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000016005 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016006 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016007 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
16008 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
16009 }
16010
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016011 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
16012 // a 32-bit target where SSE doesn't support i64->FP operations.
16013 if (Op0.getOpcode() == ISD::LOAD) {
16014 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
16015 EVT VT = Ld->getValueType(0);
16016 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
16017 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
16018 !XTLI->getSubtarget()->is64Bit() &&
16019 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000016020 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
16021 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016022 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
16023 return FILDChain;
16024 }
16025 }
16026 return SDValue();
16027}
16028
Craig Topper7fd5e162012-04-24 06:02:29 +000016029static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
16030 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000016031
16032 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000016033 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
16034 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000016035 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000016036 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
16037 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
16038 }
16039
16040 return SDValue();
16041}
16042
Chris Lattner23a01992010-12-20 01:37:09 +000016043// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16044static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16045 X86TargetLowering::DAGCombinerInfo &DCI) {
16046 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16047 // the result is either zero or one (depending on the input carry bit).
16048 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16049 if (X86::isZeroNode(N->getOperand(0)) &&
16050 X86::isZeroNode(N->getOperand(1)) &&
16051 // We don't have a good way to replace an EFLAGS use, so only do this when
16052 // dead right now.
16053 SDValue(N, 1).use_empty()) {
16054 DebugLoc DL = N->getDebugLoc();
16055 EVT VT = N->getValueType(0);
16056 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16057 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16058 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16059 DAG.getConstant(X86::COND_B,MVT::i8),
16060 N->getOperand(2)),
16061 DAG.getConstant(1, VT));
16062 return DCI.CombineTo(N, Res1, CarryOut);
16063 }
16064
16065 return SDValue();
16066}
16067
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016068// fold (add Y, (sete X, 0)) -> adc 0, Y
16069// (add Y, (setne X, 0)) -> sbb -1, Y
16070// (sub (sete X, 0), Y) -> sbb 0, Y
16071// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016072static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016073 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016074
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016075 // Look through ZExts.
16076 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16077 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16078 return SDValue();
16079
16080 SDValue SetCC = Ext.getOperand(0);
16081 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16082 return SDValue();
16083
16084 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16085 if (CC != X86::COND_E && CC != X86::COND_NE)
16086 return SDValue();
16087
16088 SDValue Cmp = SetCC.getOperand(1);
16089 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016090 !X86::isZeroNode(Cmp.getOperand(1)) ||
16091 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016092 return SDValue();
16093
16094 SDValue CmpOp0 = Cmp.getOperand(0);
16095 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16096 DAG.getConstant(1, CmpOp0.getValueType()));
16097
16098 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16099 if (CC == X86::COND_NE)
16100 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16101 DL, OtherVal.getValueType(), OtherVal,
16102 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16103 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16104 DL, OtherVal.getValueType(), OtherVal,
16105 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16106}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016107
Craig Topper54f952a2011-11-19 09:02:40 +000016108/// PerformADDCombine - Do target-specific dag combines on integer adds.
16109static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16110 const X86Subtarget *Subtarget) {
16111 EVT VT = N->getValueType(0);
16112 SDValue Op0 = N->getOperand(0);
16113 SDValue Op1 = N->getOperand(1);
16114
16115 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016116 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016117 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016118 isHorizontalBinOp(Op0, Op1, true))
16119 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16120
16121 return OptimizeConditionalInDecrement(N, DAG);
16122}
16123
16124static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16125 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016126 SDValue Op0 = N->getOperand(0);
16127 SDValue Op1 = N->getOperand(1);
16128
16129 // X86 can't encode an immediate LHS of a sub. See if we can push the
16130 // negation into a preceding instruction.
16131 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016132 // If the RHS of the sub is a XOR with one use and a constant, invert the
16133 // immediate. Then add one to the LHS of the sub so we can turn
16134 // X-Y -> X+~Y+1, saving one register.
16135 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16136 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016137 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016138 EVT VT = Op0.getValueType();
16139 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16140 Op1.getOperand(0),
16141 DAG.getConstant(~XorC, VT));
16142 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016143 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016144 }
16145 }
16146
Craig Topper54f952a2011-11-19 09:02:40 +000016147 // Try to synthesize horizontal adds from adds of shuffles.
16148 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016149 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016150 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16151 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016152 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16153
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016154 return OptimizeConditionalInDecrement(N, DAG);
16155}
16156
Dan Gohman475871a2008-07-27 21:46:04 +000016157SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016158 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016159 SelectionDAG &DAG = DCI.DAG;
16160 switch (N->getOpcode()) {
16161 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016162 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016163 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016164 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016165 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016166 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016167 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16168 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016169 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016170 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016171 case ISD::SHL:
16172 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016173 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016174 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016175 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016176 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016177 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016178 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016179 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016180 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016181 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016182 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16183 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016184 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016185 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016186 case X86ISD::FMIN:
16187 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016188 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016189 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016190 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016191 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016192 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016193 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016194 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016195 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016196 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016197 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016198 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016199 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016200 case X86ISD::UNPCKH:
16201 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016202 case X86ISD::MOVHLPS:
16203 case X86ISD::MOVLHPS:
16204 case X86ISD::PSHUFD:
16205 case X86ISD::PSHUFHW:
16206 case X86ISD::PSHUFLW:
16207 case X86ISD::MOVSS:
16208 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016209 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016210 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016211 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016212 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016213 }
16214
Dan Gohman475871a2008-07-27 21:46:04 +000016215 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016216}
16217
Evan Chenge5b51ac2010-04-17 06:13:15 +000016218/// isTypeDesirableForOp - Return true if the target has native support for
16219/// the specified value type and it is 'desirable' to use the type for the
16220/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16221/// instruction encodings are longer and some i16 instructions are slow.
16222bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16223 if (!isTypeLegal(VT))
16224 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016225 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016226 return true;
16227
16228 switch (Opc) {
16229 default:
16230 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016231 case ISD::LOAD:
16232 case ISD::SIGN_EXTEND:
16233 case ISD::ZERO_EXTEND:
16234 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016235 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016236 case ISD::SRL:
16237 case ISD::SUB:
16238 case ISD::ADD:
16239 case ISD::MUL:
16240 case ISD::AND:
16241 case ISD::OR:
16242 case ISD::XOR:
16243 return false;
16244 }
16245}
16246
16247/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016248/// beneficial for dag combiner to promote the specified node. If true, it
16249/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016250bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016251 EVT VT = Op.getValueType();
16252 if (VT != MVT::i16)
16253 return false;
16254
Evan Cheng4c26e932010-04-19 19:29:22 +000016255 bool Promote = false;
16256 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016257 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016258 default: break;
16259 case ISD::LOAD: {
16260 LoadSDNode *LD = cast<LoadSDNode>(Op);
16261 // If the non-extending load has a single use and it's not live out, then it
16262 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016263 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16264 Op.hasOneUse()*/) {
16265 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16266 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16267 // The only case where we'd want to promote LOAD (rather then it being
16268 // promoted as an operand is when it's only use is liveout.
16269 if (UI->getOpcode() != ISD::CopyToReg)
16270 return false;
16271 }
16272 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016273 Promote = true;
16274 break;
16275 }
16276 case ISD::SIGN_EXTEND:
16277 case ISD::ZERO_EXTEND:
16278 case ISD::ANY_EXTEND:
16279 Promote = true;
16280 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016281 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016282 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016283 SDValue N0 = Op.getOperand(0);
16284 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016285 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016286 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016287 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016288 break;
16289 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016290 case ISD::ADD:
16291 case ISD::MUL:
16292 case ISD::AND:
16293 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016294 case ISD::XOR:
16295 Commute = true;
16296 // fallthrough
16297 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016298 SDValue N0 = Op.getOperand(0);
16299 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016300 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016301 return false;
16302 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016303 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016304 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016305 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016306 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016307 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016308 }
16309 }
16310
16311 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016312 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016313}
16314
Evan Cheng60c07e12006-07-05 22:17:51 +000016315//===----------------------------------------------------------------------===//
16316// X86 Inline Assembly Support
16317//===----------------------------------------------------------------------===//
16318
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016319namespace {
16320 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016321 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016322 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016323
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016324 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016325 StringRef piece(*args[i]);
16326 if (!s.startswith(piece)) // Check if the piece matches.
16327 return false;
16328
16329 s = s.substr(piece.size());
16330 StringRef::size_type pos = s.find_first_not_of(" \t");
16331 if (pos == 0) // We matched a prefix.
16332 return false;
16333
16334 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016335 }
16336
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016337 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016338 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016339 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016340}
16341
Chris Lattnerb8105652009-07-20 17:51:36 +000016342bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16343 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016344
16345 std::string AsmStr = IA->getAsmString();
16346
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016347 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16348 if (!Ty || Ty->getBitWidth() % 16 != 0)
16349 return false;
16350
Chris Lattnerb8105652009-07-20 17:51:36 +000016351 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016352 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016353 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016354
16355 switch (AsmPieces.size()) {
16356 default: return false;
16357 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016358 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016359 // we will turn this bswap into something that will be lowered to logical
16360 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16361 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016362 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016363 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16364 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16365 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16366 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16367 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16368 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016369 // No need to check constraints, nothing other than the equivalent of
16370 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016371 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016372 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016373
Chris Lattnerb8105652009-07-20 17:51:36 +000016374 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016375 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016376 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016377 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16378 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016379 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016380 const std::string &ConstraintsStr = IA->getConstraintString();
16381 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016382 std::sort(AsmPieces.begin(), AsmPieces.end());
16383 if (AsmPieces.size() == 4 &&
16384 AsmPieces[0] == "~{cc}" &&
16385 AsmPieces[1] == "~{dirflag}" &&
16386 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016387 AsmPieces[3] == "~{fpsr}")
16388 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016389 }
16390 break;
16391 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016392 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016393 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016394 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16395 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16396 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016397 AsmPieces.clear();
16398 const std::string &ConstraintsStr = IA->getConstraintString();
16399 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16400 std::sort(AsmPieces.begin(), AsmPieces.end());
16401 if (AsmPieces.size() == 4 &&
16402 AsmPieces[0] == "~{cc}" &&
16403 AsmPieces[1] == "~{dirflag}" &&
16404 AsmPieces[2] == "~{flags}" &&
16405 AsmPieces[3] == "~{fpsr}")
16406 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016407 }
Evan Cheng55d42002011-01-08 01:24:27 +000016408
16409 if (CI->getType()->isIntegerTy(64)) {
16410 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16411 if (Constraints.size() >= 2 &&
16412 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16413 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16414 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016415 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16416 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16417 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016418 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016419 }
16420 }
16421 break;
16422 }
16423 return false;
16424}
16425
16426
16427
Chris Lattnerf4dff842006-07-11 02:54:03 +000016428/// getConstraintType - Given a constraint letter, return the type of
16429/// constraint it is for this target.
16430X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016431X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16432 if (Constraint.size() == 1) {
16433 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016434 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016435 case 'q':
16436 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016437 case 'f':
16438 case 't':
16439 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016440 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016441 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016442 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016443 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016444 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016445 case 'a':
16446 case 'b':
16447 case 'c':
16448 case 'd':
16449 case 'S':
16450 case 'D':
16451 case 'A':
16452 return C_Register;
16453 case 'I':
16454 case 'J':
16455 case 'K':
16456 case 'L':
16457 case 'M':
16458 case 'N':
16459 case 'G':
16460 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016461 case 'e':
16462 case 'Z':
16463 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016464 default:
16465 break;
16466 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016467 }
Chris Lattner4234f572007-03-25 02:14:49 +000016468 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016469}
16470
John Thompson44ab89e2010-10-29 17:29:13 +000016471/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016472/// This object must already have been set up with the operand type
16473/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016474TargetLowering::ConstraintWeight
16475 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016476 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016477 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016478 Value *CallOperandVal = info.CallOperandVal;
16479 // If we don't have a value, we can't do a match,
16480 // but allow it at the lowest weight.
16481 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016482 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016483 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016484 // Look at the constraint type.
16485 switch (*constraint) {
16486 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016487 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16488 case 'R':
16489 case 'q':
16490 case 'Q':
16491 case 'a':
16492 case 'b':
16493 case 'c':
16494 case 'd':
16495 case 'S':
16496 case 'D':
16497 case 'A':
16498 if (CallOperandVal->getType()->isIntegerTy())
16499 weight = CW_SpecificReg;
16500 break;
16501 case 'f':
16502 case 't':
16503 case 'u':
16504 if (type->isFloatingPointTy())
16505 weight = CW_SpecificReg;
16506 break;
16507 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016508 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016509 weight = CW_SpecificReg;
16510 break;
16511 case 'x':
16512 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016513 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016514 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016515 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016516 break;
16517 case 'I':
16518 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16519 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016520 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016521 }
16522 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016523 case 'J':
16524 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16525 if (C->getZExtValue() <= 63)
16526 weight = CW_Constant;
16527 }
16528 break;
16529 case 'K':
16530 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16531 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16532 weight = CW_Constant;
16533 }
16534 break;
16535 case 'L':
16536 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16537 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16538 weight = CW_Constant;
16539 }
16540 break;
16541 case 'M':
16542 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16543 if (C->getZExtValue() <= 3)
16544 weight = CW_Constant;
16545 }
16546 break;
16547 case 'N':
16548 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16549 if (C->getZExtValue() <= 0xff)
16550 weight = CW_Constant;
16551 }
16552 break;
16553 case 'G':
16554 case 'C':
16555 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16556 weight = CW_Constant;
16557 }
16558 break;
16559 case 'e':
16560 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16561 if ((C->getSExtValue() >= -0x80000000LL) &&
16562 (C->getSExtValue() <= 0x7fffffffLL))
16563 weight = CW_Constant;
16564 }
16565 break;
16566 case 'Z':
16567 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16568 if (C->getZExtValue() <= 0xffffffff)
16569 weight = CW_Constant;
16570 }
16571 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016572 }
16573 return weight;
16574}
16575
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016576/// LowerXConstraint - try to replace an X constraint, which matches anything,
16577/// with another that has more specific requirements based on the type of the
16578/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016579const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016580LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016581 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16582 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016583 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016584 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016585 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016586 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016587 return "x";
16588 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016589
Chris Lattner5e764232008-04-26 23:02:14 +000016590 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016591}
16592
Chris Lattner48884cd2007-08-25 00:47:38 +000016593/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16594/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016595void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016596 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016597 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016598 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016599 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016600
Eric Christopher100c8332011-06-02 23:16:42 +000016601 // Only support length 1 constraints for now.
16602 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016603
Eric Christopher100c8332011-06-02 23:16:42 +000016604 char ConstraintLetter = Constraint[0];
16605 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016606 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016607 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016608 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016609 if (C->getZExtValue() <= 31) {
16610 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016611 break;
16612 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016613 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016614 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016615 case 'J':
16616 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016617 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016618 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16619 break;
16620 }
16621 }
16622 return;
16623 case 'K':
16624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016625 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016626 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16627 break;
16628 }
16629 }
16630 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016631 case 'N':
16632 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016633 if (C->getZExtValue() <= 255) {
16634 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016635 break;
16636 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016637 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016638 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016639 case 'e': {
16640 // 32-bit signed value
16641 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016642 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16643 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016644 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016645 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016646 break;
16647 }
16648 // FIXME gcc accepts some relocatable values here too, but only in certain
16649 // memory models; it's complicated.
16650 }
16651 return;
16652 }
16653 case 'Z': {
16654 // 32-bit unsigned value
16655 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016656 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16657 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016658 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16659 break;
16660 }
16661 }
16662 // FIXME gcc accepts some relocatable values here too, but only in certain
16663 // memory models; it's complicated.
16664 return;
16665 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016666 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016667 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016668 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016669 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016670 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016671 break;
16672 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016673
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016674 // In any sort of PIC mode addresses need to be computed at runtime by
16675 // adding in a register or some sort of table lookup. These can't
16676 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016677 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016678 return;
16679
Chris Lattnerdc43a882007-05-03 16:52:29 +000016680 // If we are in non-pic codegen mode, we allow the address of a global (with
16681 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016682 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016683 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016684
Chris Lattner49921962009-05-08 18:23:14 +000016685 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16686 while (1) {
16687 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16688 Offset += GA->getOffset();
16689 break;
16690 } else if (Op.getOpcode() == ISD::ADD) {
16691 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16692 Offset += C->getZExtValue();
16693 Op = Op.getOperand(0);
16694 continue;
16695 }
16696 } else if (Op.getOpcode() == ISD::SUB) {
16697 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16698 Offset += -C->getZExtValue();
16699 Op = Op.getOperand(0);
16700 continue;
16701 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016702 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016703
Chris Lattner49921962009-05-08 18:23:14 +000016704 // Otherwise, this isn't something we can handle, reject it.
16705 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016706 }
Eric Christopherfd179292009-08-27 18:07:15 +000016707
Dan Gohman46510a72010-04-15 01:51:59 +000016708 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016709 // If we require an extra load to get this address, as in PIC mode, we
16710 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016711 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16712 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016713 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016714
Devang Patel0d881da2010-07-06 22:08:15 +000016715 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16716 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016717 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016718 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016719 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016720
Gabor Greifba36cb52008-08-28 21:40:38 +000016721 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016722 Ops.push_back(Result);
16723 return;
16724 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016725 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016726}
16727
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016728std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016729X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016730 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016731 // First, see if this is a constraint that directly corresponds to an LLVM
16732 // register class.
16733 if (Constraint.size() == 1) {
16734 // GCC Constraint Letters
16735 switch (Constraint[0]) {
16736 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016737 // TODO: Slight differences here in allocation order and leaving
16738 // RIP in the class. Do they matter any more here than they do
16739 // in the normal allocation?
16740 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16741 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016742 if (VT == MVT::i32 || VT == MVT::f32)
16743 return std::make_pair(0U, &X86::GR32RegClass);
16744 if (VT == MVT::i16)
16745 return std::make_pair(0U, &X86::GR16RegClass);
16746 if (VT == MVT::i8 || VT == MVT::i1)
16747 return std::make_pair(0U, &X86::GR8RegClass);
16748 if (VT == MVT::i64 || VT == MVT::f64)
16749 return std::make_pair(0U, &X86::GR64RegClass);
16750 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016751 }
16752 // 32-bit fallthrough
16753 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016754 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016755 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16756 if (VT == MVT::i16)
16757 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16758 if (VT == MVT::i8 || VT == MVT::i1)
16759 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16760 if (VT == MVT::i64)
16761 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016762 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016763 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016764 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016765 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016766 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016767 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016768 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016769 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016770 return std::make_pair(0U, &X86::GR32RegClass);
16771 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016772 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016773 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016774 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016775 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016776 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016777 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016778 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16779 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016780 case 'f': // FP Stack registers.
16781 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16782 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016783 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016784 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016785 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016786 return std::make_pair(0U, &X86::RFP64RegClass);
16787 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016788 case 'y': // MMX_REGS if MMX allowed.
16789 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016790 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016791 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016792 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016793 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016794 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016795 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016796
Owen Anderson825b72b2009-08-11 20:47:22 +000016797 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016798 default: break;
16799 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016800 case MVT::f32:
16801 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016802 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016803 case MVT::f64:
16804 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016805 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016806 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016807 case MVT::v16i8:
16808 case MVT::v8i16:
16809 case MVT::v4i32:
16810 case MVT::v2i64:
16811 case MVT::v4f32:
16812 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016813 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016814 // AVX types.
16815 case MVT::v32i8:
16816 case MVT::v16i16:
16817 case MVT::v8i32:
16818 case MVT::v4i64:
16819 case MVT::v8f32:
16820 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016821 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016822 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016823 break;
16824 }
16825 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016826
Chris Lattnerf76d1802006-07-31 23:26:50 +000016827 // Use the default implementation in TargetLowering to convert the register
16828 // constraint into a member of a register class.
16829 std::pair<unsigned, const TargetRegisterClass*> Res;
16830 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016831
16832 // Not found as a standard register?
16833 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016834 // Map st(0) -> st(7) -> ST0
16835 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16836 tolower(Constraint[1]) == 's' &&
16837 tolower(Constraint[2]) == 't' &&
16838 Constraint[3] == '(' &&
16839 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16840 Constraint[5] == ')' &&
16841 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016842
Chris Lattner56d77c72009-09-13 22:41:48 +000016843 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016844 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016845 return Res;
16846 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016847
Chris Lattner56d77c72009-09-13 22:41:48 +000016848 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016849 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016850 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016851 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016852 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016853 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016854
16855 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016856 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016857 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016858 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016859 return Res;
16860 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016861
Dale Johannesen330169f2008-11-13 21:52:36 +000016862 // 'A' means EAX + EDX.
16863 if (Constraint == "A") {
16864 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016865 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016866 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016867 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016868 return Res;
16869 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016870
Chris Lattnerf76d1802006-07-31 23:26:50 +000016871 // Otherwise, check to see if this is a register class of the wrong value
16872 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16873 // turn into {ax},{dx}.
16874 if (Res.second->hasType(VT))
16875 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016876
Chris Lattnerf76d1802006-07-31 23:26:50 +000016877 // All of the single-register GCC register classes map their values onto
16878 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16879 // really want an 8-bit or 32-bit register, map to the appropriate register
16880 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016881 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016882 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016883 unsigned DestReg = 0;
16884 switch (Res.first) {
16885 default: break;
16886 case X86::AX: DestReg = X86::AL; break;
16887 case X86::DX: DestReg = X86::DL; break;
16888 case X86::CX: DestReg = X86::CL; break;
16889 case X86::BX: DestReg = X86::BL; break;
16890 }
16891 if (DestReg) {
16892 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016893 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016894 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016895 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016896 unsigned DestReg = 0;
16897 switch (Res.first) {
16898 default: break;
16899 case X86::AX: DestReg = X86::EAX; break;
16900 case X86::DX: DestReg = X86::EDX; break;
16901 case X86::CX: DestReg = X86::ECX; break;
16902 case X86::BX: DestReg = X86::EBX; break;
16903 case X86::SI: DestReg = X86::ESI; break;
16904 case X86::DI: DestReg = X86::EDI; break;
16905 case X86::BP: DestReg = X86::EBP; break;
16906 case X86::SP: DestReg = X86::ESP; break;
16907 }
16908 if (DestReg) {
16909 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016910 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016911 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016912 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016913 unsigned DestReg = 0;
16914 switch (Res.first) {
16915 default: break;
16916 case X86::AX: DestReg = X86::RAX; break;
16917 case X86::DX: DestReg = X86::RDX; break;
16918 case X86::CX: DestReg = X86::RCX; break;
16919 case X86::BX: DestReg = X86::RBX; break;
16920 case X86::SI: DestReg = X86::RSI; break;
16921 case X86::DI: DestReg = X86::RDI; break;
16922 case X86::BP: DestReg = X86::RBP; break;
16923 case X86::SP: DestReg = X86::RSP; break;
16924 }
16925 if (DestReg) {
16926 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016927 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016928 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016929 }
Craig Topperc9099502012-04-20 06:31:50 +000016930 } else if (Res.second == &X86::FR32RegClass ||
16931 Res.second == &X86::FR64RegClass ||
16932 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016933 // Handle references to XMM physical registers that got mapped into the
16934 // wrong class. This can happen with constraints like {xmm0} where the
16935 // target independent register mapper will just pick the first match it can
16936 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016937
16938 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016939 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016940 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016941 Res.second = &X86::FR64RegClass;
16942 else if (X86::VR128RegClass.hasType(VT))
16943 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016944 else if (X86::VR256RegClass.hasType(VT))
16945 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016946 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016947
Chris Lattnerf76d1802006-07-31 23:26:50 +000016948 return Res;
16949}